-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises1.html
145 lines (90 loc) · 2.89 KB
/
exercises1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A Taste of Haskell</title>
<meta name="author" content="Sergio de Carvalho">
<xmp theme="spacelab" style="display:none;">
# Part 1 - Exercises
1. Predict the _type_ of the following values and confirm them in the REPL (GHCi):
```haskell
e1 = 1 > 2
e2 = 1 * 2
e3 = (1 :: Int) * 2
e4 = (1 :: Float) * 2
e5 = not (not True)
e6 = 'h' : "askell"
e7 = [True] ++ [False]
e8 = head "haskell"
e9 = take 3 "haskell"
e10 = length "haskell"
e11 = take 26 ['a'..]
e12 = sum (take 1000 (filter odd ([1..] :: [Integer])))
e13 = fst ("haskell", (&&))
e14 = snd ("haskell", (&&))
e15 = snd ("haskell", (&& True))
e16 = snd ("haskell", ((&&) True False))
e17 = ("haskell" !! 2, (null "haskell", reverse "haskell"), 1 :: Int)
e18 = ("haskell")
e19 = repeat True
```
1. Predict the _type_ of the following functions and confirm them in the REPL -
don't worry too much with the polymorphic constraints:
```haskell
minor age = age >= 18
bbq sunny temperature = (&&) sunny (temperature > 15)
succeeded codes = null (filter (/=200) codes)
```
1. Write down definitions that match the declared types, replacing the
`undefined` values; it does not matter what the actual values are or what the
functions do, as long as they are type correct:
```haskell
bools :: [Bool]
bools = undefined
nums :: [[Int]]
nums = undefined
record :: ([Char], Int, Bool)
record = undefined
add :: Int -> Int -> Int -> Int
add = undefined
(-+) :: Num a => a -> a -> a -> a
(-+) = undefined
```
1. What's wrong with the following expressions?
```haskell
e1 = 1 > '0'
e2 = (1 :: Int) * (2 :: Integer)
e3 = 'h' ++ "askell"
e4 = "h" : "askell"
e5 = fst [1,2,3]
e6 = fst (1,2,3)
e7 = 2 * - 1
e8 = [1,'1']
9e = "something's wrong"
E10 = "even worse"
e11 = 10 mod 3
e12 = + 2 5
'e13 = "oops"
```
1. Using `head` and `drop` (and brackets!), extract the character `'a'` from the
string `"haskell"`.
1. Using `tail` and `take`, extract the substring `"as"` from the
string `"haskell"`.
1. Given the following string value, construct a string `"all"` from it using
cons (`:`), `head` and `drop`:
```haskell
s = "haskell"
```
1. Given the following tuple `t`, extract the value `True` using `fst` and `snd`:
```haskell
t = (1, (True, "text"))
```
1. Now extract the `'e'` character from the string inside the tuple above using
a combination of tuple and list functions of your choice.
1. Use Hoogle to find a function that replicates a value a number of times -
it's type signature is `Int -> a -> [a]`. How do you use it?
1. Use Hoogle to find out what the `cycle` function does. How do you use it?
</xmp>
<script src="v/0.2/strapdown.js"></script>
</head>
</html>