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"