-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.hl
38 lines (29 loc) · 1.42 KB
/
stdlib.hl
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
(defun map (f lst) (if (= (length lst) 0) [] (++ [(f (head lst))] (map f (tail lst)))))
(defun filter (f lst) (if (= (length lst) 0) [] (if (f (head lst)) (++ [(head lst)] (filter f (tail lst))) (filter f (tail lst)))))
(defun empty? (lst) (= (length lst) 0))
(defun foldr (f v lst) (if (empty? lst)
v
(f (head lst) (foldr f v (tail lst)))
))
(defun reverse (lst) (if (empty? lst)
[]
(++ (reverse (tail lst)) [(head lst)])))
(defun sum (lst) (if (empty? lst)
0
(+ (head lst) (sum (tail lst)))))
(defun member? (v lst) (if (empty? lst)
false
(if (= v (head lst))
true
(member? v (tail lst)))))
(defun common (lst1 lst2) (if (empty? lst1)
[]
(if (member? (head lst1) lst2)
(++ [(head lst1)] (common (tail lst1) lst2))
(union (tail lst1) lst2))))
(defun unique (lst) (if (empty? lst)
[]
(if (member? (head lst) (tail lst))
(unique (tail lst))
(++ [(head lst)] (unique (tail lst))))))
(defun curry (f arg) (lambda (arg2) (f arg arg2)))