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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
module List
import Result:Result:*
pub type Err =
| Empty
// Using the Erlang C BIF implementation.
//
external length : fn(List(a)) { Int } = :erlang :length
test length {
length([]) |> Assert.equal(_, 0)
length([1]) |> Assert.equal(_, 1)
length([1, 1]) |> Assert.equal(_, 2)
length([1, 1, 1]) |> Assert.equal(_, 3)
}
// Using the Erlang C BIF implementation.
//
pub external fn reverse(List(a)) { List(a) } = :erlang :reverse
test reverse {
length([]) |> Assert.equal(_, [])
length([1, 2, 3, 4, 5]) |> Assert.equal(_, [5, 4, 3, 2, 1])
}
pub fn is_empty(list) {
list == []
}
test is_empty {
is_empty([]) |> Assert.true
is_empty([1]) |> Assert.false
}
pub fn member(list, elem) {
case list {
| [] => False
| elem :: _ => True
| _ :: rest => member(rest, elem)
}
}
test is_member {
is_member([0, 4, 5, 1], 1) |> Assert.true
is_member([0, 4, 5, 7], 1) |> Assert.false
is_member([], 1) |> Assert.false
}
pub fn head(list) {
case list {
| [] => Error(Err:Empty)
| elem :: _ => Ok(elem)
}
}
test head {
head([0, 4, 5, 7])
|> Assert.equal(_, Ok(0))
head([])
|> Assert.equal(_, Error(Err:Empty))
}
pub fn tail(list) {
case list {
| [] => Error(Err:Empty)
| _ :: rest => Ok(rest)
}
}
test tail {
tail([0, 4, 5, 7])
|> Assert.equal(_, Ok([4, 5, 7]))
tail([0])
|> Assert.equal(_, Ok([]))
tail([])
|> Assert.equal(_, Error(Err:Empty))
}
pub fn filter(list, fun) {
filter(list, fun, [])
}
test filter {
[]
|> filter(_, fn(x) { True })
|> Assert.equal(_, [])
[0, 4, 5, 7, 3]
|> filter(_, fn(x) { True })
|> Assert.equal(_, [0, 4, 5, 7, 3])
[0, 4, 5, 7, 3]
|> filter(_, fn(x) { x > 4 })
|> Assert.equal(_, [5, 7])
[0, 4, 5, 7, 3]
|> filter(_, fn(x) { x < 4 })
|> Assert.equal(_, [0, 3])
}
fn do_filter(list, fun, acc) {
case list {
| [] => reverse(acc)
| x :: xs =>
new_acc =
case fun(x) {
| True => x :: acc
| False => acc
}
do_filter(xs, fun, new_acc)
}
}
pub fn map(list, fun) {
do_map(list, fun, [])
}
test map {
[]
|> map(_, fn(x) { x * 2 })
|> Assert.equal(_, [])
[0, 4, 5, 7, 3]
|> map(_, fn(x) { x * 2 })
|> Assert.equal(_, [0, 8, 10, 14, 6])
}
fn do_map(list, fun, acc) {
case list {
| [] => reverse(acc)
| x :: xs => do_map(xs, fun, fun(x) :: acc)
}
}
pub fn drop(list, n) {
case n <= 0 {
| True => list
| False =>
case list {
| [] => []
| _ :: xs => drop(xs, n - 1)
}
}
}
test drop/2 {
[]
|> drop(_, 5)
|> Assert.equal(_, [])
[1, 2, 3, 4, 5, 6, 7, 8]
|> drop(_, 5)
|> Assert.equal(_, [6, 7, 8])
}
pub fn take(list, n) {
do_take(list, n, [])
}
fn do_take(list, n, acc) {
case n <= 0 {
| True => reverse(acc)
| False =>
case list {
| [] => reverse(acc)
| x :: xs => take(xs, n - 1, x :: acc)
}
}
}
test take {
[]
|> take(_, 5)
|> Assert.equal(_, [])
[1, 2, 3, 4, 5, 6, 7, 8]
|> take(_, 5)
|> Assert.equal(_, [1, 2, 3, 4, 5])
}
pub fn of(x) {
[x]
}
test of() {
of([]) |> Assert.equal(_, [[]])
of(1) |> Assert.equal(_, [1])
}
pub fn new() {
[]
}
test new() {
new() |> Assert.equal(_, [])
}
pub fn flatten(lists) {
do_flatten(lists, [])
}
test flatten() {
flatten([]) |> Assert.equal(_, [])
flatten([[]]) |> Assert.equal(_, [])
flatten([[], [], []]) |> Assert.equal(_, [])
flatten([[1, 2], [], [3, 4]]) |> Assert.equal(_, [1, 2, 3, 4])
}
fn do_flatten(lists, acc) {
case lists {
| [] => acc
| l :: rest => flatten(rest, acc ++ l)
}
}
pub fn foldl(list, acc, fun) {
case list {
| [] => acc
| x :: rest => foldl(rest, fun(x, acc), fun)
}
}
test foldl() {
[1, 2, 3]
|> foldl(_, [], fn(x, acc) { x :: acc })
|> Assert.equal(_, [3, 2, 1])
}
pub fn foldr(list, acc, fun) {
case list {
| [] => acc
| x :: rest => fun(x, foldl(rest, acc, fun))
}
}
test foldr() {
[1, 2, 3]
|> foldr(_, [], |x, acc| x :: acc)
|> Assert.equal(_, [1, 2, 3])
}
|