Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
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).