3 ;; define (procedural syntax)
5 (define-macro (define args . body)
7 `(define ,(car args) (lambda ,(cdr args) ,@body))
12 (define-macro (begin . sequence)
13 `((lambda () ,@sequence)))
17 (define-macro (caar l) `(car (car ,l)))
18 (define-macro (cadr l) `(car (cdr ,l)))
19 (define-macro (cdar l) `(cdr (car ,l)))
20 (define-macro (cddr l) `(cdr (cdr ,l)))
21 (define-macro (caaar l) `(car (car (car ,l))))
22 (define-macro (caadr l) `(car (car (cdr ,l))))
23 (define-macro (cadar l) `(car (cdr (car ,l))))
24 (define-macro (caddr l) `(car (cdr (cdr ,l))))
25 (define-macro (cdaar l) `(cdr (car (car ,l))))
26 (define-macro (cdadr l) `(cdr (car (cdr ,l))))
27 (define-macro (cddar l) `(cdr (cdr (car ,l))))
28 (define-macro (cdddr l) `(cdr (cdr (cdr ,l))))
29 (define-macro (cadddr l) `(car (cdr (cdr (cdr ,l)))))
32 ;; Methods used in remaining macro definitions:
37 (cons (proc (car l)) (map proc (cdr l)))))
41 (define-macro (let args . body)
42 `((lambda ,(map (lambda (x) (car x)) args)
43 ,@body) ,@(map (lambda (x) (cadr x)) args)))
47 (define-macro (let* args . body)
51 (let* ,(cdr args) ,@body))))
55 (define-macro (while condition . body)
56 (let ((loop (gensym)))
60 (begin ,@body (,loop))))
66 (define (cond-predicate clause) (car clause))
67 (define (cond-actions clause) (cdr clause))
68 (define (cond-else-clause? clause)
69 (eq? (cond-predicate clause) 'else))
71 (define (expand-clauses clauses)
74 (let ((first (car clauses))
76 (if (cond-else-clause? first)
78 `(begin ,@(cond-actions first))
79 (error "else clause isn't last in cond expression."))
80 `(if ,(cond-predicate first)
81 (begin ,@(cond-actions first))
82 ,(expand-clauses rest))))))
84 (define-macro (cond . clauses)
86 (error "cond requires at least one clause.")
87 (expand-clauses clauses)))))
92 (define (expand-and-expressions expressions)
93 (let ((first (car expressions))
94 (rest (cdr expressions)))
98 ,(expand-and-expressions rest)
101 (define-macro (and . expressions)
102 (if (null? expressions)
104 (expand-and-expressions expressions)))
110 (define (expand-or-expressions expressions)
111 (if (null? expressions)
113 (let ((first (car expressions))
114 (rest (cdr expressions))
116 `(let ((,val ,first))
119 ,(expand-or-expressions rest))))))
121 (define-macro (or . expressions)
122 (expand-or-expressions expressions))
127 (define-macro (not x)
130 ;; FUNCTIONAL PROGRAMMING
132 (define (fold-left proc init l)
135 (fold-left proc (proc init (car l)) (cdr l))))
137 (define (reduce-left proc init l)
142 (fold-left proc (proc (car l) (car (cdr l))) (cdr (cdr l))))))
147 ; Rational primitives
149 (define (numerator x)
154 (define (denominator x)
162 (make-rational (fix:+ (fix:* (numerator x) (denominator y))
163 (fix:* (denominator x) (numerator y)))
164 (fix:* (denominator x) (denominator y))))
167 (make-rational (fix:- (fix:* (numerator x) (denominator y))
168 (fix:* (denominator x) (numerator y)))
169 (fix:* (denominator x) (denominator y))))
172 (make-rational (fix:* (numerator x) (numerator y))
173 (fix:* (denominator x) (denominator y))))
176 (make-rational (fix:* (numerator x) (denominator y))
177 (fix:* (denominator x) (numerator y))))
180 (make-rational (denominator x) (numerator x)))
182 ; Type dispatch and promotion
184 (define (type-dispatch ops x)
189 (define (promote-dispatch ops x y)
193 ((cdr ops) x (fixnum->flonum y)))
195 ((cdr ops) (fixnum->flonum x) y)
201 (type-dispatch (cons fix:neg flo:neg) x))
204 (type-dispatch (cons fix:abs flo:abs) x))
206 (define (flo:1+ x) (flo:+ x 1.0))
207 (define (flo:1- x) (flo:- x 1.0))
210 (type-dispatch (cons fix:1+ flo:1+) n))
213 (type-dispatch (cons fix:1- flo:1-) n))
215 (define (apply-to-flonum op x)
216 (if (flonum? x) (op x) x))
219 (apply-to-flonum flo:round x))
221 (apply-to-flonum flo:floor x))
223 (apply-to-flonum flo:ceiling x))
225 (apply-to-flonum flo:truncate x))
229 (define (fix:/ x y) ; Non-standard definition while we don't have rationals
230 (if (fix:= 0 (fix:remainder x y))
232 (flo:/ (fixnum->flonum x) (fixnum->flonum y))))
234 (define (pair+ x y) (promote-dispatch (cons fix:+ flo:+) x y))
235 (define (pair- x y) (promote-dispatch (cons fix:- flo:-) x y))
236 (define (pair* x y) (promote-dispatch (cons fix:* flo:*) x y))
237 (define (pair/ x y) (promote-dispatch (cons fix:/ flo:/) x y))
239 (define (pair> x y) (promote-dispatch (cons fix:> flo:>) x y))
240 (define (pair< x y) (promote-dispatch (cons fix:< flo:<) x y))
241 (define (pair>= x y) (promote-dispatch (cons fix:>= flo:>=) x y))
242 (define (pair<= x y) (promote-dispatch (cons fix:<= flo:<=) x y))
243 (define (pair= x y) (promote-dispatch (cons fix:= flo:=) x y))
249 (fold-left pair+ 0 args))
251 (define (- first . rest)
254 (pair- first (apply + rest))))
257 (fold-left pair* 1 args))
259 (define (/ first . rest)
262 (pair/ first (apply * rest))))
264 (define (quotient n1 n2)
265 (fix:quotient n1 n2))
267 (define (remainder n1 n2)
268 (fix:remainder n1 n2))
270 (define modulo remainder)
274 (define (test-relation rel l)
279 (if (rel (car l) (car (cdr l)))
280 (test-relation rel (cdr l))
284 (test-relation pair= args))
287 (test-relation pair> args))
290 (test-relation pair< args))
293 (test-relation pair>= args))
296 (test-relation pair<= args))
300 (define (zero? x) (pair= x 0.0))
301 (define (positive x) (pair> x 0.0))
302 (define (odd? n) (pair= (remainder n 2) 0))
303 (define (odd? n) (not (pair= (remainder n 2) 0)))
306 ; Current state of the numerical tower
307 (define (complex? x) #f)
308 (define (real? x) #t)
309 (define (rational? x) #t)
310 (define (integer? x) (= x (round x)))
311 (define (exact? x) (fixnum? x))
312 (define (inexact? x) (flonum? x))
316 (if (ratnum? x) #t #f))))
322 (define (list . args) args)
324 ; Return number of items in list
326 (define (iter a count)
329 (iter (cdr a) (fix:+ count 1))))
332 ; Join two lists together
336 (cons (car l1) (join (cdr l1) l2))))
338 ; Append an arbitrary number of lists together
339 (define (append . lists)
342 (if (null? (cdr lists))
344 (join (car lists) (apply append (cdr lists))))))
346 ; Reverse the contents of a list
350 (append (reverse (cdr l)) (list (car l)))))
355 ; Test for the while macro.
359 (display counter) (newline)
360 (set! counter (- counter 1))))
362 ; Basic iterative summation. Run this on large numbers to
363 ; test garbage collection and tail-call optimization.
366 (define (sum-iter total count maxcount)
367 (if (fix:> count maxcount)
369 (sum-iter (fix:+ total count) (fix:+ count 1) maxcount)))
373 ; Recursive summation. Use this to compare with tail call
374 ; optimized iterative algorithm.
375 (define (sum-recurse n)
378 (fix:+ n (sum-recurse (fix:- n 1)))))
386 "This program is free software; you can redistribute it and/or modify
387 it under the terms of the GNU General Public License as published by
388 the Free Software Foundation; either version 3 of the License, or
389 (at your option) any later version.
391 This program is distributed in the hope that it will be useful,
392 but WITHOUT ANY WARRANTY; without even the implied warranty of
393 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
394 GNU General Public License for more details.
396 You should have received a copy of the GNU General Public License
397 along with this program. If not, see http://www.gnu.org/licenses/.
402 "Welcome to scheme.forth.jl!
404 Copyright (C) 2016 Tim Vaughan.
405 This program comes with ABSOLUTELY NO WARRANTY; for details type '(license)'.