From 73d974fcfaab953f319ca1b7a677baf5c986506e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Depreeuw?= Date: Sun, 8 Oct 2017 19:31:44 +0200 Subject: [PATCH] Complete first 4 chapters --- 01-all-you-need-is-lambda/README.md | 1 + 02-hello-haskell/2.10-a-head-code.md | 10 +++++ 02-hello-haskell/2.11-chapter-exercises.md | 25 ++++++++++++ 02-hello-haskell/2.5-comprehension-check.md | 17 +++++++++ .../2.6-parentheses-and-association.md | 15 ++++++++ 02-hello-haskell/2.7-heal-the-sick.md | 19 ++++++++++ 02-hello-haskell/src/chapter.hs | 11 ++++++ 02-hello-haskell/src/chapter2.hs | 38 +++++++++++++++++++ 02-hello-haskell/src/practise.hs | 18 +++++++++ 03-strings/3.11-chapter-exercises.md | 30 +++++++++++++++ 03-strings/3.4-scope.md | 5 +++ 03-strings/3.5-syntax-errors.md | 4 ++ 03-strings/src/chapter3.hs | 34 +++++++++++++++++ 03-strings/src/reverse.hs | 11 ++++++ 04-basic-datatypes/4.4-mood-swings.md | 6 +++ 04-basic-datatypes/4.6-find-the-mistakes.md | 6 +++ 04-basic-datatypes/4.9-chapter-exercises.md | 31 +++++++++++++++ 04-basic-datatypes/src/chapter4.hs | 30 +++++++++++++++ 04-basic-datatypes/src/mood.hs | 7 ++++ 19 files changed, 318 insertions(+) create mode 100644 01-all-you-need-is-lambda/README.md create mode 100644 02-hello-haskell/2.10-a-head-code.md create mode 100644 02-hello-haskell/2.11-chapter-exercises.md create mode 100644 02-hello-haskell/2.5-comprehension-check.md create mode 100644 02-hello-haskell/2.6-parentheses-and-association.md create mode 100644 02-hello-haskell/2.7-heal-the-sick.md create mode 100644 02-hello-haskell/src/chapter.hs create mode 100644 02-hello-haskell/src/chapter2.hs create mode 100644 02-hello-haskell/src/practise.hs create mode 100644 03-strings/3.11-chapter-exercises.md create mode 100644 03-strings/3.4-scope.md create mode 100644 03-strings/3.5-syntax-errors.md create mode 100644 03-strings/src/chapter3.hs create mode 100644 03-strings/src/reverse.hs create mode 100644 04-basic-datatypes/4.4-mood-swings.md create mode 100644 04-basic-datatypes/4.6-find-the-mistakes.md create mode 100644 04-basic-datatypes/4.9-chapter-exercises.md create mode 100644 04-basic-datatypes/src/chapter4.hs create mode 100644 04-basic-datatypes/src/mood.hs diff --git a/01-all-you-need-is-lambda/README.md b/01-all-you-need-is-lambda/README.md new file mode 100644 index 0000000..76ca189 --- /dev/null +++ b/01-all-you-need-is-lambda/README.md @@ -0,0 +1 @@ +The answers to Chapter 1 exercises are available in the book. \ No newline at end of file diff --git a/02-hello-haskell/2.10-a-head-code.md b/02-hello-haskell/2.10-a-head-code.md new file mode 100644 index 0000000..42ffab7 --- /dev/null +++ b/02-hello-haskell/2.10-a-head-code.md @@ -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 + diff --git a/02-hello-haskell/2.11-chapter-exercises.md b/02-hello-haskell/2.11-chapter-exercises.md new file mode 100644 index 0000000..bec1925 --- /dev/null +++ b/02-hello-haskell/2.11-chapter-exercises.md @@ -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. \ No newline at end of file diff --git a/02-hello-haskell/2.5-comprehension-check.md b/02-hello-haskell/2.5-comprehension-check.md new file mode 100644 index 0000000..3f220b8 --- /dev/null +++ b/02-hello-haskell/2.5-comprehension-check.md @@ -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)` \ No newline at end of file diff --git a/02-hello-haskell/2.6-parentheses-and-association.md b/02-hello-haskell/2.6-parentheses-and-association.md new file mode 100644 index 0000000..fff075c --- /dev/null +++ b/02-hello-haskell/2.6-parentheses-and-association.md @@ -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. \ No newline at end of file diff --git a/02-hello-haskell/2.7-heal-the-sick.md b/02-hello-haskell/2.7-heal-the-sick.md new file mode 100644 index 0000000..84e39c3 --- /dev/null +++ b/02-hello-haskell/2.7-heal-the-sick.md @@ -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 +``` \ No newline at end of file diff --git a/02-hello-haskell/src/chapter.hs b/02-hello-haskell/src/chapter.hs new file mode 100644 index 0000000..f1108b2 --- /dev/null +++ b/02-hello-haskell/src/chapter.hs @@ -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 \ No newline at end of file diff --git a/02-hello-haskell/src/chapter2.hs b/02-hello-haskell/src/chapter2.hs new file mode 100644 index 0000000..89fbb1f --- /dev/null +++ b/02-hello-haskell/src/chapter2.hs @@ -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 diff --git a/02-hello-haskell/src/practise.hs b/02-hello-haskell/src/practise.hs new file mode 100644 index 0000000..a0f7ffe --- /dev/null +++ b/02-hello-haskell/src/practise.hs @@ -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 \ No newline at end of file diff --git a/03-strings/3.11-chapter-exercises.md b/03-strings/3.11-chapter-exercises.md new file mode 100644 index 0000000..aac3334 --- /dev/null +++ b/03-strings/3.11-chapter-exercises.md @@ -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 diff --git a/03-strings/3.4-scope.md b/03-strings/3.4-scope.md new file mode 100644 index 0000000..d00a54e --- /dev/null +++ b/03-strings/3.4-scope.md @@ -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. diff --git a/03-strings/3.5-syntax-errors.md b/03-strings/3.5-syntax-errors.md new file mode 100644 index 0000000..fdb4199 --- /dev/null +++ b/03-strings/3.5-syntax-errors.md @@ -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. \ No newline at end of file diff --git a/03-strings/src/chapter3.hs b/03-strings/src/chapter3.hs new file mode 100644 index 0000000..ba71d0e --- /dev/null +++ b/03-strings/src/chapter3.hs @@ -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 diff --git a/03-strings/src/reverse.hs b/03-strings/src/reverse.hs new file mode 100644 index 0000000..be2b5c8 --- /dev/null +++ b/03-strings/src/reverse.hs @@ -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" \ No newline at end of file diff --git a/04-basic-datatypes/4.4-mood-swings.md b/04-basic-datatypes/4.4-mood-swings.md new file mode 100644 index 0000000..47f6bfa --- /dev/null +++ b/04-basic-datatypes/4.4-mood-swings.md @@ -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 \ No newline at end of file diff --git a/04-basic-datatypes/4.6-find-the-mistakes.md b/04-basic-datatypes/4.6-find-the-mistakes.md new file mode 100644 index 0000000..476750a --- /dev/null +++ b/04-basic-datatypes/4.6-find-the-mistakes.md @@ -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. \ No newline at end of file diff --git a/04-basic-datatypes/4.9-chapter-exercises.md b/04-basic-datatypes/4.9-chapter-exercises.md new file mode 100644 index 0000000..1e75486 --- /dev/null +++ b/04-basic-datatypes/4.9-chapter-exercises.md @@ -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) + diff --git a/04-basic-datatypes/src/chapter4.hs b/04-basic-datatypes/src/chapter4.hs new file mode 100644 index 0000000..6249663 --- /dev/null +++ b/04-basic-datatypes/src/chapter4.hs @@ -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 \ No newline at end of file diff --git a/04-basic-datatypes/src/mood.hs b/04-basic-datatypes/src/mood.hs new file mode 100644 index 0000000..de03c55 --- /dev/null +++ b/04-basic-datatypes/src/mood.hs @@ -0,0 +1,7 @@ +module Mood where + +data Mood = Woot | Blah deriving Show + +changeMood :: Mood -> Mood +changeMood Blah = Woot +changeMood _ = Blah \ No newline at end of file