Chapter 2 Reading Guide
Haskell Starting Out Questions
A. Sections 2.1–2.3
-
There is an annoyance with negative numbers that is mentioned.
- What is the correct way to write five times negative three in Haskell?
- What is a wrong way to do the same thing?
-
Logical operations are basic building blocks of programs. How do you write:
- and
- or
- not
- equals
- not equals
-
Haskell follows mostly the normal order of operations from mathematics. However, in math functions must be written
f(x)
with parentheses, whereas in Haskell functions can be writtenf x
without parentheses. This means we need to fit functions into the order of operations.- Where does function calling fit in the order of operations?
- Give an example of a problem that this could create. (That is, the program does not do what a math student would expect it to do.)
-
Write the function
hypSquare a b
that computes the square of the length of the hypotenuse of a right triangle with legsa
andb
. -
The apostrophe character (
'
) is usually used for one of two purposes. List both:- Purpose 1:
- Purpose 2:
-
Are you allowed to use the apostrophe in function or variable names?
-
In Racket you used (cons x listy) to make a list with x before an existing list. How do you do that in Haskell?
-
In Racket you used (concatenate firstStr secondStr) to put two strings together. How do you do that in Haskell?
-
Slightly longer exercise:
- Make a list
learn
containing the words “this” “is” “all” “good”. - Make a new list called
learnMore
containing “Hopefully” “this” “is” “all” “good” - What is
learnMore !! 4
.
- Make a list
-
Complete the table of equivalents
Racket | Haskell language equivalent functions |
---|---|
first | … |
rest | … |
length | … |
empty? | … |
-
What are the signatures and purposes of these functions?
- init
- last
- take
- drop
-
How can you ask the computer if the list called
xs
contains the number 5? -
There are several more functions that are used less often, but still make it into the basic starting out chapter. Write quick summaries as necessary.
- reverse
- maximum
- minimum
- sum
- product
B. Sections 2.4–2.6
-
Create a list of integers from 1 to 1000 (inclusive).
-
Create a list of letters from
'm'
to'a'
in that order. -
New functions: write signature and purpose for each.
- cycle
- repeat
- replicate
-
Write a function that removes all of the vowels from a word.
-
What is the main difference between a tuple and a list?
-
New functions: write signature and purpose for each.
- fst
- snd
- zip
-
Write a function that takes in a list of words and puts out a list of ordered pairs (word, length of word).
-
Write a function that takes in a list of words and puts out a list with the second word doubled in its own list, the third word tripled in its own list, etc.
For example, the function call
amplify ["hill","rock","star","four"]
should produce
[["hill"],["rock","rock"],["star","star","star"], ["four","four","four","four"]]