|
15 | 15 | ;;; expanded form using cons and list. |
16 | 16 | ;;; |
17 | 17 |
|
18 | | -;; (progn expr ...) |
19 | | -;; => ((lambda () expr ...)) |
20 | | -;(defmacro progn (expr . rest) |
21 | | -; (list (cons 'lambda (cons () (cons expr rest))))) |
22 | | - |
23 | | -(defun list (x . y) (cons x y)) |
24 | | - |
25 | | -;(defun not (x) (if x () t)) |
26 | | - |
27 | | -;; (let1 var val body ...) |
28 | | -;; => ((lambda (var) body ...) val) |
29 | | -(defmacro let1 (var val . body) |
30 | | - (cons (cons 'lambda (cons (list var) body)) |
31 | | - (list val))) |
32 | | - |
33 | | -;; (and e1 e2 ...) |
34 | | -;; => (if e1 (and e2 ...)) |
35 | | -;; (and e1) |
36 | | -;; => e1 |
37 | | -(defmacro and (expr . rest) |
38 | | - (if rest |
39 | | - (list 'if expr (cons 'and rest)) |
40 | | - expr)) |
41 | | - |
42 | | -;; (or e1 e2 ...) |
43 | | -;; => (let1 <tmp> e1 |
44 | | -;; (if <tmp> <tmp> (or e2 ...))) |
45 | | -;; (or e1) |
46 | | -;; => e1 |
47 | | -;; |
48 | | -;; The reason to use the temporary variables is to avoid evaluating the |
49 | | -;; arguments more than once. |
50 | | -(defmacro or (expr . rest) |
51 | | - (if rest |
52 | | - (let1 var (gensym) |
53 | | - (list 'let1 var expr |
54 | | - (list 'if var var (cons 'or rest)))) |
55 | | - expr)) |
56 | | - |
57 | | -;; (when expr body ...) |
58 | | -;; => (if expr (progn body ...)) |
59 | | -(defmacro when (expr . body) |
60 | | - (cons 'if (cons expr (list (cons 'progn body))))) |
61 | | - |
62 | | -;; (unless expr body ...) |
63 | | -;; => (if expr () body ...) |
64 | | -(defmacro unless (expr . body) |
65 | | - (cons 'if (cons expr (cons () body)))) |
66 | | - |
67 | | -;;; |
68 | | -;;; Numeric operators |
69 | | -;;; |
70 | | - |
71 | | -;(defun <= (e1 e2) |
72 | | -; (or (< e1 e2) |
73 | | -; (= e1 e2))) |
74 | | - |
75 | | -;;; |
76 | | -;;; List operators |
77 | | -;;; |
78 | | - |
79 | | -;; Applies each element of lis to pred. If pred returns a true value, terminate |
80 | | -;; the evaluation and returns pred's return value. If all of them return (), |
81 | | -;; returns (). |
82 | | -(defun any (lis pred) |
83 | | - (when lis |
84 | | - (or (pred (car lis)) |
85 | | - (any (cdr lis) pred)))) |
86 | | - |
87 | | -;;; Applies each element of lis to fn, and returns their return values as a list. |
88 | | -(defun map (lis fn) |
89 | | - (when lis |
90 | | - (cons (fn (car lis)) |
91 | | - (map (cdr lis) fn)))) |
92 | | - |
93 | | -;; Returns nth element of lis. |
94 | | -(defun nth (lis n) |
95 | | - (if (= n 0) |
96 | | - (car lis) |
97 | | - (nth (cdr lis) (- n 1)))) |
98 | | - |
99 | | -;; Returns the nth tail of lis. |
100 | | -(defun nth-tail (lis n) |
101 | | - (if (= n 0) |
102 | | - lis |
103 | | - (nth-tail (cdr lis) (- n 1)))) |
104 | | - |
105 | | -;; Returns a list consists of m .. n-1 integers. |
106 | | -(defun %iota (m n) |
107 | | - (unless (<= n m) |
108 | | - (cons m (%iota (+ m 1) n)))) |
109 | | - |
110 | | -;; Returns a list consists of 0 ... n-1 integers. |
111 | | -(defun iota (n) |
112 | | - (%iota 0 n)) |
113 | | - |
114 | | -;; Returns a new list whose length is len and all members are init. |
115 | | -(defun make-list (len init) |
116 | | - (unless (= len 0) |
117 | | - (cons init (make-list (- len 1) init)))) |
118 | | - |
119 | | -;; Applies fn to each element of lis. |
120 | | -(defun for-each (lis fn) |
121 | | - (or (not lis) |
122 | | - (progn (fn (car lis)) |
123 | | - (for-each (cdr lis) fn)))) |
| 18 | +(load "examples/library.lisp") |
124 | 19 |
|
125 | 20 | ;;; |
126 | 21 | ;;; N-queens solver |
|
164 | 59 | ;; Check if there's no conflicting queen upward |
165 | 60 | (set? board n y) |
166 | 61 | ;; Upper left |
167 | | - (let1 z (+ y (- n x)) |
| 62 | + (let z (+ y (- n x)) |
168 | 63 | (and (<= 0 z) |
169 | 64 | (set? board n z))) |
170 | 65 | ;; Upper right |
171 | | - (let1 z (+ y (- x n)) |
| 66 | + (let z (+ y (- x n)) |
172 | 67 | (and (< z board-size) |
173 | 68 | (set? board n z))))))) |
174 | 69 |
|
|
0 commit comments