associative.
An element e is said to be an identity element of ⊕ if x ⊕ e = e ⊕ x = x for all x . What are the identity elements of addition, concatenation and functional composition?
Exercise F
My wife has a book with the title
EHT CDOORRSSW AAAGMNR ACDIINORTY .
It contains lists of entries like this:
6-letter words
--------------
...
eginor: ignore,region
eginrr: ringer
eginrs: resign,signer,singer
...
Yes, it is an anagram dictionary. The letters of the anagrams are sorted and the results are stored in dictionary order. Associated with each anagram are the English words with the same letters. Describe how you would go about designing a function anagrams :: Int -> [Word] -> String
so that anagrams n takes a list of English words in alphabetical order, extracts just the n -letter words and produces a string that, when displayed, gives a list of the anagram entries for the n -letter words. You are not expected to be able to define the various functions; just give suitable names and types and describe what each of them is supposed to do.
Exercise G
Let’s end with a song:
One man went to mow
Went to mow a meadow
One man and his dog
Went to mow a meadow
Two men went to mow
Went to mow a meadow
Two men, one man and his dog
Went to mow a meadow
Three men went to mow
Went to mow a meadow
Three men, two men, one man and his dog
Went to mow a meadow
Write a Haskell function song :: Int -> String so that song n is the song when there are n men. Assume n<10 .
To print the song, type for example
ghci> putStrLn (song 5)
The function putStrLn will be explained in the following chapter. I suggest starting with
song n = if n==0 then ""
else song (n-1) ++ "\n" ++ verse n
verse n = line1 n ++ line2 n ++ line3 n ++ line4 n
This defines song recursively .
1.7 Answers
Answer to Exercise A
map double [1,4,4,3]
= [2,8,8,6]
map (double . double) [1,4,4,3]
= [4,16,16,12]
map double []
= []
You will gather from this that [] denotes the empty list.
All the following equations hold:
sum . map double
= double . sum
sum . map sum
= sum . concat
sum . sort
= sum
In fact, each of these three equations are consequences of the three simpler laws:
a*(x+y)
= a*x + a*y
x+(y+z)
= (x+y)+z
x+y
= y+x
Of course, we don’t know yet how to prove that the equations hold. (By the way, to avoid fuss we will often use a typewriter = sign to denote the equality of two Haskell expressions written in typewriter font. But a mathematical = sign is used in equations such as sin 2 θ = 2 sin θ cos θ .) Answer to Exercise B
Both sin theta^2 and (sin theta)^2 are okay, but not sin^2 theta .
Here is the rendering of sin 2 θ /2 π in Haskell:
sin (2*theta) / (2*pi)
Note that
sin (2*theta) / 2 pi = (sin (2 theta) / 2) * pi
which is not what we want. The reason is that operators such as / and * at the same level of precedence associate to the left in expressions. More on this in the next chapter.
Answer to Exercise C
'H'
:: Char
"H"
:: [Char]
2001
:: Integer
"2001"
:: [Char]
By the way, '\' is used as an escape character, so '\n' is the newline character, and '\t' is the tab character. Also, '\\' is the backslash character, and "\\n" is a list of two characters, a backslash and the letter n . As a consequence, the file path C:\firefox\stuff is written as the Haskell string "C:\\firefox\\stuff" .
[1,2,3] ++ [3,2,1]
= [1,2,3,3,2,1]
"Hello" ++ " World!"
= "Hello World!"
[1,2,3] ++ []
= [1,2,3]
"Hello" ++ "" ++"World!"
= "HelloWorld!"
If you got the last two right, you will have appreciated that [] is an empty list of anything, but "" is an empty list of characters.
Answer to Exercise D
The clue is in the phrase ‘converting each letter in each word to lowercase’. Converting each letter in a single word is expressed by map toLower , so the answer is map (map toLower) . words . That means the following equation holds: words . map toLower = map (map toLower) . words
Answer to Exercise E
Numerical addition, list