程序代写代做代考 Haskell Lab 3: Making Sense of Haskell Definitions CIS 252: Introduction to Computer Science

Lab 3: Making Sense of Haskell Definitions CIS 252: Introduction to Computer Science

The purpose of this lab is to give you (1) familiarity with some of the built-in
Haskell functions and (2) practice with reading Haskell code and figuring out
what it does.

You may work singly or in pairs on this lab: If you work with a partner, turn
in a single solution with both names on it.

1 Your Mission

Grab a copy of the file http://www.cis.syr.edu/˜sueo/cis252/code/lab3-
start.hs: you will be making modifications to it. This file automatically loads
the Data.Char module. It also contains several functions that you will use
throughout this lab.

The Rules of the Game In this lab, you will need to solve a series of puzzles.
Each puzzle involves a Haskell expression with one or more blanks in it, along
with an intended value: you will need to figure out how to fill in the blanks to
create an expression that evaluates to the intended value.

• The only changes you are allowed to make are to replace the various oc-
currences of replaceThisTextWithYourAnswer within the file.

Otherwise, do not alter any of the definitions or type signatures (i.e., con-
tracts) we have provided, and do not replace any occurrences of the vari-
able blank (or its variants).

• You may use any valid Haskell expressions you want to fill in the blanks.
Everything can be answered using things we’ve seen in lecture and pre-
vious labs. You may find it convenient to refer to the Haskell slides from
lecture (available off the course web site), which provide an overview of
the basic built-in Haskell types and some functions that operate on them.

2 An Extended Example

Suppose you are asked to fill in the blanks in the definition

zero = wonder ____ ____

so that zero evaluates to 5. (This example appears in the lab starter file.)

Here’s one way to approach the problem:

1. Look at the definition of wonder in the starter file:

wonder :: Int -> Int -> Int
wonder x y = (div x y) + (mod x y)

What does this function do? It takes an Int x and another Int y: it then
sums div x y and mod x y. Recall that div x y performs integer di-
vision and computes the quotient, and mod x y computes the resulting
remainder. (For example, div 17 3 is 5 and mod 17 3 is 2, because 17
divided by 3 yields 5, with a remainder of 2.)

2. Based on the definition of wonder, we see that zero will evaluate to 5 if
we fill the blanks with Int values m and n such that div m n + mod
m n evaluates to 5. Thus, any of the following values will work (other
choices also work):

11 and 3 (11 divided by 3 is 3 with a remainder of 2)
10 and 2 (10 divided by 2 is 5 with a remainder of 0)
17 and 4 (17 divided by 4 is 4 with a remainder of 1)

3. Having figured out our answer, replace each occurrence of
replaceThisTextWithYourAnswer in the definition of zero with the
appropriate answer (e.g., 11 and 3). Thus, the definition of zero should
now be as follows:

zero :: Int
zero = wonder blank1 blank2
where
blank1 = 11
blank2 = 3

4. Save the file, and then load it into Ghci. If you now evaluate zero, you
should get the required answer: 5.

Page 1 Spring 2017

http://www.cis.syr.edu/~sueo/cis252/code/lab3-start.hs
http://www.cis.syr.edu/~sueo/cis252/code/lab3-start.hs
http://www.cis.syr.edu/courses/cis252/lectures/introHaskell-handouts.pdf
http://www.cis.syr.edu/courses/cis252/lectures/introHaskell-handouts.pdf

Lab 3: Making Sense of Haskell Definitions CIS 252: Introduction to Computer Science

3 Your Problems

Do only one problem at a time: make sure that it’s correct before moving on to
another one. Two important hints/suggestions before you begin:

• You may encounter one or more blanks that need to be filled in with a
function that converts a value of one numeric type to a similar value of
another numeric type. In such cases, these functions may be useful:

toInteger :: Int -> Integer
fromInteger :: Integer -> Int

ceiling, round, floor :: Float -> Integer

• One potentially useful resource when you encounter an unknown Haskell
function is Hoogle (www.haskell.org/hoogle). For example, head to
Hoogle, type toUpper in the search box, hit return, select the first result,
and read how the the function toUpper behaves.

1. one evaluates to 9.0:

one = sqrt ____

2. two evaluates to False:

two = bigOdd ____

3. three evaluates to False:

three = isUpper (toUpper ___)

4. four evaluates to “Syracuse”:

four = ______ ++ “cuse”

5. five evaluates to True:

five = not (isAlphaNum ____)

6. six evaluates to True:

six = baffle ___ ___

7. seven evaluates to True:

seven = ____ /= (query 3 True)

8. eight evaluates to True:

eight = compares ___ ___ ___ ___

9. nine evaluates to False:

nine = enigma (5ˆ3) ___

10. ten evaluates to True:

ten = mystery ___ ___

11. eleven evaluates to 100:

eleven = ceiling ( _____ + 6.7)

12. twelve evaluates to 25:

twelve = query ___ (not False)

13. thirteen evaluates to False:

thirteen = riddle ____

14. fourteen evaluates to True:

fourteen = riddle (_______ (query 3 False))

15. fifteen evaluates to 49:

fifteen = query (wonder ___ ____ ) (bigOdd ____)

Page 2 Spring 2017

http://www.haskell.org/hoogle

Lab 3: Making Sense of Haskell Definitions CIS 252: Introduction to Computer Science

What to Hand In: Hand in (i) a copy of the file lab3-start.hs with your
modifications and (ii) a transcript demonstrating the correctness of your an-
swers. (As always, remember to staple them, along with a completed disclosure
cover sheet!).

To be kind to trees (and graders), do the following:

• Print out your code with two pages side-by-side (i.e., when printing, select
“Layout” preferences and then choose 2 for “Pages per sheet”).

• Generate a clean transcript after you have figured out all the correct an-
swers. Your transcript should consist entirely of entries of the form one,
two, and so forth, such as in the following:

*Main> one
9.0

*Main> two
False

*Main> three
“Syracuse”

How to hand it in: This lab is due by noon on Friday, February 3. Submit it in
lab or to the labeled bin near CST 4-226.

Page 3 Spring 2017

Your Mission
An Extended Example
Your Problems