联系方式

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

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-09-26 09:05

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


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp