-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch15_stdlib.lspy
172 lines (141 loc) · 2.93 KB
/
ch15_stdlib.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
; Atoms
(def {nil} {})
(def {true} 1)
(def {false} 0)
; Function definition
(def {fun} (\ {f b} {def (head f) (\ (tail f) b)}))
; Square a number
(fun {sqr x} {* x x})
; Modulo operation
(fun {mod x y} {- x (* (/ x y) y)})
; Test if number is odd/even
(fun {odd x} {mod x 2})
(fun {even x} {not (odd x)})
; Generate number range
(fun {range first last} {
if (== first last)
{(list last)}
{join (list first) (range (+ first 1) last)}
})
; Unpack list for function
(fun {unpack f l} {
eval (join (list f) l)
})
(def {curry} unpack)
; Pack list for function
(fun {pack f & xs} {f xs})
(def {uncurry} pack)
; Perform several things in order
(fun {do & l} {
if (== l nil)
{nil}
{last l}
})
; 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})
; Call 2 arg function with args reversed
(fun {flip f a b} {f b a})
; Compose two functions
(fun {comp f g x} {f (g x)})
; First, second or third item in a list
(fun {fst l} {eval (head l)})
(fun {snd l} {eval (head (tail l))})
(fun {trd l} {eval (head (tail (tail l)))})
; List length (shadows the builtin version)
(fun {len l} {foldl (\ {z i} {+ z 1}) 0 l})
; Nth item in the list
(fun {nth n l} {
if (== n 0)
{fst l}
{nth (- n 1) (tail l)}
})
; Last item in the list
(fun {last l} {nth (- (len l) 1) l})
; Take n items
(fun {take n l} {
if (== n 0)
{nil}
{join (head l) (take (- n 1) (tail l))}
})
; Drop n items
(fun {drop n l} {
if (== n 0)
{l}
{drop (- n 1) (tail l)}
})
; Split at n
(fun {split n l} {list (take n l) (drop n l)})
; Check if item is in list
(fun {elem x l} {foldl (\ {z i} {or z (== i x)}) false l})
; Apply function to list
(fun {map f l} {
if (== l nil)
{nil}
{join (list (f (fst l))) (map f (tail l))}
})
; Apply filter to list
(fun {filter f l} {
if (== l nil)
{nil}
{join (if (f (fst l)) {head l} {nil} ) (filter f (tail l))}
})
; Fold left
(fun {foldl f z l} {
if (== l nil)
{z}
{foldl f (f z (fst l)) (tail l)}
})
; Sum
(fun {sum l} {foldl + 0 l})
; Product
(fun {product l} {foldl * 1 l})
; Conditionally evaluate one of several options
(fun {select & cs} {
if (== cs nil)
{error "No selection found"}
{if (fst (fst cs)) {snd (fst cs)} {unpack select (tail cs)}}
})
(def {otherwise} true)
; Function to test select
(fun {month-day-suffix i} {
select
{(== i 1) "st"}
{(== i 2) "nd"}
{(== i 3) "rd"}
{otherwise "th"}
})
; Similar to select, but with simple comparison instead of eval of checks
(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))}
}
})
; Function to test case
(fun {day-name x} {
case x
{0 "Monday"}
{1 "Tuesday"}
{2 "Wednesday"}
{3 "Thursday"}
{4 "Friday"}
{5 "Saturday"}
{6 "Sunday"}
})
; Fibonacci
(fun {fib n} {
select
{(== n 0) 0}
{(== n 1) 1}
{otherwise (+ (fib (- n 1)) (fib (- n 2)))}
})
(print "end of stdlib")