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 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.- Find the values of
sqrt (2 + 7)
and(sqrt 2) + 7
. Findsqrt 2 + 7
. Where is Haskell putting the parentheses? - Find the values of
sqrt (9 * 2)
and(sqrt 9) * 2
. Findsqrt 9 * 2
. Where is Haskell putting the parentheses? - Define
x = sqrt(pi)
. Find the values0.1+(sin x)^2
and0.1+sin(x^2)
. How does Haskell interpret0.1 + sin x^2
? - 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.)
Having trouble answering this question? Search the chapter for the word “precedence”.
- Find the values of
-
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. (Also: see the Tidy Details section at the end.) 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)
or(list* 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"]]
Tiny Details
The apostrophe is on the key with the double quote (on a US keyboard),
so look for '
and "
together. The grave accent is on a key with
the tilde, so look for ~
and `
together. Minutae:
The difference between the apostrophe and the grave accent is hard to
see in most typefaces, but they mean totally different things to
Haskell. Do not mix them. Some word processors (Google Docs) also
convert apostrophe to a curly quote (the “Smart Quotes”
feature). Haskell does not understand the smart single quote at all.