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))))
16 (define (caddr l) (car (cdr (cdr l))))
17 (define (cadddr l) (car (cdr (cdr (cdr l)))))
19 ;; FUNCTIONAL PROGRAMMING
21 (define (fold-left proc init l)
24 (fold-left proc (proc init (car l)) (cdr l))))
26 (define (reduce-left proc init l)
31 (fold-left proc (proc (car l) (car (cdr l))) (cdr (cdr l))))))
36 (cons (proc (car l)) (map proc (cdr l)))))
47 (define (denominator x)
55 (make-rational (fix:+ (fix:* (numerator x) (denominator y))
56 (fix:* (denominator x) (numerator y)))
57 (fix:* (denominator x) (denominator y))))
60 (make-rational (fix:- (fix:* (numerator x) (denominator y))
61 (fix:* (denominator x) (numerator y)))
62 (fix:* (denominator x) (denominator y))))
65 (make-rational (fix:* (numerator x) (numerator y))
66 (fix:* (denominator x) (denominator y))))
69 (make-rational (fix:* (numerator x) (denominator y))
70 (fix:* (denominator x) (numerator y))))
73 (make-rational (denominator x) (numerator x)))
75 ; Type dispatch and promotion
77 (define (type-dispatch ops x)
82 (define (promote-dispatch ops x y)
86 ((cdr ops) x (fixnum->flonum y)))
88 ((cdr ops) (fixnum->flonum x) y)
94 (type-dispatch (cons fix:neg flo:neg) x))
97 (type-dispatch (cons fix:abs flo:abs) x))
99 (define (flo:1+ x) (flo:+ x 1.0))
100 (define (flo:1- x) (flo:- x 1.0))
103 (type-dispatch (cons fix:1+ flo:1+) n))
106 (type-dispatch (cons fix:1- flo:1-) n))
108 (define (apply-to-flonum op x)
109 (if (flonum? x) (op x) x))
112 (apply-to-flonum flo:round x))
114 (apply-to-flonum flo:floor x))
116 (apply-to-flonum flo:ceiling x))
118 (apply-to-flonum flo:truncate x))
122 (define (fix:/ x y) ; Non-standard definition while we don't have rationals
123 (if (fix:= 0 (fix:remainder x y))
125 (flo:/ (fixnum->flonum x) (fixnum->flonum y))))
127 (define (pair+ x y) (promote-dispatch (cons fix:+ flo:+) x y))
128 (define (pair- x y) (promote-dispatch (cons fix:- flo:-) x y))
129 (define (pair* x y) (promote-dispatch (cons fix:* flo:*) x y))
130 (define (pair/ x y) (promote-dispatch (cons fix:/ flo:/) x y))
132 (define (pair> x y) (promote-dispatch (cons fix:> flo:>) x y))
133 (define (pair< x y) (promote-dispatch (cons fix:< flo:<) x y))
134 (define (pair>= x y) (promote-dispatch (cons fix:>= flo:>=) x y))
135 (define (pair<= x y) (promote-dispatch (cons fix:<= flo:<=) x y))
136 (define (pair= x y) (promote-dispatch (cons fix:= flo:=) x y))
142 (fold-left pair+ 0 args))
144 (define (- first . rest)
147 (pair- first (apply + rest))))
150 (fold-left pair* 1 args))
152 (define (/ first . rest)
155 (pair/ first (apply * rest))))
157 (define (quotient n1 n2)
158 (fix:quotient n1 n2))
160 (define (remainder n1 n2)
161 (fix:remainder n1 n2))
163 (define modulo remainder)
167 (define (test-relation rel l)
172 (if (rel (car l) (car (cdr l)))
173 (test-relation rel (cdr l))
177 (test-relation pair= args))
180 (test-relation pair> args))
183 (test-relation pair< args))
186 (test-relation pair>= args))
189 (test-relation pair<= args))
193 (define (zero? x) (pair= x 0.0))
194 (define (positive x) (pair> x 0.0))
195 (define (odd? n) (pair= (remainder n 2) 0))
196 (define (odd? n) (not (pair= (remainder n 2) 0)))
199 ; Current state of the numerical tower
200 (define (complex? x) #f)
201 (define (real? x) #t)
202 (define (rational? x) #t)
203 (define (integer? x) (= x (round x)))
204 (define (exact? x) (fixnum? x))
205 (define (inexact? x) (flonum? x))
209 ; Return number of items in list
211 (define (iter a count)
214 (iter (cdr a) (fix:+ count 1))))
217 ; Join two lists together
221 (cons (car l1) (join (cdr l1) l2))))
223 ; Append an arbitrary number of lists together
224 (define (append . lists)
227 (if (null? (cdr lists))
229 (join (car lists) (apply append (cdr lists))))))
231 ; Reverse the contents of a list
235 (append (reverse (cdr l)) (list (car l)))))
238 ;; LIBRARY SPECIAL FORMS
242 (define (let-vars args)
245 (cons (caar args) (let-vars (cdr args)))))
247 (define (let-inits args)
250 (cons (cadar args) (let-inits (cdr args)))))
252 (define-macro (let args . body)
253 `((lambda ,(let-vars args)
254 ,@body) ,@(let-inits args)))
258 (define-macro (while condition . body)
259 (let ((loop (gensym)))
263 (begin ,@body (,loop))))
268 (define (cond-predicate clause) (car clause))
269 (define (cond-actions clause) (cdr clause))
270 (define (cond-else-clause? clause)
271 (eq? (cond-predicate clause) 'else))
273 (define (expand-clauses clauses)
274 (display "Expanding cond clauses...")
277 (let ((first (car clauses))
278 (rest (cdr clauses)))
279 (if (cond-else-clause? first)
281 `(begin ,@(cond-actions first))
282 (error "else clause isn't last in cond expression."))
283 `(if ,(cond-predicate first)
284 (begin ,@(cond-actions first))
285 ,(expand-clauses rest))))))
287 (define-macro (cond . clauses)
289 (error "cond requires at least one clause.")
290 (expand-clauses clauses)))
294 (define (expand-and-expressions expressions)
295 (let ((first (car expressions))
296 (rest (cdr expressions)))
300 ,(expand-and-expressions rest)
303 (define-macro (and . expressions)
304 (if (null? expressions)
306 (expand-and-expressions expressions)))
310 (define (expand-or-expressions expressions)
311 (if (null? expressions)
313 (let ((first (car expressions))
314 (rest (cdr expressions))
316 `(let ((,val ,first))
319 ,(expand-or-expressions rest))))))
321 (define-macro (or . expressions)
322 (expand-or-expressions expressions))
327 (define-macro (backwards . body)
328 (cons 'begin (reverse body)))
330 ; Test for the while macro.
334 (display counter) (newline)
335 (set! counter (- counter 1))))
337 ; Basic iterative summation. Run this on large numbers to
338 ; test garbage collection and tail-call optimization.
341 (define (sum-iter total count maxcount)
342 (if (fix:> count maxcount)
344 (sum-iter (fix:+ total count) (fix:+ count 1) maxcount)))
348 ; Recursive summation. Use this to compare with tail call
349 ; optimized iterative algorithm.
350 (define (sum-recurse n)
353 (fix:+ n (sum-recurse (fix:- n 1)))))
359 "This program is free software; you can redistribute it and/or modify
360 it under the terms of the GNU General Public License as published by
361 the Free Software Foundation; either version 3 of the License, or
362 (at your option) any later version.
364 This program is distributed in the hope that it will be useful,
365 but WITHOUT ANY WARRANTY; without even the implied warranty of
366 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
367 GNU General Public License for more details.
369 You should have received a copy of the GNU General Public License
370 along with this program. If not, see http://www.gnu.org/licenses/.
375 "Welcome to scheme.forth.jl!
377 Copyright (C) 2016 Tim Vaughan.
378 This program comes with ABSOLUTELY NO WARRANTY; for details type '(license)'.