-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandard.slisp
165 lines (133 loc) · 3.24 KB
/
Standard.slisp
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
; Standard Library
; Common operations and atoms
(def {nil} {})
(def {fun}
(\ {args body}
{def (head args) (\ (tail args) body)
}))
; Unpack a list of arguments for a function.
(fun {unpack f l} {
eval (join (list f) l)
})
; Pack a list of arguments for a function.
(fun {pack f & xs} {f xs})
(def {curry} unpack)
(def {uncurry} pack)
; Execute a sequence of operations
( fun {do & l} {
if (eq l nil)
{nil}
{last l}
})
; Let scope
(fun {let b} {
( (\ {_} b) () )
})
; flip function arguments
(fun { flip f a b } { f b a })
; Function composition
(fun { comp f g x } { f (g x) })
; Basic list accessors
(fun { fst l } { eval (head l) })
(fun { snd l } { eval (head (tail l)) })
; List Operations
; Nth item in list
(fun {nth n l} {
if (eq n 0)
{fst l}
{nth (- n 1) (tail l)}
} )
; List item in list
(fun {last l} {
nth (- (len l) 1) l
})
; Take N items from list
(fun {take n l} {
if (eq n 0)
{nil}
{join (head l) (take (- n 1) (tail l)) }
})
; Drop N items from list.
(fun {drop n l} {
if (eq n 0)
{l}
{drop (- n 1) (tail l)}
})
; Split at Nth position
(fun {split n l} {
list (take n l) (drop n l)
})
; Check if element is in the list
; BUG , since foldl uses x as arg, using x in elem causes a scope bug
; yy is used to avoid conflict.
; This can only be fixed with lexical (static) scoping. Current implementation is dynamic.
;; (fun {elem x l} { foldl (\ {acc i} { if (eq x i) {true} {acc} } ) false l})
(fun {elem x l} {
do
; wrap eq, cannot partially evaluate built-in functions for now.
(= {equal} (\ {x y} {eq x y}) )
; This must be done so X is binded to the correct argument before foldl.
; It will execute the S-expression and store it in isX.
(= {isX} (equal x) )
(foldl
(\ {acc i} {if (isX i) {true} {acc} })
false
l
)})
; Application
; Apply function to list elements.
(fun {map f l}{
if (eq l nil)
{nil}
{join (list (f (fst l))) (map f (tail l))}
})
; Filter list element.
(fun {filter f l} {
if (eq l nil)
{nil}
{join ( if ( f(fst l) ){head l} {nil} ) (filter f (tail l))}
})
; Accumulate value from operating on a list
(fun {foldl f x l} {
if (eq l nil)
{x} ; final accumulated result
{foldl f (f x (fst l)) (tail l)} ; traverse the rest of the list with new accumulated value
})
; Like foldl but returns intermediate values.
; (b -> a -> b) -> b -> [a] -> [b]
(fun {scanl f x l} {
if (eq l nil)
{list x}
{join (list x) (scanl f (f x (fst l)) (tail l)) };
})
(fun {sum l} {foldl + 0 l})
(fun {product l} {foldl * 1 l})
; Switch
(fun {select & expr} {
if (eq expr nil)
{error "Nothing to select"}
{if (fst(fst expr)) {snd (fst expr)} {unpack select (tail expr)}}
})
; Can be used with switches
(def {otherwise} true)
; Matching
(fun {case x & xs} {
if (eq xs nil)
{error "No case found"}
{ if (eq x (fst (fst xs)) ) { snd (fst cs) } {
unpack case (join (list x) (tail xs) )
} }
})
; Fibonacci sequence
(fun {fib n} {
select
{ (< n 0) (error "Fibonacci expects natural numbers") }
{ (eq n 0) 0 }
{ (eq n 1) 1 }
{ otherwise (+ (fib (- n 1) ) (fib (- n 2))) }
})
(fun {range a b} {
if (eq a b)
{list b}
{join (list a) (range (+ a 1) b)}
})