|
22 | 22 | ((null (cdr args)) (car args)) |
23 | 23 | (t `(aif ,(car args) (aand ,@(cdr args)))))) |
24 | 24 |
|
| 25 | +;; version in book |
| 26 | +#+nil |
| 27 | +(defmacro acond (&rest clauses) |
| 28 | + (if (null clauses) |
| 29 | + nil |
| 30 | + (let ((cl1 (car clauses)) |
| 31 | + (sym (gensym))) |
| 32 | + `(let ((,sym ,(car cl1))) |
| 33 | + (if ,sym |
| 34 | + (let ((it ,sym)) ,@(cdr cl1)) |
| 35 | + (acond ,@(cdr clauses))))))) |
25 | 36 |
|
26 | | -;; problem noted by pg but no fix given |
27 | | -;; http://www.paulgraham.com/onlisperrata.html: |
| 37 | +;; Corrected problem in http://www.paulgraham.com/onlisperrata.html: |
28 | 38 | ;; p. 191 (acond (3)) returns nil when it should return 3. |
29 | 39 | ;; Same problem with acond2, p. 198. Caught by Terrence Ireland. |
30 | 40 | (defmacro acond (&rest clauses) |
|
34 | 44 | (sym (gensym))) |
35 | 45 | `(let ((,sym ,(car cl1))) |
36 | 46 | (if ,sym |
37 | | - (let ((it ,sym)) |
38 | | - (declare (ignorable it)) ;; not in original code |
39 | | - ,@(cdr cl1)) |
| 47 | + ,(if (null (cdr cl1)) |
| 48 | + sym ;; return result of test if no non-test forms in clause |
| 49 | + `(let ((it ,sym)) |
| 50 | + (declare (ignorable it)) ;; not in original code |
| 51 | + ,@(cdr cl1))) |
40 | 52 | (acond ,@(cdr clauses))))))) |
41 | 53 |
|
42 | 54 | ;; p. 193 |
|
85 | 97 | (progn ,@body) |
86 | 98 | (setq ,flag nil)))))) |
87 | 99 |
|
88 | | -;; problem noted by pg but no fix given |
89 | | -;; http://www.paulgraham.com/onlisperrata.html: |
| 100 | +;; version in book |
| 101 | +#+nil |
| 102 | +(defmacro acond2 (&rest clauses) |
| 103 | + (if (null clauses) |
| 104 | + nil |
| 105 | + (let ((cl1 (car clauses)) |
| 106 | + (val (gensym)) |
| 107 | + (win (gensym))) |
| 108 | + `(multiple-value-bind (,val ,win) ,(car cl1) |
| 109 | + (if (or ,val ,win) |
| 110 | + (let ((it ,val)) ,@(cdr cl1)) |
| 111 | + (acond2 ,@(cdr clauses))))))) |
| 112 | + |
| 113 | +;; Corrected problem in http://www.paulgraham.com/onlisperrata.html: |
90 | 114 | ;; p. 191 (acond (3)) returns nil when it should return 3. |
91 | 115 | ;; Same problem with acond2, p. 198. Caught by Terrence Ireland. |
92 | 116 | (defmacro acond2 (&rest clauses) |
|
97 | 121 | (win (gensym))) |
98 | 122 | `(multiple-value-bind (,val ,win) ,(car cl1) |
99 | 123 | (if (or ,val ,win) |
100 | | - (let ((it ,val)) |
101 | | - (declare (ignorable it)) ;; not in original code |
102 | | - ,@(cdr cl1)) |
| 124 | + ,(if (null (cdr cl1)) |
| 125 | + val ;; return result of test if no non-test forms in clause |
| 126 | + `(let ((it ,val)) |
| 127 | + (declare (ignorable it)) ;; not in original code |
| 128 | + ,@(cdr cl1))) |
103 | 129 | (acond2 ,@(cdr clauses))))))) |
104 | 130 |
|
105 | 131 | ;; p. 199 |
|
0 commit comments