-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.l
61 lines (52 loc) · 1.54 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
(define cons (lambda (x y) (lambda (m) (m x y))))
(define car (lambda (z) (z (lambda (p q) p))))
(define cdr (lambda (z) (z (lambda (p q) q))))
(define listprint
(lambda (l)
(map (lambda (x) (print x)) l)))
(define identity (lambda (x) x))
(define positive? (lambda (x) (> x 0)))
(define negative? (lambda (x) (< x 0)))
(define even? (lambda (x) (= 0 (% x 2))))
(define odd? (lambda (x) (! (even? x))))
(define xor (lambda (x y) (and (or x y) (not (and x y)))))
(define nor (lambda (x y) (not (or x y))))
(define nand (lambda (x y) (not (and x y))))
(define >= (lambda (x y) (or (> x y) (= x y))))
(define <= (lambda (x y) (or (< x y) (= x y))))
(define repeat (lambda (token number)
(if (= number 0)
list
(cons
token
(repeat token (- number 1))))))
(define pow (lambda (b e) (reduce (lambda (a b) (* a b)) (repeat b e))))
(define range (lambda (a b) (if (>= a b) (list) (cons a (range (+ a 1) b)))))
(define do (lambda () (last ...)))
(define map
(lambda (f l)
(if (none? (car l))
None
(cons (f (car l)) (map f (cdr l))))))
(define foreach
(lambda (f l)
(do
(f (car l))
(if (none? (cdr l))
None
(foreach f (cdr l))))))
(define filter
(lambda (f l)
(if (none? (car l)) l
(if (f (car l))
(cons (car l) (filter f (cdr l)))
(filter f (cdr l))))))
(define reduce
(lambda (f l)
(if (none? (car l))
None
(if (none? (cdr l))
(car l)
(f
(car l)
(reduce f (cdr l)))))))