-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstdlib.l
166 lines (135 loc) · 4.29 KB
/
stdlib.l
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
; Copyright (c) 2013 Simon Howard
;
; Permission to use, copy, modify, and/or distribute this software
; for any purpose with or without fee is hereby granted, provided
; that the above copyright notice and this permission notice appear
; in all copies.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
; LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
; Yoctolisp standard library functions.
; This file is loaded on startup.
(define (list . values) values)
(define (null? x) (builtin-eq x '()))
(define (map callback list)
(if (null? list)
'()
(cons (callback (car list))
(map callback (cdr list)))))
(define (filter callback list)
(if (null? list)
'()
(if (callback (car list))
(cons (car list)
(filter callback (cdr list)))
(filter callback (cdr list)))))
(define (for-each callback list)
(if (null? list)
'()
(begin (callback (car list))
(for-each callback (cdr list)))))
(define (foldl func acc list)
(if (null? list)
acc
(foldl func
(func acc (car list))
(cdr list))))
(define (foldr func acc list)
(if (null? list)
acc
(func (car list)
(foldr func acc (cdr list)))))
(define (reduce func list)
(foldl func (car list) (cdr list)))
(define (apply func args)
(eval
(cons func
(map (lambda (x) (list 'quote x))
args))))
(define (curry func x)
(lambda args (apply func (cons x args))))
(define (compose f g)
(lambda args (f (apply g args))))
(define (primitive-append a b)
(if (null? a)
b
(cons (car a)
(append (cdr a) b))))
(define (append . args)
(foldr primitive-append '() args))
(define (length list)
(foldl (lambda (x) (+ x 1)) 0 list))
(define (sum . list) (foldl + 0 list))
(define (product . list) (foldl * 1 list))
(define (sort l)
(if (null? l)
l
(let ((pivot (car l))
(left (filter (lambda (x) (< x pivot)) (cdr l)))
(right (filter (lambda (x) (>= x pivot)) (cdr l))))
(append (sort left)
(list pivot)
(sort right)))))
(define (reverse-with-ending list ending)
(if (null? list)
ending
(reverse-with-ending
(cdr list)
(cons (car list) ending))))
(define (reverse list)
(reverse-with-ending list '()))
(define (min first . list)
(foldl (lambda (x y) (if (< x y) x y))
first
list))
(define (max first . list)
(foldl (lambda (x y) (if (> x y) x y))
first
list))
(define (+ . args) (reduce builtin-add args))
(define (- . args)
(if (null? (cdr args)) ; (- x) means (- 0 x)
(- 0 (car args))
(reduce builtin-sub args)))
(define (* . args) (reduce builtin-mul args))
(define (/ . args) (reduce builtin-div args))
(define (and . args) (reduce builtin-and args))
(define (or . args) (reduce builtin-or args))
(define (pairs l)
(if (null? (cdr l))
'()
(cons (list (car l) (cadr l))
(pairs (cdr l)))))
(define (pairs-operator func)
(lambda args
(reduce builtin-and
(map (lambda (pair) (apply func pair))
(pairs args)))))
; Build up everything from builtin-eq and builtin-lt:
(define (basic-gt x y) (builtin-lt y x))
(define (basic-lte x y) (not (basic-gt x y)))
(define (basic-gte x y) (not (builtin-lt x y)))
(define = (pairs-operator builtin-eq))
(define < (pairs-operator builtin-lt))
(define > (pairs-operator basic-gt))
(define <= (pairs-operator basic-lte))
(define >= (pairs-operator basic-gte))
(define (id x) x)
(define (cadr x) (car (cdr x)))
(define (caar x) (car (car x)))
(define (cddr x) (cdr (cdr x)))
(define (cdar x) (cdr (car x)))
(define (caaar x) (car (car (car x))))
(define (caadr x) (car (car (cdr x))))
(define (cadar x) (car (cdr (car x))))
(define (caddr x) (car (cdr (cdr x))))
(define (cdaar x) (cdr (car (car x))))
(define (cdadr x) (cdr (car (cdr x))))
(define (cddar x) (cdr (cdr (car x))))
(define (cdddr x) (cdr (cdr (cdr x))))