-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChapter2Ex.hs
More file actions
182 lines (134 loc) · 4.38 KB
/
Chapter2Ex.hs
File metadata and controls
182 lines (134 loc) · 4.38 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{-# LANGUAGE NoImplicitPrelude #-}
module Chapter2Ex where
import Prelude (div, error, otherwise, undefined, (+), (++), (-),
(==), (>))
-- 1. Parenthesise the following arithmetic expressions:
{-
2 ^ 3 * 4
(2 ^ 3) * 4
2 * 3 + 4 * 5
(2 ∗ 3) + (4 ∗ 5)
2 + 3 * 4 ^ 5
2 + (3 * (4 ^ 5))
-}
-- 2. Work through the examples from this chapter using ghci.
{-
-}
-- 3. The script below contains three syntactic errors. Correct these errors and then check that your script works properly using ghci.
{-
N = a ’div’ length xs
where
a = 10
xs = [1, 2, 3, 4, 5]
-}
n = a `div` length xs
where a = 10
xs = [1, 2, 3, 4, 5]
-- 4. Show how the library function last that selects the last element of a non-empty list could be defined in terms of the library functions introduced in this chapter. Can you think of another possible definition?
length [] = 0
length (x : xs) = 1 + length xs
[] !! n = error "invalid index"
(x : xs) !! 0 = x
(x : xs) !! n | n > 0 = xs !! (n - 1)
| otherwise = error "invalid index"
last' xs = xs !! (length xs - 1)
{-
last' [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] !! (length [1, 2, 3, 4, 5] - 1)
[1, 2, 3, 4, 5] !! ((1 + length [2, 3, 4, 5]) - 1)
[1, 2, 3, 4, 5] !! ((1 + (1 + length [3, 4, 5])) - 1)
[1, 2, 3, 4, 5] !! ((1 + (1 + (1 + length [4, 5]))) - 1)
[1, 2, 3, 4, 5] !! ((1 + (1 + (1 + (1 + length [5])))) - 1)
[1, 2, 3, 4, 5] !! ((1 + (1 + (1 + (1 + (1 + length []))))) - 1)
[1, 2, 3, 4, 5] !! ((1 + (1 + (1 + (1 + (1 + (0)))))) - 1)
[2, 3, 4, 5] !! (((1 + (1 + (1 + (1 + (1 + (0)))))) - 1) - 1)
[3, 4, 5] !! ((((1 + (1 + (1 + (1 + (1 + (0)))))) - 1) - 1) - 1)
[4, 5] !! (((((1 + (1 + (1 + (1 + (1 + (0)))))) - 1) - 1) - 1) - 1)
[5] !! ((((((1 + (1 + (1 + (1 + (1 + (0)))))) - 1) - 1) - 1) - 1) - 1)
[5] !! 0
5
-}
head [] = error "empty list"
head (x : xs) = x
reverse [] = []
reverse (x : xs) = reverse xs ++ [x]
last'' xs = head (reverse xs)
{-
last'' [1, 2, 3, 4, 5]
head (reverse [1, 2, 3, 4, 5])
head (reverse [2, 3, 4, 5] ++ [1])
head ((reverse [3, 4, 5] ++ [2]) ++ [1])
head (((reverse [4, 5] ++ [3]) ++ [2]) ++ [1])
head ((((reverse [5] ++ [4]) ++ [3]) ++ [2]) ++ [1])
head (((((reverse [] ++ [5]) ++ [4]) ++ [3]) ++ [2]) ++ [1])
head (((((([]) ++ [5]) ++ [4]) ++ [3]) ++ [2]) ++ [1])
head [5, 4, 3, 2, 1]
5
-}
last''' [] = error "empty list"
last''' [x] = x
last''' (x : xs) = last''' xs
{-
last''' [1, 2, 3, 4, 5]
last''' [2, 3, 4, 5]
last''' [3, 4, 5]
last''' [4, 5]
last''' [5]
5
-}
-- 5. Show how the library function init that removes the last element from a non-empty list could similarly be defined in two different ways.
take n [] = []
take 0 xs = xs
take 1 (x : xs) = [x]
take n (x : xs) = x : take (n - 1) xs
init' xs = take (length xs - 1) xs
{-
init' [1, 2, 3, 4, 5]
take (length [1, 2, 3, 4, 5] - 1) [1, 2, 3, 4, 5]
take ((1 + length [2, 3, 4, 5]) - 1) [1, 2, 3, 4, 5]
take ((1 + (1 + length [3, 4, 5])) - 1) [1, 2, 3, 4, 5]
take ((1 + (1 + (1 + length [4, 5]))) - 1) [1, 2, 3, 4, 5]
take ((1 + (1 + (1 + (1 + length [5])))) - 1) [1, 2, 3, 4, 5]
take ((1 + (1 + (1 + (1 + (1 + length []))))) - 1) [1, 2, 3, 4, 5]
take ((1 + (1 + (1 + (1 + (1 + (0)))))) - 1) [1, 2, 3, 4, 5]
take 4 [1, 2, 3, 4, 5]
1 : (take 3 [2, 3, 4, 5])
1 : (2 : (take 2 [3, 4, 5]))
1 : (2 : (3 : (take 1 [4, 5])))
1 : (2 : (3 : ([4])))
[1, 2, 3, 4]
-}
drop n [] = []
drop 0 xs = xs
drop 1 (x : xs) = xs
drop n (x : xs) = drop (n - 1) xs
init'' xs = reverse (drop 1 (reverse xs))
{-
init'' [1, 2, 3, 4, 5]
reverse (drop 1 (reverse [1, 2, 3, 4, 5]))
reverse (drop 1 (reverse [2, 3, 4, 5] ++ [1]))
reverse (drop 1 ((reverse [3, 4, 5] ++ [2]) ++ [1]))
reverse (drop 1 (((reverse [4, 5] ++ [3]) ++ [2]) ++ [1]))
reverse (drop 1 ((((reverse [5] ++ [4]) ++ [3]) ++ [2]) ++ [1]))
reverse (drop 1 ((((([] ++ [5]) ++ [4]) ++ [3]) ++ [2]) ++ [1]))
reverse (drop 1 [5, 4, 3, 2, 1])
reverse [4, 3, 2, 1]
(reverse [3, 2, 1]) ++ [4]
((reverse [2, 1] ++ [3]) ++ [4])
(((reverse [1] ++ [2]) ++ [3]) ++ [4])
((((reverse [] ++ [1]) ++ [2]) ++ [3]) ++ [4])
((((([]) ++ [1]) ++ [2]) ++ [3]) ++ [4])
[1, 2, 3, 4]
-}
init''' [] = error "empty list"
init''' [x] = []
init''' (x : xs) = x : init''' xs
{-
init''' [1, 2, 3, 4, 5]
1 : (init''' [2, 3, 4, 5])
1 : (2 : (init''' [3, 4, 5]))
1 : (2 : (3 : init''' [4, 5]))
1 : (2 : (3 : (4 : init''' [5])))
1 : (2 : (3 : (4 : [])))
[1, 2, 3, 4]
-}