Skip to content

Commit d6f92da

Browse files
committed
Add solutions to the Basic data types chapter
1 parent ce47de5 commit d6f92da

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Intermission: Exercises
2+
3+
Given the following datatype
4+
```haskell
5+
data Mood = Blah | Woot deriving Show
6+
```
7+
8+
1. The type constructor is `Mood`.
9+
10+
2. If the function requires a `Mood` value,
11+
we could use `Blah` and `Woot`.
12+
13+
3. `changeMood :: Mood -> Woot` does not type check because
14+
`Woot` is a data constructor, not a type constructor.
15+
16+
4. We have
17+
```haskell
18+
changeMood Blah = Woot
19+
changeMood _ = Blah
20+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module BasicDatatypes.ChapterExercises.Awesome where
2+
3+
awesome = ["Papuchon", "curry", ":)"]
4+
alsoAwesome = ["Quake", "The Simons"]
5+
allAwesome = [awesome, alsoAwesome]
6+
7+
-- Question 1
8+
-- length :: [a] -> Int
9+
-- or length :: (Foldable t) => t a -> Int
10+
11+
-- Question 2
12+
-- length [1, 2, 3, 4, 5] = 5
13+
-- length [(1, 2), (2, 3), (3, 4)] = 3
14+
-- length allAwesome = 2
15+
-- length (concat allAwesome) = 5
16+
17+
-- Question 3
18+
-- 6 / 3 works
19+
-- but 6 / length [1, 2, 3] does not type check because
20+
-- Int has no instance of Fractional
21+
22+
-- Question 4
23+
-- 6 `div` length [1, 2, 3]
24+
25+
-- Question 5
26+
-- 2 + 3 == 5 :: Bool
27+
28+
-- Question 6
29+
-- let x = 5
30+
-- x + 3 == 5 :: Bool
31+
32+
-- Question 7
33+
q71 = length allAwesome == 2
34+
q72 = length ['1', 'a', '3', 'b']
35+
q73 = length allAwesome + length awesome
36+
q74 = (8 == 8) && ('b' < 'a')
37+
q75 = (8 == 8) && True
38+
39+
-- Question 8
40+
isPalindrome :: (Eq a) => [a] -> Bool
41+
isPalindrome x = x == reverse x
42+
43+
-- Question 9
44+
myAbs :: Integer -> Integer
45+
myAbs x = if x < 0 then -x else x
46+
47+
-- Question 10
48+
f :: (a, b) -> (c, d) -> ((b, d), (a, c))
49+
f ab cd = ((snd ab, snd cd), (fst ab, fst cd))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Match the function names to their types
2+
3+
1. c) `Show a => a -> String` is the type of `show`.
4+
5+
2. b) `Eq a => a -> a -> Bool` is the type of `==`.
6+
7+
3. a) `(a, b) -> a` is the type of `fst`.
8+
9+
4. d) `Num a => a -> a -> a` is the type of `(+)`.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module BasicDatatypes.ChapterExercises.ReadingSyntax where
2+
3+
-- Question 1
4+
x = (+)
5+
f xs = w `x` 1
6+
where w = length xs
7+
8+
-- Question 2
9+
id' = \ x -> x
10+
11+
-- Question 3
12+
head' = \ (x : xs) -> x
13+
14+
-- Question 4
15+
f' (a, b) = a
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module BasicDatatypes.ComparingValues.IntermissionExercises where
2+
3+
-- Question 1
4+
a = not True && True
5+
6+
-- Question 2
7+
b = not (5 == 6)
8+
9+
-- Question 3
10+
c = (1 * 2) > 5
11+
12+
-- Question 4
13+
d = "Merry" > "Happy"
14+
15+
-- Question 5
16+
e = ['1', '2', '3'] ++ "look at me!"

0 commit comments

Comments
 (0)