Skip to content

Commit 51547cb

Browse files
committed
Examples now load library.lisp
1 parent d64c074 commit 51547cb

3 files changed

Lines changed: 5 additions & 203 deletions

File tree

examples/hanoi.lisp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defun list (x . y) (cons x y))
1+
(load "examples/library.lisp")
22

33
(defun hanoi-print (disk from to)
44
(println (string-concat "Move disk " disk

examples/life.lisp

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,7 @@
22
;;; Conway's game of life
33
;;;
44

5-
;; (progn expr ...)
6-
;; => ((lambda () expr ...))
7-
;(defmacro progn (expr . rest)
8-
; (list (cons 'lambda (cons () (cons expr rest)))))
9-
10-
(defun list (x . y)
11-
(cons x y))
12-
13-
14-
;(defun not (x)
15-
; (if x () t))
16-
17-
;; (let var val body ...)
18-
;; => ((lambda (var) body ...) val)
19-
(defmacro let (var val . body)
20-
(cons (cons 'lambda (cons (list var) body))
21-
(list val)))
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-
;;; Numeric operators
59-
;;;
60-
61-
;(defun <= (e1 e2)
62-
; (or (< e1 e2)
63-
; (= e1 e2)))
64-
65-
;;;
66-
;;; List operators
67-
;;;
68-
69-
;;; Applies each element of lis to fn, and returns their return values as a list.
70-
(defun map (lis fn)
71-
(when lis
72-
(cons (fn (car lis))
73-
(map (cdr lis) fn))))
74-
75-
;; Returns nth element of lis.
76-
(defun nth (lis n)
77-
(if (= n 0)
78-
(car lis)
79-
(nth (cdr lis) (- n 1))))
80-
81-
;; Returns the nth tail of lis.
82-
(defun nth-tail (lis n)
83-
(if (= n 0)
84-
lis
85-
(nth-tail (cdr lis) (- n 1))))
86-
87-
;; Returns a list consists of m .. n-1 integers.
88-
(defun %iota (m n)
89-
(unless (<= n m)
90-
(cons m (%iota (+ m 1) n))))
91-
92-
;; Returns a list consists of 0 ... n-1 integers.
93-
(defun iota (n)
94-
(%iota 0 n))
95-
96-
;;;
97-
;;; Main
98-
;;;
5+
(load "examples/library.lisp")
996

1007
(define width 10)
1018
(define height 10)

examples/nqueens.lisp

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -15,112 +15,7 @@
1515
;;; expanded form using cons and list.
1616
;;;
1717

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")
12419

12520
;;;
12621
;;; N-queens solver
@@ -164,11 +59,11 @@
16459
;; Check if there's no conflicting queen upward
16560
(set? board n y)
16661
;; Upper left
167-
(let1 z (+ y (- n x))
62+
(let z (+ y (- n x))
16863
(and (<= 0 z)
16964
(set? board n z)))
17065
;; Upper right
171-
(let1 z (+ y (- x n))
66+
(let z (+ y (- x n))
17267
(and (< z board-size)
17368
(set? board n z)))))))
17469

0 commit comments

Comments
 (0)