首页 > > 详细

CISC 360讲解、Java,c++编程设计辅导、Python程序语言辅导 调试Web开发|辅导Database

CISC 360 Assignment 2
Reminder: All work submitted must be your own.
Some questions ask you to fill in comments in a2.hs.
Late policy: Assignments submitted up to 24 hours late (that is, by 11:59 pm the following
day) will be accepted with a 15% penalty.
0 IMPORTANT: Your file must compile
Your file must load (:load in GHCi) successfully, or we will automatically subtract 30% from your
mark.
If you are halfway through a problem and run out of time, comment out the code that is
causing :load to fail by surrounding it with {- . . . -}, and write a comment describing what you
were trying to do. We can often give (partial) marks for evidence of progress towards a solution,
but we need the file to load and compile.
1 Add your student ID
The file a2.hs will not compile until you add your student ID number by writing it after the =:
-- Your student ID:
student_id :: Integer
student_id =
You do not need to write your name. When we download your submission, onQ includes your
name in the filename.
a2, Jana Dunfield, CISC 360, Fall 2020 1 2020/10/11
§1 Add your student ID
2 Justification
Text processing systems, such as word processors and TeX (which I used for this document), usually
allow text to be “justified” or “fully justified” so that the left and right margins line up neatly. Doing
this “professionally” requires wrapping words, adding hyphens, and spacing words out evenly in a
line. A simpler version of the problem is for monospaced text (where every character has the same
width as every other character) with no word wrapping and no additional space within a line. For
example, this sentence could be wrapped to a width of 9 as shown below.
For examp
le, this
sentence
could be
wrapped t
o a width
of 9 as s
hown belo
w.
The function justify takes an integer w (the line width) and a string, and returns a string with
newline characters ’\n’ inserted every w characters. Also, the last character in the string returned
should be a newline.
{-
justify width s:
Format s with ‘width’ characters per line,
ending in a newline.
Assume width >= 1.
-}
justify :: Int -> [Char] -> [Char]
justify width s = justify_aux 0 width s
We have written justify for you, but the real work is done by justify aux, which we have not
written. justify aux is similar to justify but takes an extra first argument, the “current column”
(with the first column being column zero).
The idea is that, to produce the string "abc\ndef\n", we start by calling justify aux with 0 as
the first argument (because we are “at column zero”). justify aux should return "a" followed by
the result of justify aux with 1 as its first argument. When the current column equals the width,
add a newline character and call justify aux with 0 as the first argument (we have “moved to the
next line”, so we return to column 0).
{- justify_aux column width s:
Format s with ‘width’ characters per line,
ending in a newline.
Maintain current position in ‘column’.
a2, Jana Dunfield, CISC 360, Fall 2020 2 2020/10/11
§2 Justification
Assume column >= 0, width >= 1.
-}
justify_aux :: Int -> Int -> [Char] -> [Char]
justify_aux column width s = undefined
If you’d like to solve the problem in a different way, that’s fine—you may delete justify aux,
or change its type, and change the definition of justify. But if you do that, write a comment
explaining what you’re doing. You may not change the type declaration of justify.
Generally, the read-eval-print loop displays newlines as \n, which isn’t very readable:
*A2> justify 3 "abcdef"
"abc\ndef\n"
To see the output of justify in a more readable form, use putStr:
*A2> putStr (justify 3 "abcdef")
abc
def
However, you should also test justify without putStr. If you had a bug that adds extra spaces
to each line, they wouldn’t show up in the output of putStr (justify. . . , but would show up
when you call justify without putStr:
*A2> justify 3 "abcdef" -- buggy output:
"abc \ndef \n"
*A2> putStr (justify 3 "abcdef") -- bug not evident:
abc
def
3 ‘rewrite’
Haskell has a built-in function ord, with the following type:
ord :: Char -> Int
When applied to a Char, the ord function returns the ASCII code corresponding to that Char.
For example, ord ’A’ returns 65.
Your task is to implement a function named rewrite. Given a String, rewrite returns a copy
of that String with all “important” Chars duplicated.
However, your employer keeps changing their definition of what counts as “important”, so the
first argument to the function rewrite is actually a function that tells you whether a given character
is important.
For example, if the function passed to rewrite is
divisible_by_5 ch = (mod (ord ch) 5 == 0)
then every character that is evenly divisible by 5 is “important” and should be duplicated.
Some examples:
a2, Jana Dunfield, CISC 360, Fall 2020 3 2020/10/11
§3 ‘rewrite’
rewrite divisible_by_5 "" should evaluate to ""
rewrite divisible_by_5 "Queen’s" should evaluate to "Queenn’ss"
rewrite divisible_by_5 "Cataraqui" should evaluate to "Cataraquii"
rewrite divisible_by_5 "Combinatory Logic" should evaluate to "Combiinnatory Logiic"
4 Comparing lists
4a. Fill in the definition of listCompare, which takes two (Haskell) lists of Integers, and should
return a list of Bools such that:
• if the kth element of the first list is equal to the kth element of the second list, the kth element
of the result should be True (because the elements are the same);
• if the kth element of the first list is not equal to the kth element of the second list, the kth
element of the result should be False (because the elements are different);
• if the first and second lists are of different lengths, the result should be “padded” with False,
so that the result list is as long as the longer input.
Examples:
listCompare [1, 2, 4] [3, 2, 0] should be [False, True, False]
^^^^^ ^^^^ ^^^^^
1 /= 3 2 == 2 4 /= 0
listCompare [1, 2, 1, 1] [1, 2] should be [True, True, False, False]
^^^^^^^^^^^^ ^^^^^^ ^^^^ ^^^^ ^^^^^ ^^^^^
length 4 length 2 1 == 1 2 == 2 2nd list 2nd list
has no 3rd has no 4th
element element
(resulting list is length 4)
4b. In the comment “Q4b”, briefly explain why listCompare cannot be implemented by
listCompare :: [Integer] -> [Integer] -> [Bool]
listCompare = zipWith (==)
4c.
The function listCompare only works with integer lists. Fill in the definition of polyCompare,
which takes two arguments:
1. a comparison function eq, of type a -> a -> Bool, which takes two a’s and returns True if
they are the same, and False if they are different;
2. a list whose elements have type a;
3. another list whose elements have type a.
a2, Jana Dunfield, CISC 360, Fall 2020 4 2020/10/11

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!