Chapter 4 Reading Guide
Sections 4.1–4.3
-
A pattern starts out with “dog”,“cat”, “cat dog”, “cat dog cat”, “cat dog cat cat dog”, “cat dog cat cat dog cat dog cat” and so on - each new string is created by appending the previous two with a space in between them. Write a function that takes in the number of the word in the sequence and returns the word, starting with 1 is “dog”.
-
Write the function
scalar_mult :: (Num a) => a -> (a,a) -> (a,a)
that multiplies each number in a coordinate pair by the same number, soscalar_mult 5 (1,3) == (5,15)
. -
Write a signature and function body for
pair_prod
that takes in a list of ordered pairs of numbers and returns a list of the products of the pairs, sopair_prod [(3,5), (10,0), (7,3)]===[15,0,21]
. -
What pattern do you use to recursively process the first item in a list and the rest of the list?
-
Are parentheses necessary when writing a function using the pattern from the previous question? Explain why or why not.
-
What Haskell function is used to indicate that the user is asking the program to do something impossible?
-
A function
two_of
takes in a list of showable numbers and puts out a String that says “__ is item 2 of __”. For exampletwo_of [10,25,50] == "25 is item 2 of [10,25,50]". Write the signature and function definition for
two_of`. -
Write the type signature and function definition for
double_double
which takes in any list and produces the same kind of list. Thedouble_double
function makes a list where the first term of the input appears twice, follwed by the whole input twice, so
double_double [3] == [3,3,3,3]
double_double [4,5] == [4,4,4,5,4,5]
double_double [5,10,15] == [5,5,5,10,15,5,10,15]
-
Write a function that takes in a number and returns “small” if the number is below 10, “medium” if the number is between 10 and 20 (inclusive), and “large” if the number is larger than 20. Do not use the
if...then...else
construct. -
Define the function
burgers_of
using an infix definition so that
3 `burgers_of` "beef" == "Three hamburgers"
5 `burgers_of` "soy" == "Five tofu burgers"
any other set of inputs gives "No thanks, not hungry"
- Repeating yourself.
- What code segment do you see repeated in the version of the
densityTell
function that uses the mass and volume? - What experience does the author compare writing to writing this code? (“Notice that we repeat ourselves…”)
- What does the book suggest that you should do instead of this coding style?
- What code segment do you see repeated in the version of the
Sections 4.4–4.5
-
Visibility. What is the difference between where you can see terms from
let
bindings andwhere
bindings? -
The book says
let
bindings are expressions. What does this mean? Even if you don’t know, at least give one consequence of that fact. -
List five different ways to use
let
bindings: -
You want to define two different variables in one let binding. Give two ways you can separate those definitions.
-
Are patterns in function definitions and the “case” statement interchangeable? Give one way they are and one way they are not.