-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.lspy
173 lines (146 loc) · 3.32 KB
/
lib.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
(print "Loading Standard Lib...")
(def {fun} (\ {args body} {def (head args) (\ (tail args) body)}))
(fun {unpack f xs} {eval (join (list f) xs)})
(fun {pack f & xs} {f xs})
(fun {len l} {if (== l {}) {0} {+ 1 (len (tail l))}})
(fun {nth_elem n my_list} {if (== n 0) {head my_list} {nth_elem (- n 1) (tail(my_list)) }})
; Atoms
(def {nil} {})
(def {true} 1)
(def {false} 0)
; First, Second, or Third Item in List
(fun {fst l} { eval (head l) })
(fun {snd l} { eval (head (tail l)) })
(fun {trd l} { eval (head (tail (tail l))) })
; Perform Several things in Sequence
; Use case do (+ 20 10) (- 20 10)
(fun {do & l} {
if (== l nil)
{nil}
{last l}
})
; Open new scope
; not sure how this works, but opens a local context
; e.g. let {do (= {x} 100) (x)}
(fun {let b} {
((\ {_} b) ())
})
(fun {flip f a b} {f b a})
;Use case
;comp - (unpack *) {5 6}
(fun {comp f g x} {f (g x)})
; Last item in List
(fun {last l} {nth_elem (- (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)})
; Element of List
(fun {elem x l} {
if (== l nil)
{false}
{if (== x (eval (head l))) {true} {elem x (tail l)}}
})
; Apply Function to List
; basically a for-loop
(fun {map f l} {
if (== l nil)
{nil}
{join (list (f (eval (head l)))) (map f (tail l))}
})
;Add 1 element for comparison in a for loop
;Use case: filter (compare == 0) { 1 2 5 6 7}
(fun {compare f item_1 item_2} {
if (f item_1 item_2)
{true}
{false}
})
;Apply Filter to List
(fun {filter f l} {
if (== l nil)
{nil}
{join (if (f (eval (head l))) {head l} {nil}) (filter f (tail l))}
})
; Fold Left
(fun {foldl f z l} {
if (== l nil)
{z}
{foldl f (f z (eval (head l))) (tail l)}
})
;Sum and product of a list
(fun {sum l} {foldl + 0 l})
(fun {product l} {foldl * 1 l})
; Default Case
(def {otherwise} true)
; essentially a case statement
; usecase
; function {function_name input}
; select
; {(== input value1) "return1"}
; {(== input value2) "return2"}
; {otherwise "return_default"}
; })
(fun {select & cs} {
if (== cs nil)
{error "No Selection Found"}
{if (fst (fst cs)) {snd (fst cs)} {unpack select (tail cs)}}
})
(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))}}
})
;Example of case statement
(fun {day-name x} {
case x
{0 "Monday"}
{1 "Tuesday"}
{2 "Wednesday"}
{3 "Thursday"}
{4 "Friday"}
{5 "Saturday"}
{6 "Sunday"}
})
; Fibonacci sequence
(fun {fib n} {
select
{ (== n 0) 0 }
{ (== n 1) 1 }
{ otherwise (+ (fib (- n 1)) (fib (- n 2))) }
})
;Turn any number into 1
(
fun {or_one num value}
{+ num (or 1 value)}
)
; Getting the length of a list using foldl
(fun {len_foldl my_list}
{foldl or_one 0 my_list}
)
; Use map to check for equality
; {x_value, counter}
(
fun {check_eq my_list check_val}
{
if (== (fst my_list) check_val)
{join (head my_list) (list (+ 1 (snd my_list)))}
{my_list}
})
; Checking for existence using foldl
; a list in the form
; {original value we want to check, number of instances detected}
(
fun {elem_foldl x my_list}
{foldl check_eq x my_list}
)