Posted: March 30th, 2009 | Author: Michel Rijnders | Filed under: Haskell, Real World Haskell | No Comments »
My answers to the exercises of the second chapter.
Page 25:
1. What are the types of the following expressions?
False :: Bool
(["foo", "bar"], 'a') :: ([String], Char)
[(True, []), (False, [['a']])] :: [(Bool,[[Char]])]
Page 39:
1. Haskell provides a standard function, last :: [a] -> a, that returns the last element of a list. From reading the type alone, what are the possible valid behaviors (omitting crashes and infinite loops) that this function could have?
From the type alone there is no way to find out what the real type is, neither is there a way to manipulate a value of that type, thus the “only” thing the code can do is return an element of the list.
What are a few things that this function clearly cannot do?
- Return the size of the list.
- Reverse the list.
2. Write a function, lastButOne, that returns the element before the last.
lastButOne = head . tail . reverse
Load your lastButOne function into ghci and try it out on lists of different lengths. What happens when you pass it a list that’s too short?
Prelude> lastButOne "a"
*** Exception: Prelude.head: empty list
Posted: March 28th, 2009 | Author: Michel Rijnders | Filed under: Haskell, Real World Haskell | No Comments »
Here are my answers to the exercises of chapter 1.
1. Enter the following expressions into ghci What are their types?
Prelude> 5 + 8
5 + 8
13
it :: Integer
Prelude> 3 * 5 + 8
3 * 5 + 8
23
it :: Integer
Prelude> 2 + 4
2 + 4
6
it :: Integer
Prelude> (+) 2 4
(+) 2 4
6
it :: Integer
Prelude> sqrt 16
sqrt 16
4.0
it :: Double
Prelude> succ 6
succ 6
7
it :: Integer
Prelude> succ 7
succ 7
8
it :: Integer
Prelude> pred 9
pred 9
8
it :: Integer
Prelude> pred 8
pred 8
7
it :: Integer
Prelude> sin (pi / 2)
sin (pi / 2)
1.0
it :: Double
Prelude> truncate pi
truncate pi
3
it :: Integer
Prelude> round 3.5
round 3.5
4
it :: Integer
Prelude> round 3.4
round 3.4
3
it :: Integer
Prelude> floor 3.7
floor 3.7
3
it :: Integer
Prelude> ceiling 3.3
ceiling 3.3
4
it :: Integer
2. From ghci, type :? to print some help. Define a variable and then type :show bindings. What do you see?
Prelude> let x = 1
let x = 1
Prelude> :show bindings
:show bindings
it :: Integer = 4
x :: Integer = _
Prelude> x
x
1
Prelude> :show bindings
:show bindings
it :: Integer = 4
x :: Integer = 1
3. The words function counts the number of words in a string. Modify the WC.hs example in order to count the number of words in a file.
main = interact wordCount
where wordCount input = show (length (words input)) ++ "n"
4. Modify the WC.hs example again, in order to print the number of characters in a file.
main = interact wordCount
where wordCount input = show (length input) ++ "n"
Posted: March 28th, 2009 | Author: Michel Rijnders | Filed under: Haskell | No Comments »
For some weird reason I haven’t done any Haskell programming since I finished my thesis last year. Probably for the same reason I have not read Real World Haskell yet, though I bought it immediately when it came out. So to get back into Haskell I’m finally going to read the book; I’ll post comments, solutions to the exercises, and more here.