CISC 360 Piazza

【CISC 360 Piazza】CISC 360 Assignment 1
due Tuesday, 2021–01–26 at 11:59pm, via onQ
Jana Dunfield
January 17, 2021
Reminder: All work submitted must be your own. You may (and should) ask for help from the
instructor, TAs or on Piazza.
Most of the questions on this assignment are about writing code, but a few questions are about
stepping. Please write your answers within the comments indicated in the file a1.hs (search for
Q3.1 and Q3.2).
Start early: Even if some questions look easy to you, Haskell is a very different language and
you may need more time than you think.
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.
0 Document your code
This is basically a “fill-in-the-blanks” assignment, so you will not need to document much, unless
you declare new functions. In that case, you need to write a comment that explains what the
function does.
0 Strive for simplicity
You should try to find a simple solution. You do not have to find the simplest solution to get
full marks, but you should not have an excessively complicated solution. Marks may be deducted if
your solution is too complicated. If you are worried about whether your solution is too complicated,
contact the instructor.
0 Be careful with library functions
Haskell has a rather large built-in library. This assignment is not about how to find library functions,
but about how to use some of the core features of Haskell. You will not receive many marks if you
just call a library function that solves the whole problem. The point is to solve the problem yourself.
a1, Jana Dunfield, CISC 360, W. 2021 1 2021/1/17
§1 Add your student ID
If you are not sure whether you are calling a library function that solves the whole problem,
contact the instructor. Note that if we suggest a library function, you may certainly use it.
(The only way I know to avoid this issue is to craft problems that are complicated and arbitrary,
such that no library function can possibly solve them. I don’t like solving complicated and arbitrary
problems, and you probably don’t either.)
1 Add your student ID
The file a1.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 don’t need to write your name. When we download your submission, onQ includes your
name in the filename.
2 Writing and testing small Haskell functions
2.1 between
This function takes three integers m, n and p, and should return True if m ≤ n ≤ p, and return
False otherwise.
Fill in the definition of between by replacing the word undefined with appropriate Haskell
code, and, if necessary, deleting the = (for example, if you decide to define between using guards).
We have already written five test cases for between, called test between1, 2, etc. These are
combined into test between, which is True only if all five tests pass.
2.2 carry
The carry function takes two integers, and should return 1 if both integers are odd, and 0 otherwise
(if both are even, or if one of them is even and the other is odd). (The name comes from the fact
that its result is the “carry bit” produced by adding the least significant bits of the two integers.)
3 Stepping questions
3.1 First expression
These questions ask you to step two small expressions. For example, the first stepping question asks
you to do the three steps needed to get the result. Replace the parts. (If you want to check
the last line, you can find it by entering the expression into GHCi.)
(\x -> x - (4 + x)) 1
=> _ _
=> _ _
=> __ by arithmetic
a1, Jana Dunfield, CISC 360, W. 2021 2 2021/1/17
§3 Stepping questions
3.2 Second expression
The other expression is somewhat larger, and uses a function triple, which is defined in the
comment.
Hint: The first step does not apply (call) the function triple. Haskell sees the expression
(\a -> (\b -> b (b 3))) 0 triple
as the expression
(\a -> (\b -> b (b 3))) 0
applied to the argument triple.
The expression
(\a -> \b -> (b (b 3))) 0
is the function (\a -> (\b -> b (b 3))) applied to the argument 0. That is where the first step
happens.
Hint: The correct solution steps 6 times (as indicated by the structure of blanks).
4 tower
In this question, you need to write a function tower. Your function should be recursive—its definition
will call itself—and should not use lists.
The idea is that tower is specified by the following equation:
The only remaining problem is that you may want to think of the Π product as looping from k
to n, but Haskell does not have loops, so you have to use recursion instead. The example of diag
from the lecture notes may be helpful: diag n computes the sum of integers from 0 up to n, but
does not use a loop. Instead, it calls itself recursively on n?1. You can think of it as counting down
from n to 0.
The file a1.hs includes a few tests for tower.
5 Base conversion
The functions you’ll write for this question return Haskell strings, which are lists of characters. We
have not discussed lists in any detail; however, for this problem, you should only need to know the
following:
? To return a string containing a single character 0, write
[’0’]
You could also write "0".
? To concatenate (append) two strings, use the built-in function ++. For example:
[’1’, ’2’, 3’] ++ [’4’]
returns "1234", which is the same thing as [’1’, ’2’, ’3’, ’4’].
5.1 toBinary
Read the specification in the comment immediately above toBinary.
Fill in the definition of toBinary by replacing the word undefined with appropriate Haskell
code, and, if necessary, deleting the = (for example, if you decide to define toBinary using guards).
5.2 toNary
Read the specification in the comment immediately above toNary.
We have already written part of the definition, to check for an invalid base—toNary only needs
to handle bases between 2 and 10.
If you pass a valid base to toNary, you will get an error when Haskell reaches the “otherwise”
guard. Replace “undefined” with your code.
You should handle all bases between 2 and 10 with the same code. Don’t “hard-code” each
base, that is, don’t do something like
... = if base == 2 then ...
else if base == 3 then ...
else if base == 4 then ...
That would fall under the category of being “excessively complicated”, and would lose marks.
a1, Jana Dunfield, CISC 360, W. 2021 4 2021/1/17

    推荐阅读