Skip to content

Commit 876acd6

Browse files
committed
Review ch2 exercise solutions
1 parent 0bd9785 commit 876acd6

11 files changed

+93
-66
lines changed

ch2/01-comprehension-check.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Exercises: Comprehension Check
2+
3+
1.
4+
5+
Given,
6+
7+
half x = x / 2
8+
square x = x * x
9+
10+
Then, in the REPL we write them as
11+
12+
> half x = x / 2
13+
> square x = x * x
14+
15+
> half 5
16+
2.5
17+
> half 4
18+
2
19+
> :t half
20+
half :: Fractional a => a -> a
21+
22+
> square 4
23+
16
24+
> :t square
25+
square :: Num a => a -> a
26+
27+
2.
28+
29+
areaOfCircle r = 3.14 * (r * r)
30+
-- or
31+
-- areaOfCircle r = 3.14 * square r
32+
33+
3.
34+
35+
areaOfCircle r = pi * (r * r)
36+
-- or
37+
-- areaOfCircle r = pi * square r
File renamed without changes.

ch2/03-heal-the-sick.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Exercises: Heal the Sick
2+
3+
1. area x = 3.14 * (x * x) -- extra space 3._14
4+
5+
2. double x = x * 2 -- variable b not in scope
6+
7+
-- or
8+
-- double b = b * 2
9+
10+
3.
11+
12+
x = 7
13+
y = 10 -- incorrect indentation
14+
f = x + y
File renamed without changes.

ch2/04-a-head-code.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Exercises: A Head Code
2+
3+
1.
4+
5+
> let x = 5 in x
6+
5
7+
8+
2.
9+
10+
> let x = 5 in x * x
11+
25
12+
13+
3.
14+
15+
> let x = 5; y = 6 in x * y
16+
30
17+
18+
4.
19+
20+
> let x = 3; y = 1000 in x + 3
21+
6

ch2/ex6.txt renamed to ch2/05-chapter-exercises.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@ Equivalent expressions
2929

3030
More fun with functions
3131

32-
let z = 7 ; z = 7
33-
let y = z + 8 ; y = 15
34-
let x = y ^ 2 ; x = 225
35-
let waxOn = x * 5 ; waxOn = 1125
32+
> z = 7 ; z = 7
33+
> y = z + 8 ; y = 15
34+
> x = y ^ 2 ; x = 225
35+
> waxOn = x * 5 ; waxOn = 1125
3636

3737
1. 10 + waxOn = 1135
3838
(+10) waxOn = 1135
3939
(-) 15 waxOn = 15 - waxOn = -1110
4040
(-) waxOn 15 = waxOn - 15 = 1110
4141

42-
2. let triple x = x * 3
42+
2.
4343

44-
3. triple waxOn = triple 1125
45-
= 1125 * 3
46-
= 3375
44+
> triple x = x * 3
45+
46+
3.
47+
48+
triple waxOn = triple 1125
49+
= 1125 * 3
50+
= 3375
4751

4852
4. See waxOn.hs
4953

ch2/NOTES.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
There is a difference between `div`/`mod` and `quot`/`rem`. Be careful when
2-
choosing between these functions.
1+
Notes:
32

4-
See http://stackoverflow.com/a/28027235/391924.
3+
- [Using GHCi](https://downloads.haskell.org/ghc/8.6.5/docs/html/users_guide/ghci.html)
4+
- There is a difference between `div`/`mod` and `quot`/`rem`. Be careful when choosing between these functions. See http://stackoverflow.com/a/28027235/391924.
5+
- Haskell doesn't evaluate everything to canonical or normal form by default. Instead, it only evaluates to weak head normal form (WHNF) by default.
6+
7+
Follow-up resources:
8+
9+
- [Let vs Where](https://wiki.haskell.org/Let_vs._Where)
10+
- [How to desugar Haskell code](https://www.haskellforall.com/2014/10/how-to-desugar-haskell-code.html)

ch2/ex1.txt

Lines changed: 0 additions & 21 deletions
This file was deleted.

ch2/ex3.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

ch2/ex4.hs

Lines changed: 0 additions & 13 deletions
This file was deleted.

ch2/ex4.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)