parent
							
								
									290c50f5bd
								
							
						
					
					
						commit
						73d974fcfa
					
				
				 19 changed files with 318 additions and 0 deletions
			
			
		| @ -0,0 +1 @@ | |||||||
|  | The answers to Chapter 1 exercises are available in the book. | ||||||
| @ -0,0 +1,10 @@ | |||||||
|  | # Exercises: A Head Code | ||||||
|  | # Exercise 1 | ||||||
|  | 1. Returns `5`  | ||||||
|  | 2. Returns `5 * 5 = 25` | ||||||
|  | 3. Returns `5 * 6 = 30` | ||||||
|  | 4. Returns `3 + 3 = 6` | ||||||
|  | 
 | ||||||
|  | # Exercise 2 | ||||||
|  | see src/practise.hs | ||||||
|  | 
 | ||||||
| @ -0,0 +1,25 @@ | |||||||
|  | # Chapter Exercises | ||||||
|  | # Parenthesization | ||||||
|  | 1. `(2 + (2 * 3)) - 1` | ||||||
|  | 2. `(^) 10 (1 + 1)` | ||||||
|  | 3. `((2 ^ 2) * (4 ^ 5)) + 1` | ||||||
|  | 
 | ||||||
|  | # Equivalent expressions | ||||||
|  | 1. Equivalent. | ||||||
|  | 2. Equivalent. | ||||||
|  | 3. Different. `(-) 37 400` is equal to `37 - 400` | ||||||
|  | 4. Different. Integer division versus real division. | ||||||
|  | 5. Different. Multiplication has higher precedence. | ||||||
|  | 
 | ||||||
|  | # More fun with functions | ||||||
|  | In the REPL: | ||||||
|  | 
 | ||||||
|  | `let z = 7; x = y ^ 2; waxOn = x * 5; y = z + 8` | ||||||
|  | 
 | ||||||
|  | 1. `waxOn` is a number equal to `1125`. So it would be as if you replace `waxOn` by `1125`. | ||||||
|  | 2. / | ||||||
|  | 3. This will return `1125 * 3` or `3375` | ||||||
|  | 4. see src/chapter.hs | ||||||
|  | 5. see src/chapter.hs | ||||||
|  | 6. see src/chapter.hs | ||||||
|  | 7. waxOff is  basically an for the triple function. | ||||||
| @ -0,0 +1,17 @@ | |||||||
|  | # Exercises: Comprhension check | ||||||
|  | ## Exercise 1 | ||||||
|  | To declare these functions in the REPL, you need to prepend them with **let**. | ||||||
|  | 
 | ||||||
|  | `let half x = x / 2` | ||||||
|  | 
 | ||||||
|  | `let square x = x * x` | ||||||
|  | 
 | ||||||
|  | ## Excercise 2 | ||||||
|  | In the REPL: | ||||||
|  | 
 | ||||||
|  | `let foo x = 3.14 * (x * x)` | ||||||
|  | 
 | ||||||
|  | ## Excercise 3 | ||||||
|  | In the REPL: | ||||||
|  | 
 | ||||||
|  | `let bar x = pi * (x * x)` | ||||||
| @ -0,0 +1,15 @@ | |||||||
|  | # Exercises: Parentheses and Association | ||||||
|  | ## Exercise 1 | ||||||
|  | The parentheses change the result here. | ||||||
|  | 
 | ||||||
|  | Multiplication (*) has a higher precedence, so is normally evaluated first. By adding parentheses to the addition (+), the order in which terms are evaluated is changed. | ||||||
|  | 
 | ||||||
|  | ## Exercise 2 | ||||||
|  | The parenthese do not change the result. | ||||||
|  | 
 | ||||||
|  | Parentheses surround mulitplication, but multiplication has a higher precedence, so it does not change anything. | ||||||
|  | 
 | ||||||
|  | ## Exercise 3 | ||||||
|  | The parentheses change the result here. | ||||||
|  | 
 | ||||||
|  | Division (\\) has a higher precedence. By adding parentheses to the addition, the order in which terms are evaluated is changed. | ||||||
| @ -0,0 +1,19 @@ | |||||||
|  | # Exercises: Heal the Sick | ||||||
|  | # Excercise 1 | ||||||
|  | The space between 3. and 14 should be removed. | ||||||
|  | 
 | ||||||
|  | `let area x = 3.14 * (x * x)` | ||||||
|  | 
 | ||||||
|  | # Exercise 2 | ||||||
|  | b is unknown is this definition, unless previously defined. The former will generate and error, the latter won't, but the intent of the function is to double the input, so `b` should be replaced by `x` | ||||||
|  | 
 | ||||||
|  | `let double x = x * 2` | ||||||
|  | 
 | ||||||
|  | # Exercise 3 | ||||||
|  | The line should start at the same indentation. | ||||||
|  | 
 | ||||||
|  | ``` | ||||||
|  | x = 7 | ||||||
|  | y = 10 | ||||||
|  | f = x + y | ||||||
|  | ``` | ||||||
| @ -0,0 +1,11 @@ | |||||||
|  | -- 4 | ||||||
|  | waxOn = x * 5 | ||||||
|  |   where z = 7 | ||||||
|  |         x = y ^ 2 | ||||||
|  |         y = z + 8 | ||||||
|  | 
 | ||||||
|  | -- 5 | ||||||
|  | triple x = x * 3 | ||||||
|  | 
 | ||||||
|  | -- 6 | ||||||
|  | waxOff x = triple x | ||||||
| @ -0,0 +1,38 @@ | |||||||
|  | -- 2.5 Exercises: Comprehension check | ||||||
|  | 
 | ||||||
|  | -- Exercise 1 | ||||||
|  | half x = x / 2 | ||||||
|  | -- let half x = x / 2 | ||||||
|  | 
 | ||||||
|  | square x = x * x | ||||||
|  | -- let square x = x * x | ||||||
|  | 
 | ||||||
|  | -- Exercise 2 | ||||||
|  | foo x = 3.14 * (x * x) | ||||||
|  | 
 | ||||||
|  | -- Exercise 3 | ||||||
|  | bar x = pi * (x * x) | ||||||
|  | 
 | ||||||
|  | -- 2.6 Exercises: Parentheses and Association | ||||||
|  | 
 | ||||||
|  | -- Exercise 1 | ||||||
|  | -- aThey are different because * has priority over + | ||||||
|  | 
 | ||||||
|  | -- Exercise 2 | ||||||
|  | -- Both are the same because * has priority over + | ||||||
|  | 
 | ||||||
|  | -- Exercise 3 | ||||||
|  | -- They are different because / has priority over + | ||||||
|  | 
 | ||||||
|  | -- 2.7 Exercises: Heal the Sick | ||||||
|  | 
 | ||||||
|  | -- Exercise 1 | ||||||
|  | -- let area x = 3.14 * (x*x) | ||||||
|  | 
 | ||||||
|  | -- Exercise 2 | ||||||
|  | -- let double x = x *  2 | ||||||
|  | 
 | ||||||
|  | -- Exercise 3 | ||||||
|  | x = 7 | ||||||
|  | y = 18 | ||||||
|  | f = x + y | ||||||
| @ -0,0 +1,18 @@ | |||||||
|  | -- The order of the where statements | ||||||
|  | -- does not matter | ||||||
|  | 
 | ||||||
|  | -- 1 | ||||||
|  | foo1 = x * 3 + y | ||||||
|  |   where x = 3 | ||||||
|  |         y = 1000 | ||||||
|  | 
 | ||||||
|  | -- 2 | ||||||
|  | foo2 = x * 5 | ||||||
|  |   where x = 10 * 5 + y | ||||||
|  |         y = 10 | ||||||
|  | 
 | ||||||
|  | -- 3 | ||||||
|  | foo3 = z / x + y | ||||||
|  |   where x = 7 | ||||||
|  |         y = negate x | ||||||
|  |         z = y * 10 | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | # Chapter Exercises | ||||||
|  | ## Reading syntax | ||||||
|  | ### Excercise 1 | ||||||
|  | 1. Correct | ||||||
|  | 2. Incorrect, should be:  `(++) [1,2,3] [4,5,6] | ||||||
|  | 3. Correct | ||||||
|  | 4. Correct | ||||||
|  | 5. Incorrect, should be:  `"hello" !! 4` | ||||||
|  | 6. Correct | ||||||
|  | 7. Incorrect, should be:  `take 4 "lovely"` | ||||||
|  | 8. Correct | ||||||
|  | 
 | ||||||
|  | ### Exercise 2 | ||||||
|  | 1. a -> d | ||||||
|  | 2. b -> c | ||||||
|  | 3. c -> e | ||||||
|  | 4. d -> a | ||||||
|  | 5. e -> b | ||||||
|  | 
 | ||||||
|  | ## Building functions | ||||||
|  | # Exercise 1 | ||||||
|  | 1. `"Curry is awesome" ++ "!"` | ||||||
|  | 2. `("Curry is awesome!" !! 4) : ""` | ||||||
|  | 3. `drop 9 "Curry is awesome!"` | ||||||
|  | 
 | ||||||
|  | # Exercises 2-5 | ||||||
|  | see src/chapter3.hs | ||||||
|  | 
 | ||||||
|  | # Exercise 6 | ||||||
|  | see src/reverse.hs | ||||||
| @ -0,0 +1,5 @@ | |||||||
|  | # Exercises: Scope | ||||||
|  | 1. Yes. | ||||||
|  | 2. No. (Assuming h is not defined previously) | ||||||
|  | 3. No. r is not in scope. Also d is not in scope of r. | ||||||
|  | 4. Yes. | ||||||
| @ -0,0 +1,4 @@ | |||||||
|  | # Exercises: Syntax Errors | ||||||
|  | 1. Won't compile. Should be: `(++) [1,2,3] [4,5,6]` | ||||||
|  | 2. Will not compile. Should be `"<3" ++ " Haskell"` | ||||||
|  | 3. Will compile. | ||||||
| @ -0,0 +1,34 @@ | |||||||
|  | module Chapter3 where | ||||||
|  | 
 | ||||||
|  | -- Building Functions | ||||||
|  | -- Exercise 2 | ||||||
|  | -- This is just the most straighforward solution. Not very flexible. | ||||||
|  | appendBang :: String -> String | ||||||
|  | appendBang x = x ++ "!" | ||||||
|  | 
 | ||||||
|  | -- Again not very flexible, nor safe. Not every string has a fourth element. | ||||||
|  | getFourth :: String -> String | ||||||
|  | getFourth x = (x !! 4) : "" | ||||||
|  | 
 | ||||||
|  | -- Also not safe as there might not be enough characters to drop | ||||||
|  | dropNine :: String -> String | ||||||
|  | dropNine x = drop 9 x | ||||||
|  | 
 | ||||||
|  | -- Exercise 3 | ||||||
|  | thirdLetter :: String -> Char | ||||||
|  | thirdLetter x = x !! 2 | ||||||
|  | 
 | ||||||
|  | -- Exercise 4 | ||||||
|  | letterIndex :: Int -> Char | ||||||
|  | letterIndex x = "Curry is awesome!" !! x | ||||||
|  | 
 | ||||||
|  | -- Exercise 5 | ||||||
|  | -- Because we only want this to work for "Curry is awesome", I've decided to | ||||||
|  | -- only implement it as such and thus it returns just a String | ||||||
|  | rvrs :: String | ||||||
|  | rvrs =  | ||||||
|  |     let s = "Curry is awesome" | ||||||
|  |         start = take 5 s          -- "Curry" | ||||||
|  |         mid   = take 4 $ drop 5 s -- " is " | ||||||
|  |         end   = drop 9 s          -- "awesome"   | ||||||
|  |     in end ++ mid ++ start | ||||||
| @ -0,0 +1,11 @@ | |||||||
|  | module Reverse where | ||||||
|  | 
 | ||||||
|  | rvrs :: String -> String | ||||||
|  | rvrs s = end ++ mid ++ start | ||||||
|  |     where start = take 5 s          -- "Curry" | ||||||
|  |           mid   = take 4 $ drop 5 s -- " is " | ||||||
|  |           end   = drop 9 s          -- "awesome" | ||||||
|  | 
 | ||||||
|  | main :: IO () | ||||||
|  | -- main = print (rvrs "Curry is awesome") | ||||||
|  | main = print $ rvrs "Curry is aweomse" | ||||||
| @ -0,0 +1,6 @@ | |||||||
|  | # Exercises: Mood Swings | ||||||
|  | 1. Mood | ||||||
|  | 2. Blah or Moot | ||||||
|  | 3. Woot is not a type, but a value. You need a type in the type signature. It should be `changeMood :: Mood -> Mood` | ||||||
|  | 4. see src/mood.hs | ||||||
|  | 5. see src/mood.hs | ||||||
| @ -0,0 +1,6 @@ | |||||||
|  | # Exercises: Find the Mistakes | ||||||
|  | 1. Incorrect, should be: `not True && True` | ||||||
|  | 2. Incorrect, should be: `not (x == 6)` | ||||||
|  | 3. Correct | ||||||
|  | 4. Incorrect, should be: `["Merry"] > ["Happy"]` | ||||||
|  | 5. Incorrect, mismatched types. | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | # Chapter Exercises | ||||||
|  | 
 | ||||||
|  | 1. `length :: [a] -> Int` (Why not `Integer`?) | ||||||
|  | 2. Answers: | ||||||
|  |    1. 5 | ||||||
|  |    2. 3 | ||||||
|  |    3. 2 | ||||||
|  |    4. 5 | ||||||
|  | 3. `(/)` takes two `Fractional` types. `length` returns and `Int` which is not `Fractional`. (`Float` and `Double` are) | ||||||
|  | 4. Use `div` instead: `div 6 $ length [1, 2, 3]` | ||||||
|  | 5. `Bool`. `True` (Note: `(+)` has higher precedence) | ||||||
|  | 6. `Bool`. `False` | ||||||
|  | 7. Answers: | ||||||
|  |     1. Works. `Bool`. `True` | ||||||
|  |     2. Won't work. List needs to have elements of the same type. | ||||||
|  |     3. Works. `Int`. `5` | ||||||
|  |     4. Works. `Bool`. `False` | ||||||
|  |     5. Won't work. `(&&)` needs two `Bool`'s, `9` isn't one. | ||||||
|  | 8. see src/chapter4.hs | ||||||
|  | 9. see src/chapter4.hs | ||||||
|  | 10. see src/chapter4.hs | ||||||
|  | 
 | ||||||
|  | ## Correcting syntax | ||||||
|  | see src/chapter4.hs | ||||||
|  | 
 | ||||||
|  | ## Match the function names to their types | ||||||
|  | 1. (c) | ||||||
|  | 2. (b) | ||||||
|  | 3. (a) | ||||||
|  | 4. (d) | ||||||
|  | 
 | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | module Chapter4 where | ||||||
|  | 
 | ||||||
|  | -- Chapter Exercises | ||||||
|  | -- 8 | ||||||
|  | isPalindrome :: (Eq a) => [a] -> Bool | ||||||
|  | isPalindrome x = reverse x == x | ||||||
|  | 
 | ||||||
|  | -- 9 | ||||||
|  | myAbs :: Integer -> Integer | ||||||
|  | myAbs x = if x < 0 then negate x else x | ||||||
|  | 
 | ||||||
|  | -- 10 | ||||||
|  | f :: (a,b) -> (c,d) -> ((b,d), (a,c)) | ||||||
|  | f x y = ((snd x, snd y), (fst x, fst y)) | ||||||
|  | 
 | ||||||
|  | -- Correcting Syntax | ||||||
|  | -- 1 | ||||||
|  | myF :: String -> Int | ||||||
|  | myF xs = w + 1 | ||||||
|  |     where w = length xs | ||||||
|  | 
 | ||||||
|  | -- 2 | ||||||
|  | myId :: a -> a | ||||||
|  | myId x = x | ||||||
|  | 
 | ||||||
|  | -- 3 | ||||||
|  | myFirst :: (a,b) -> a | ||||||
|  | myFirst (a, _) = a | ||||||
|  | -- or myFirst (a, b) = a | ||||||
|  | -- or myFirst t = fst t | ||||||
| @ -0,0 +1,7 @@ | |||||||
|  | module Mood where | ||||||
|  | 
 | ||||||
|  | data Mood = Woot | Blah deriving Show | ||||||
|  | 
 | ||||||
|  | changeMood :: Mood -> Mood | ||||||
|  | changeMood Blah = Woot | ||||||
|  | changeMood _    = Blah | ||||||
					Loading…
					
					
				
		Reference in new issue