-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard.ilisp
218 lines (177 loc) · 4.07 KB
/
standard.ilisp
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
;; IdeLISP Standard library
;; Atoms
(def :nil 0)
(def :true 1)
(def :false 0)
;; Function
; Unpack list to function
(defn :unpack '(f items)
'(eval (join (list f) items)))
; Unapply list to function
(defn :pack '(f &rest items)
'(f items))
; Curry/uncurry aliases
(def :curry unpack)
(def :uncurry pack)
; Call fns in sequence
(defn :do '(&rest items)
'(if (== items nil)
'(nil)
'(last items)))
; Flip arguments
(defn :flip '(f x y)
'(f y x))
; Compose function
(defn :comp '(f g fs)
'(f (g fs)))
; Thread first macro
(defn :-> '(&rest args)
'(foldl
(fn '(acc curr)
'(curr acc))
(fst args)
(tail args)))
; Always return the same value
(defn :always '(x)
'(fn '(&rest _) '(x)))
; Flatten list elements
(defn :flatten '(values)
'(foldl
(fn '(acc curr)
'(join acc
(cond
'((list? curr) (flatten curr))
'(true (list curr)))))
'()
values))
;; List functions
; Retrive unwrapped first, second and third element in list
(defn :fst '(items)
'(eval (head items)))
(defn :snd '(items)
'(eval (head (tail items))))
(defn :trd '(items)
'(eval (head (tail (tail items)))))
; Retrive element at list index
(defn :nth '(n items)
'(if (== n 0)
'(fst items)
'(nth (- n 1) (tail items))))
; Retrive last element of list
(defn :last '(items)
'(nth (- (len items) 1) items))
; Retrive elements from list
(defn :take '(num items)
'(if (== num 0)
'('())
'(join
(head items)
(take
(- num 1)
(tail items)))))
; Drop elements from list
(defn :drop '(num items)
'(if (== num 0)
'(items)
'(drop (- num 1) (tail items))))
; Split list after num
(defn :split '(num items)
'(list (take num items) (drop num items)))
; Check if element is in list
(defn :elem '(needle items)
'(if (== items '())
'(false)
'(if (== needle (fst items))
'(true)
'(elem needle (tail items)))))
; Apply function on every element in list
(defn :map '(f items)
'(if (== items '())
'('())
'(join
(list (f (fst items)))
(map f (tail items)))))
; Filter list by predicate
(defn :filter '(p items)
'(if (== items '())
'('())
'(join
(if (p (fst items))
'(head items)
'('()))
(filter p (tail items)))))
; Fold value left to right
(defn :foldl '(f acc items)
'(if (== items '())
'(acc)
'(foldl f (f acc (fst items)) (tail items))))
; Combine two lists into pairs
(defn :zip '(x y)
'(if (or (not x) (not y))
'('())
'(join
(list
(join
(head x)
(head y)))
(zip
(tail x)
(tail y)))))
; Returns true if any of the elements match predicate
(defn :any '(f items)
'(if (== items '())
'(false)
'(if (f (fst items))
'(true)
'(any f (tail items)))))
; Returns true if all elements match predicate
(defn :all '(f items)
'(if (== items '())
'(true)
'(if (f (fst items))
'(any f (tail items))
'(false))))
; Returns true if list is empty
(defn :empty? '(items)
'(cond
'((== (len items) 0) false)
'(true true)))
(defn :list? '(x)
'(cond
'((== (type x) "Quoted Expression") true)
'(true false)))
;; Conditional
(defn :cond '(&rest cs)
'(if (not cs)
'(error "No cases found")
'(if (fst (fst cs))
'(snd (fst cs))
'(if (== (- (len cs) 1) 0)
'(error "No matching case found")
'(unpack cond (tail cs))))))
(defn :case '(needle &rest cs)
'(if (not cs)
'(error "No case found")
'(if (== needle (fst (fst cs)))
'(snd (fst cs))
'(if (== (- (len cs) 1) 0)
'(error "No matching case found")
'(unpack case (join (list needle) (tail cs)))))))
;; Numerical
; Get sum of args
(defn :sum '(items) '(foldl + 0 items))
; Get products of args
(defn :product '(items) '(foldl * 1 items))
; Increase int by one
(defn :inc '(x) '(+ x 1))
;; String
; Construct string from list
(defn :str-join '(values)
'(foldl concat "" values))
;; Misc
; Fibonacci
(defn :fib '(x)
'(cond
'((== x 0) 0)
'((== x 1) 1)
'(1 (+ (fib (- x 1)) (fib (- x 2))))))