Skip to content

Commit 6af2d1e

Browse files
committed
Add solutions to the More functional patterns chapter
1 parent 0e95d78 commit 6af2d1e

File tree

8 files changed

+149
-0
lines changed

8 files changed

+149
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Intermission: Exercises
2+
3+
1. All expressions are equivalent.
4+
5+
2. The type of `mTh 3` is `Num a => a -> a -> a`.
6+
7+
3. We have
8+
9+
```haskell
10+
addOneIfOdd n = case odd n of
11+
True -> f n
12+
False -> n
13+
where f = \n -> n + 1
14+
15+
addFive x y = \x y -> (if x > y then y else x) + 5
16+
17+
mflip f x y = f y x
18+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module MoreFunctionalPatterns.CaseExpressions.IntermissionExercises where
2+
3+
functionC x y = case compare x y of
4+
GT -> x
5+
_ -> y
6+
7+
ifEvenAdd2 n = case even n of
8+
True -> n+2
9+
_ -> n
10+
11+
nums x = case compare x 0 of
12+
LT -> -1
13+
EQ -> 0
14+
GT -> 1
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module MoreFunctionalPatterns.ChapterExercises.LetUsWriteCode where
2+
3+
-- Question 1
4+
tensDigit :: Integral a => a -> a
5+
tensDigit x = d
6+
where (xLast, _) = x `divMod` 10
7+
(_, d) = xLast `divMod` 10
8+
9+
10+
hunsDigit :: Integral a => a -> a
11+
hunsDigit x = d
12+
where xLast = x `div` 100
13+
d = xLast `mod` 10
14+
15+
-- Question 2
16+
foldBool :: a -> a -> Bool -> a
17+
foldBool x y z = case z of
18+
True -> x
19+
_ -> y
20+
21+
foldBool' :: a -> a -> Bool -> a
22+
foldBool' x y z
23+
| z = x
24+
| otherwise = y
25+
26+
-- Question 3
27+
g :: (a -> b) -> (a, c) -> (b, c)
28+
g f (a, c) = (f a, c)
29+
30+
-- Question 5
31+
roundTrip :: (Show a, Read a) => a -> a
32+
roundTrip = read . show
33+
34+
-- Question 6
35+
roundTrip' :: (Show a, Read b) => a -> b
36+
roundTrip' = read . show
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Multiple Choice
2+
3+
1. A polymorphic function d) may resolve to values of different types, depending on inputs.
4+
5+
2. Two functions named `f` and `g` have types `Char -> String` and
6+
`String -> [String]` respectively.
7+
The composed function `g . f` has the type
8+
b) `Char -> [String]`.
9+
10+
3. A function `f` has the type `Ord a => a -> a -> Bool`
11+
and we apply it to one numeric value,
12+
its type is now d) `(Num a, Ord a) => a -> Bool`.
13+
14+
4. A function with type `(a -> b) -> c` b) is a higher-order function
15+
16+
5. Given the following definition of `f`
17+
```haskell
18+
f :: a -> a
19+
f x = x
20+
```
21+
the type of `f True` is a) `f True :: Bool`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Intermission: Exercises
2+
3+
3. The following function
4+
```haskell
5+
pal xs
6+
| xs == reverse xs = True
7+
| otherwise = False
8+
```
9+
b) returns `True` when `xs` is a palindrome.
10+
11+
5. `pal :: [a] -> Bool` (or `pal :: (Foldable t) => t a -> Bool`).
12+
13+
6. The following function
14+
```haskell
15+
numbers x
16+
| x < 0 = -1
17+
| x == 0 = 0
18+
| x > 0 = 1
19+
```
20+
c) returns an indication of whether its argument is a positive or negative number or zero
21+
22+
8. `numbers :: (Num a, Ord a) => a -> a`
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module MoreFunctionalPatterns.HigherOrderFunctions.IntermissionExercises where
2+
3+
dodgy :: (Num a) => a -> a -> a
4+
dodgy x y = x + y * 10
5+
6+
oneIsOne :: (Num a) => a -> a
7+
oneIsOne = dodgy 1
8+
9+
oneIsTwo :: (Num a) => a -> a
10+
oneIsTwo = (flip dodgy) 2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Intermission: Exercises
2+
3+
1. Given the following declarations
4+
```haskell
5+
k (x, y) = x
6+
k1 = k ((4-1), 10)
7+
k2 = k ("three", (1 + 2))
8+
k3 = k (3, True)
9+
```
10+
a) `k :: (a, b) -> a`.
11+
b) `k2 :: String`, which is different from `k1 :: Num a => a` and `k3 :: Num a => a`.
12+
c) `k1` and `k3` returns `3` as result.
13+
14+
2. We have
15+
```haskell
16+
f :: (a, b, c) -> (d, e, f) -> ((a, d), (c, f))
17+
f (a, b, c) (d, e, f) = ((a, d), (c, f))
18+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module MoreFunctionalPatterns.ChapterExercises.LetUsWriteCodeSpec where
2+
3+
import Test.Hspec
4+
import MoreFunctionalPatterns.ChapterExercises.LetUsWriteCode
5+
6+
spec :: Spec
7+
spec = do
8+
describe "Test roundTrip'" $ do
9+
it "roundTrip' 4" $ do
10+
roundTrip' 4 `shouldBe` 4

0 commit comments

Comments
 (0)