首页 >
> 详细

CISC 360 Assignment 1

September 18, 2020

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.

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 do not 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).

a1, Jana Dunfield, CISC 360, Fall 2020 1 2020/9/18

§1 Add your student ID

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 * (3 + x)) 2

=> _______________________ _________________________

=> _______________________ _________________________

=> __ by arithmetic

3.2 Second expression

The other expression is somewhat larger, and uses a function double, which is defined in the

comment.

Hint: The first step does not apply the function double. Haskell sees the expression

(\a -> \b -> a (a 3)) double 0

as the expression

(\a -> \b -> a (a 3)) double

applied to the argument 0.

The expression

(\a -> \b -> a (a 3)) double

is the function (\a -> \b -> a (a 3)) applied to the argument double. That is where the first

step happens.

Hint: The correct solution steps 6 times (as indicated by the structure of blanks).

a1, Jana Dunfield, CISC 360, Fall 2020 2 2020/9/18

§4 tower

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. This will be discussed

more in lecture, but 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:

a1, Jana Dunfield, CISC 360, Fall 2020 3 2020/9/18

§5 Base conversion

• 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 (“Nonexhaustive

patterns in function toNary”) when Haskell “falls off the end” of the definition. You can

fix this by adding an additional guard to toNary.

You should handle all bases between 2 and 10 with the same code. Don’t “hard-code” each

base; you should have no more than two guards (the given one, and one additional guard), and

should not do something like

... = if base == 2 then ...

else if base == 3 then ...

else if base == 4 then ...

a1, Jana Dunfield, CISC 360, Fall 2020 4 2020/9/18

联系我们

- QQ：99515681
- 邮箱：99515681@qq.com
- 工作时间：8:00-23:00
- 微信：codinghelp

- 代写1902/159.360编程、Java编程语言调试、代做python，C 2020-10-28
- Mte4590实验编程代写、Matlab语言程序调试、Matlab课程代做代 2020-10-28
- Program语言课程代做、代写python，C++程序设计、Java编程代 2020-10-28
- Algorithm编程代写、Python，Java程序设计调试、代做c++编 2020-10-28
- 代做ee6435课程语言、代写python留学生编程、Python程序语言调 2020-10-28
- 代写csci-Ua.0101编程、代做java课程编程、代写java实验程序 2020-10-28
- 代写crc实验程序、代做java，C/C++编程语言、Python实验编程调 2020-10-27
- Algorithms编程代写、代做python，C++实验程序、Java编程 2020-10-27
- 代做158.225课程编程、代写modelling程序、C++，Java编程 2020-10-27
- Gui留学生编程代做、代写python编程设计、Java，C++程序代做代写 2020-10-27
- 70015实验程序代做、代写python，Java，C++程序设计、Algo 2020-10-27
- 代写data编程课程、代做python，Java语言程序、C++实验编程代写 2020-10-27
- Comp9021实验代做、Python留学生程序调试、Python编程语言代 2020-10-27
- 代写159.172留学生编程、代做python课程编程、Python程序设计 2020-10-27
- 代做program实验程序、Python编程设计代写、Java，C++语言代 2020-10-27
- 代写fnce90045语言、代做java语言编程、代写python,C/C+ 2020-10-27
- 代做spss|代做留学生processing|代写pyth... 2020-10-27
- C++语言编程调试、Cs 130A语言代写、C++留学生程序代做 代做留学生 2020-10-27
- R程序语言代做、代写data留学生编程、代做r实验程序 代做python程序 2020-10-27
- 代写ssci 599课程、代做r语言程序、R编程设计调试 代写r语言程序|代 2020-10-27