-
Notifications
You must be signed in to change notification settings - Fork 6
/
library.lisp
61 lines (48 loc) · 1.48 KB
/
library.lisp
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 (abs x) (if (< x 0) (- 0 x) x))
(define (foldl proc init list)
(if list
(foldl proc
(proc init (car list))
(cdr list))
init))
(define (foldr proc init list)
(if list
(proc (car list)
(foldr proc init (cdr list)))
init))
(define (list . items)
(foldr cons nil items))
(define (reverse list)
(foldl (lambda (a x) (cons x a)) nil list))
(define (unary-map proc list)
(foldr (lambda (x rest) (cons (proc x) rest))
nil
list))
(define (map proc . arg-lists)
(if (car arg-lists)
(cons (apply proc (unary-map car arg-lists))
(apply map (cons proc
(unary-map cdr arg-lists))))
nil))
(define (append a b) (foldr cons b a))
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(defmacro (and a b) (list 'if a b nil))
(defmacro (quasiquote x)
(if (pair? x)
(if (eq? (car x) 'unquote)
(cadr x)
(if (and (pair? (car x)) (eq? (caar x) 'unquote-splicing))
(list 'append
(cadr (car x))
(list 'quasiquote (cdr x)))
(list 'cons
(list 'quasiquote (car x))
(list 'quasiquote (cdr x)))))
(list 'quote x)))
(defmacro (let defs . body)
`((lambda ,(map car defs) ,@body)
,@(map cadr defs)))
(define +
(let ((old+ +))
(lambda xs (foldl old+ 0 xs))))