1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; Standard Library Procedures and Macros ;;
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 (define (not x) (if x #f #t))
9 (define (list . args) args)
11 (define (caar l) (car (car l)))
12 (define (cadr l) (car (cdr l)))
13 (define (cdar l) (cdr (car l)))
14 (define (cddr l) (cdr (cdr l)))
15 (define (cadar l) (car (cdr (car l))))
20 ; Type dispatch and promotion
22 (define (type-dispatch ops x)
27 (define (promote-dispatch ops x y)
31 ((cdr ops) x (fixnum->flonum y)))
33 ((cdr ops) (fixnum->flonum x) y)
39 (type-dispatch (cons fix:neg flo:neg) x))
42 (type-dispatch (cons fix:abs flo:abs) x))
44 (define (flo:1+ x) (flo:+ x 1.0))
45 (define (flo:1- x) (flo:- x 1.0))
48 (type-dispatch (cons fix:1+ flo:1+) n))
51 (type-dispatch (cons fix:1- flo:1-) n))
53 (define (apply-to-flonum op x)
54 (if (flonum? x) (op x) x))
57 (apply-to-flonum flo:round x))
59 (apply-to-flonum flo:floor x))
61 (apply-to-flonum flo:ceiling x))
63 (apply-to-flonum flo:truncate x))
67 (define (fix:/ x y) ; Non-standard definition while we don't have rationals
68 (if (fix:= 0 (fix:remainder x y))
70 (flo:/ (fixnum->flonum x) (fixnum->flonum y))))
72 (define (pair+ x y) (promote-dispatch (cons fix:+ flo:+) x y))
73 (define (pair- x y) (promote-dispatch (cons fix:- flo:-) x y))
74 (define (pair* x y) (promote-dispatch (cons fix:* flo:*) x y))
75 (define (pair/ x y) (promote-dispatch (cons fix:/ flo:/) x y))
77 (define (pair> x y) (promote-dispatch (cons fix:> flo:>) x y))
78 (define (pair< x y) (promote-dispatch (cons fix:< flo:<) x y))
79 (define (pair>= x y) (promote-dispatch (cons fix:>= flo:>=) x y))
80 (define (pair<= x y) (promote-dispatch (cons fix:<= flo:<=) x y))
81 (define (pair= x y) (promote-dispatch (cons fix:= flo:=) x y))
86 (define (fold-left proc init l)
89 (fold-left proc (proc init (car l)) (cdr l))))
91 (define (reduce-left proc init l)
96 (fold-left proc (proc (car l) (car (cdr l))) (cdr (cdr l))))))
99 (fold-left pair+ 0 args))
101 (define (- first . rest)
104 (pair- first (apply + rest))))
107 (fold-left pair* 1 args))
109 (define (/ first . rest)
112 (pair/ first (apply * rest))))
114 (define (quotient n1 n2)
115 (fix:quotient n1 n2))
117 (define (remainder n1 n2)
118 (fix:remainder n1 n2))
120 (define modulo remainder)
124 (define (test-relation rel l)
129 (if (rel (car l) (car (cdr l)))
130 (test-relation rel (cdr l))
134 (test-relation pair= args))
137 (test-relation pair> args))
140 (test-relation pair< args))
143 (test-relation pair>= args))
146 (test-relation pair<= args))
150 (define (zero? x) (pair= x 0.0))
151 (define (positive x) (pair> x 0.0))
152 (define (odd? n) (pair= (remainder n 2) 0))
153 (define (odd? n) (not (pair= (remainder n 2) 0)))
156 ; Current state of the numerical tower
157 (define (complex? x) #f)
158 (define (real? x) #t)
159 (define (rational? x) #t)
160 (define (integer? x) (= x (round x)))
161 (define (exact? x) (fixnum? x))
162 (define (inexact? x) (flonum? x))
166 ; Return number of items in list
168 (define (iter a count)
171 (iter (cdr a) (+ count 1))))
174 ; Join two lists together
178 (cons (car l1) (join (cdr l1) l2))))
180 ; Append an arbitrary number of lists together
181 (define (append . lists)
184 (if (null? (cdr lists))
186 (join (car lists) (apply append (cdr lists))))))
188 ; Reverse the contents of a list
192 (append (reverse (cdr l)) (list (car l)))))
195 ;; LIBRARY SPECIAL FORMS
199 (define (let-vars args)
202 (cons (caar args) (let-vars (cdr args)))))
204 (define (let-inits args)
207 (cons (cadar args) (let-inits (cdr args)))))
209 (define-macro (let args . body)
210 `((lambda ,(let-vars args)
211 ,@body) ,@(let-inits args)))
215 (define-macro (while condition . body)
216 (let ((loop (gensym)))
220 (begin ,@body (,loop))))
225 (define (cond-predicate clause) (car clause))
226 (define (cond-actions clause) (cdr clause))
227 (define (cond-else-clause? clause)
228 (eq? (cond-predicate clause) 'else))
230 (define (expand-clauses clauses)
233 (let ((first (car clauses))
234 (rest (cdr clauses)))
235 (if (cond-else-clause? first)
237 `(begin ,@(cond-actions first))
238 (error "else clause isn't last in cond expression."))
239 `(if ,(cond-predicate first)
240 (begin ,@(cond-actions first))
241 ,(expand-clauses rest))))))
243 (define-macro (cond . clauses)
245 (error "cond requires at least one clause.")
246 (expand-clauses clauses)))
250 (define (expand-and-expressions expressions)
251 (let ((first (car expressions))
252 (rest (cdr expressions)))
256 ,(expand-and-expressions rest)
259 (define-macro (and . expressions)
260 (if (null? expressions)
262 (expand-and-expressions expressions)))
266 (define (expand-or-expressions expressions)
267 (if (null? expressions)
269 (let ((first (car expressions))
270 (rest (cdr expressions))
272 `(let ((,val ,first))
275 ,(expand-or-expressions rest))))))
277 (define-macro (or . expressions)
278 (expand-or-expressions expressions))
283 (define-macro (backwards . body)
284 (cons 'begin (reverse body)))
286 ; Test for the while macro.
290 (display counter) (newline)
291 (set! counter (- counter 1))))
293 ; Basic iterative summation. Run this on large numbers to
294 ; test garbage collection and tail-call optimization.
297 (define (sum-iter total count maxcount)
298 (if (> count maxcount)
300 (sum-iter (+ total count) (+ count 1) maxcount)))
304 ; Recursive summation. Use this to compare with tail call
305 ; optimized iterative algorithm.
306 (define (sum-recurse n)
309 (+ n (sum-recurse (- n 1)))))