-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelude.lspy
176 lines (146 loc) · 2.9 KB
/
prelude.lspy
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
;------------------
; general functions
;------------------
; atoms
(def {nil} {})
(def {true} 1)
(def {false} 0)
; function definitions
(def {fun}
(\ {f body}
{def (head f) (\ (tail f) body)}))
; unpack list for function
(fun {unpack f xs}
{eval
(join (list f) xs)})
; pack list for function
(fun {pack f & xs} {f xs})
; alias to curry and uncurry
(def {curry} unpack)
(def {uncurry} pack)
; open new scope
(fun {let b}
{((\ {_} b) ())})
; logical functions
(fun {not x} {- 1 x})
(fun {or x y} {+ x y})
(fun {and x y} {* x y})
; misc
(fun {flip f a b} {f b a})
(fun {ghost & xs} {eval xs})
(fun {comp f g x} {f (g x)})
; --------------
; list functions
; --------------
; first, second, and third helpers
(fun {fst xs}
{(eval (head xs))})
(fun {snd xs}
{(eval (head (tail xs)))})
(fun {trd xs}
{(eval (head (tail (tail xs))))})
; length of a list
(fun {len xs}
{if (== xs nil)
{0}
{+ 1 (len (tail xs))}})
; Nth element of a list
(fun {nth n xs}
{if (== n 0)
{fst xs}
{nth (- n 1) (tail xs)}})
; last item in a list
(fun {last xs}
{nth (- (len xs) 1) xs})
; take the first N elements
(fun {take n xs}
{if (== n 0)
{nil}
{join (head xs)
(take (- n 1) (tail xs))}})
; drop the first N elements
(fun {drop n xs}
{if (== n 0)
{xs}
{drop (- n 1) (tail xs)}})
; split at N
(fun {split n xs}
{list (take n xs) (drop n xs)})
; element in list
(fun {elem x xs}
{if (== xs nil)
{false}
{if (== x (fst xs))
{true}
{elem x (tail xs)}}})
; map elements
(fun {map f xs}
{if (== xs nil)
{nil}
{join (list (f (fst xs)))
(map f (tail xs))}})
; filter elements
(fun {filter f xs}
{if (== xs nil)
{nil}
{join
(if (f (fst xs))
{head xs}
{nil})
(filter f (tail xs))}})
; fold left
(fun {foldl f z xs}
{if (== xs nil)
{z}
{foldl f (f z (fst xs)) (tail xs)}})
(fun {sum xs}
{foldl + 0 xs})
(fun {prod xs}
{foldl * 1 xs})
; --------------
; do function
; --------------
; perform several things in sequence
(fun {do & xs}
{if (== xs nil)
{nil}
{last xs}})
; --------------
; conditional functions
; --------------
; select function
; e.g.
; (fun {foo x}
; {select
; {(== x 0) "bar"}
; {(== x 1) "faz"}
; {(== x 2) "baz"}})
(fun {select & cs}
{if (== cs nil)
{error "No selection found"}
{if (fst (fst cs))
{snd (fst cs)}
{unpack select (tail cs)}}})
; default case
(def {otherwise} true)
; case statement turns select into a switch statement
; e.g.
; (fun {foo x} {
; case x
; {0 "bar"}
; {1 "faz"}
; {2 "baz"}})
(fun {case x & cs}
{if (== cs nil)
{error "No case found"}
{if (== x (fst (fst cs)))
{snd (fst cs)}
{unpack case (join (list x) (tail cs))}}})
; --------------
; Fibonacci
; --------------
(fun {fib n}
{select
{(== n 0) 0}
{(== n 1) 1}
{otherwise (+ (fib (- n 1)) (fib (- n 2)))}})