|
1 | | -(defun list (x . y) (cons x y)) |
2 | | - |
3 | | -;; (let1 var val body ...) |
4 | | -;; => ((lambda (var) body ...) val) |
5 | | -(defmacro let1 (var val . body) |
6 | | - (cons (cons 'lambda (cons (list var) body)) |
7 | | - (list val))) |
8 | | - |
9 | | -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
10 | | -;;; Control structures |
11 | | -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
12 | | - |
13 | | -(defmacro cond (rest) |
14 | | - (if (= () rest) |
15 | | - () |
16 | | - (if (= (car (car rest)) t) |
17 | | - (car (cdr (car rest))) |
18 | | - (list 'if |
19 | | - (car (car rest)) |
20 | | - (car (cdr (car rest))) |
21 | | - (cond (cdr rest)))))) |
22 | | - |
23 | | -;; (and e1 e2 ...) |
24 | | -;; => (if e1 (and e2 ...)) |
25 | | -;; (and e1) |
26 | | -;; => e1 |
27 | | -(defmacro and (expr . rest) |
28 | | - (if rest |
29 | | - (list 'if expr (cons 'and rest)) |
30 | | - expr)) |
31 | | - |
32 | | -;; (or e1 e2 ...) |
33 | | -;; => (let1 <tmp> e1 |
34 | | -;; (if <tmp> <tmp> (or e2 ...))) |
35 | | -;; (or e1) |
36 | | -;; => e1 |
37 | | -;; |
38 | | -;; The reason to use the temporary variables is to avoid evaluating the |
39 | | -;; arguments more than once. |
40 | | -(defmacro or (expr . rest) |
41 | | - (if rest |
42 | | - (let1 var (gensym) |
43 | | - (list 'let1 var expr |
44 | | - (list 'if var var (cons 'or rest)))) |
45 | | - expr)) |
46 | | - |
47 | | -;; (when expr body ...) |
48 | | -;; => (if expr (progn body ...)) |
49 | | -(defmacro when (expr . body) |
50 | | - (cons 'if (cons expr (list (cons 'progn body))))) |
51 | | - |
52 | | -;; (unless expr body ...) |
53 | | -;; => (if expr () body ...) |
54 | | -(defmacro unless (expr . body) |
55 | | - (cons 'if (cons expr (cons () body)))) |
56 | | - |
57 | | - |
58 | | - |
59 | | -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
60 | | -;;; List operators |
61 | | -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
62 | | - |
63 | | -;; Applies each element of lis to pred. If pred returns a true value, terminate |
64 | | -;; the evaluation and returns pred's return value. If all of them return (), |
65 | | -;; returns (). |
66 | | -(defun any (lis pred) |
67 | | - (when lis |
68 | | - (or (pred (car lis)) |
69 | | - (any (cdr lis) pred)))) |
70 | | - |
71 | | -;;; Applies each element of lis to fn, and returns their return values as a list. |
72 | | -(defun map (lis fn) |
73 | | - (when lis |
74 | | - (cons (fn (car lis)) |
75 | | - (map (cdr lis) fn)))) |
76 | | - |
77 | | -;; Returns nth element of lis. |
78 | | -(defun nth (lis n) |
79 | | - (if (= n 0) |
80 | | - (car lis) |
81 | | - (nth (cdr lis) (- n 1)))) |
82 | | - |
83 | | -;; Returns the nth tail of lis. |
84 | | -(defun nth-tail (lis n) |
85 | | - (if (= n 0) |
86 | | - lis |
87 | | - (nth-tail (cdr lis) (- n 1)))) |
88 | | - |
89 | | -;; Returns a list consists of m .. n-1 integers. |
90 | | -(defun %iota (m n) |
91 | | - (unless (<= n m) |
92 | | - (cons m (%iota (+ m 1) n)))) |
93 | | - |
94 | | -;; Returns a list consists of 0 ... n-1 integers. |
95 | | -(defun iota (n) |
96 | | - (%iota 0 n)) |
97 | | - |
98 | | -;; Returns a new list whose length is len and all members are init. |
99 | | -(defun make-list (len init) |
100 | | - (unless (= len 0) |
101 | | - (cons init (make-list (- len 1) init)))) |
102 | | - |
103 | | -;; Applies fn to each element of lis. |
104 | | -(defun for-each (lis fn) |
105 | | - (or (not lis) |
106 | | - (progn (fn (car lis)) |
107 | | - (for-each (cdr lis) fn)))) |
108 | | - |
| 1 | +(defun list (x . y) (cons x y)) |
| 2 | + |
| 3 | +;; (let var val body ...) |
| 4 | +;; => ((lambda (var) body ...) val) |
| 5 | +(defmacro let (var val . body) |
| 6 | + (cons (cons 'lambda (cons (list var) body)) |
| 7 | + (list val))) |
| 8 | + |
| 9 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 10 | +;;; Control structures |
| 11 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 12 | + |
| 13 | +(defmacro cond (rest) |
| 14 | + (if (= () rest) |
| 15 | + () |
| 16 | + (if (= (car (car rest)) t) |
| 17 | + (car (cdr (car rest))) |
| 18 | + (list 'if |
| 19 | + (car (car rest)) |
| 20 | + (car (cdr (car rest))) |
| 21 | + (cond (cdr rest)))))) |
| 22 | + |
| 23 | +;; (and e1 e2 ...) |
| 24 | +;; => (if e1 (and e2 ...)) |
| 25 | +;; (and e1) |
| 26 | +;; => e1 |
| 27 | +(defmacro and (expr . rest) |
| 28 | + (if rest |
| 29 | + (list 'if expr (cons 'and rest)) |
| 30 | + expr)) |
| 31 | + |
| 32 | +;; (or e1 e2 ...) |
| 33 | +;; => (let <tmp> e1 |
| 34 | +;; (if <tmp> <tmp> (or e2 ...))) |
| 35 | +;; (or e1) |
| 36 | +;; => e1 |
| 37 | +;; |
| 38 | +;; The reason to use the temporary variables is to avoid evaluating the |
| 39 | +;; arguments more than once. |
| 40 | +(defmacro or (expr . rest) |
| 41 | + (if rest |
| 42 | + (let var (gensym) |
| 43 | + (list 'let var expr |
| 44 | + (list 'if var var (cons 'or rest)))) |
| 45 | + expr)) |
| 46 | + |
| 47 | +;; (when expr body ...) |
| 48 | +;; => (if expr (progn body ...)) |
| 49 | +(defmacro when (expr . body) |
| 50 | + (cons 'if (cons expr (list (cons 'progn body))))) |
| 51 | + |
| 52 | +;; (unless expr body ...) |
| 53 | +;; => (if expr () body ...) |
| 54 | +(defmacro unless (expr . body) |
| 55 | + (cons 'if (cons expr (cons () body)))) |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 60 | +;;; List operators |
| 61 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 62 | + |
| 63 | +;; Applies each element of lis to pred. If pred returns a true value, terminate |
| 64 | +;; the evaluation and returns pred's return value. If all of them return (), |
| 65 | +;; returns (). |
| 66 | +(defun any (lis pred) |
| 67 | + (when lis |
| 68 | + (or (pred (car lis)) |
| 69 | + (any (cdr lis) pred)))) |
| 70 | + |
| 71 | +;;; Applies each element of lis to fn, and returns their return values as a list. |
| 72 | +(defun map (lis fn) |
| 73 | + (when lis |
| 74 | + (cons (fn (car lis)) |
| 75 | + (map (cdr lis) fn)))) |
| 76 | + |
| 77 | +;; Returns nth element of lis. |
| 78 | +(defun nth (lis n) |
| 79 | + (if (= n 0) |
| 80 | + (car lis) |
| 81 | + (nth (cdr lis) (- n 1)))) |
| 82 | + |
| 83 | +;; Returns the nth tail of lis. |
| 84 | +(defun nth-tail (lis n) |
| 85 | + (if (= n 0) |
| 86 | + lis |
| 87 | + (nth-tail (cdr lis) (- n 1)))) |
| 88 | + |
| 89 | +;; Returns a list consists of m .. n-1 integers. |
| 90 | +(defun %iota (m n) |
| 91 | + (unless (<= n m) |
| 92 | + (cons m (%iota (+ m 1) n)))) |
| 93 | + |
| 94 | +;; Returns a list consists of 0 ... n-1 integers. |
| 95 | +(defun iota (n) |
| 96 | + (%iota 0 n)) |
| 97 | + |
| 98 | +;; Returns a new list whose length is len and all members are init. |
| 99 | +(defun make-list (len init) |
| 100 | + (unless (= len 0) |
| 101 | + (cons init (make-list (- len 1) init)))) |
| 102 | + |
| 103 | +;; Applies fn to each element of lis. |
| 104 | +(defun for-each (lis fn) |
| 105 | + (or (not lis) |
| 106 | + (progn (fn (car lis)) |
| 107 | + (for-each (cdr lis) fn)))) |
| 108 | + |
0 commit comments