Skip to content

Commit 89fe610

Browse files
committed
Add solutions to the Strings chapter
1 parent d6f92da commit 89fe610

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Strings.ChapterExercises.BuildingFunctions where
2+
3+
-- Question 3
4+
thirdLetter :: String -> Char
5+
thirdLetter x = x !! 2
6+
7+
-- Question 4
8+
letterIndex :: Int -> Char
9+
letterIndex x = "Curry is awesome!" !! x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Reading syntax
2+
3+
1. For the following lines of code, read the syntax carefully and
4+
decide if they are written correctly.
5+
a) concat [[1, 2, 3], [4, 5, 6]]
6+
b) (++) [1, 2, 3] [4, 5, 6]
7+
c) (++) "hello" " world"
8+
d) ["hello" ++ " world"]
9+
e) "hello" !! 4
10+
f) (!!) "hello" 4
11+
g) take 4 "lovely"
12+
h) take 3 "awesome"
13+
14+
2. We have
15+
a) `concat [[1 * 6], [2 * 6], [3 * 6]] = [6, 12, 18]`
16+
b) `"rain" ++ drop 2 "elbow" = "rainbow"`
17+
c) `10 * head [1, 2, 3] = 10`
18+
d) `(take 3 "Julie") ++ (tail "yes") = "Jules"`
19+
e) `concat [tail [1, 2, 3], tail [4, 5, 6], tail [7, 8, 9]] = [2,3,5,6,8,9]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Intermission: Exercises
2+
3+
1. In the following code
4+
```haskell
5+
Prelude> let x = 5
6+
Prelude> let y = 7
7+
Prelude> let z = x * y
8+
```
9+
`y` is in scope for `z`.
10+
11+
2. In the following code
12+
```haskell
13+
Prelude> let f = 3
14+
Prelude> let g = 6 * f + h
15+
```
16+
`h` is not in scope of `g`.
17+
18+
3. In the following code
19+
```haskell
20+
area d = pi * (r * r)
21+
r = d / 2
22+
```
23+
everything is in scope for executing `area`.
24+
25+
4. In
26+
```haskell
27+
area d = pi * (r * r)
28+
where r = d / 2
29+
```
30+
`r` and `d` are in scope for `area`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Intermission: Exercises
2+
3+
1. `(++) [1, 2, 3] [4, 5, 6]`
4+
5+
2. `"<3" ++ " Haskell"`
6+
7+
3. `concat ["<3", " Haskell"]`

0 commit comments

Comments
 (0)