This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.aes
More file actions
65 lines (56 loc) · 1.58 KB
/
Copy pathList.aes
File metadata and controls
65 lines (56 loc) · 1.58 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
namespace List =
function size(l : list('a)) : int = size'(l, 0)
private function size'(l : list('a), x : int) : int =
switch(l)
[] => x
_ :: l' => size'(l', x + 1)
function map(f : 'a => 'b, l : list('a)) : list('b) =
switch(l)
[] => []
e :: l' => f(e) :: map(f, l')
function foldr(f : (('a, 'b) => 'b), z: 'b, l : list('a)) : 'b =
switch(l)
[] => z
e :: l' => f(e, foldr(f, z, l'))
function foldl(f : (('b, 'a) => 'b), s: 'b, l : list('a)) : 'b =
switch(l)
[] => s
e :: l' => foldl(f, f(s, e), l')
function filter(f : ('a) => bool, l : list('a)) = filter'(f, l, [])
private function filter'(f : ('a) => bool, l : list('a), acc : list('a)) =
switch(l)
[] => acc
e :: l' =>
if(f(e))
filter'(f, l', e :: acc)
else
filter'(f, l', acc)
function find(l : list('a), f: 'a => bool) : option('a) =
switch(l)
[] => None
e :: l' =>
if(f(e))
Some(e)
else
find(l', f)
function sum(l : list('a), f : 'a => int) : int =
foldr((x, y) => x + y, 0, map(f, l))
function reverse(l) = reverse'(l, [])
private function reverse'(l, a) =
switch(l)
[] => a
(e :: l') => reverse'(l', e :: a)
function insert_by(f: (('a, 'a) => bool), x : 'a, l : list('a)) : list('a) =
switch(l)
[] => [x]
(e :: l') =>
if(f(x, e))
e :: insert_by(f, x, l')
else
x :: l
function foreach(f : 'a => 'b, l : list('a)) =
switch(l)
[] => []
e :: l' =>
f(e)
foreach(f, l')