首页 >
> 详细

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

- Cpslp程序语言代写、代做python编程设计、Program程序实验代写 2020-11-25
- Csci 1110作业代做、Data留学生编程代写、Java程序语言调试代做 2020-11-25
- 代写program程序、代做r课程编程、R程序实验代做代做留学生prolog 2020-11-25
- Be491留学生编程代做、代写java，Python/C++程序设计调试ma 2020-11-25
- 代写cmpt 214编程、代做programming语言、代写c/C++程序 2020-11-08
- 代写csci 2122课程、代做program编程实验、C++程序语言代写代 2020-11-08
- Fit5032语言编程代做、代写web程序实验、Web、Html程序语言代做 2020-11-08
- Com3503程序编程代做、Java，C++，Python留学生编程代写代写 2020-11-08
- 代写program程序课程、代写c++编程实验、C/C++编程语言代做 代做 2020-11-08
- Data留学生编程代做、代写python程序、Java，C++程序语言代写 2020-11-08
- 代写secj 1023实验编程、Programming程序代做、代写c++语 2020-11-08
- 代写cmpsc 465编程、代做java程序语言、Python，C++编程设 2020-11-07
- 代做mf 703语言编程、代写programming程序、Sql编程语言调试 2020-11-07
- 954246编程设计调试、代做programming程序、C++编程语言代写 2020-11-07
- Pstat 115程序实验代写、R编程语言调试、Data留学生程序代做 代写 2020-11-07
- Com1005课程编程代做、代写python程序、Java，C++程序语言调 2020-11-07
- Tcp留学生程序代写、Java程序设计调试、Java编程语言代写 帮做r语言 2020-11-07
- 代写program语言编程、代做data留学生程序、Python，Java编 2020-11-07
- 代做cosc2666编程、代写programming程序、C/C++程序语言 2020-11-07
- Digital编程设计代写、代做r程序实验、代写r留学生程序 调试matla 2020-11-07