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-macro (let args . body)
243 `((lambda ,(map (lambda (x) (car x)) args)
244 ,@body) ,@(map (lambda (x) (cadr x)) args)))
248 (define-macro (while condition . body)
249 (let ((loop (gensym)))
253 (begin ,@body (,loop))))
258 (define (cond-predicate clause) (car clause))
259 (define (cond-actions clause) (cdr clause))
260 (define (cond-else-clause? clause)
261 (eq? (cond-predicate clause) 'else))
263 (define (expand-clauses clauses)
264 (display "Expanding cond clauses...")
267 (let ((first (car clauses))
268 (rest (cdr clauses)))
269 (if (cond-else-clause? first)
271 `(begin ,@(cond-actions first))
272 (error "else clause isn't last in cond expression."))
273 `(if ,(cond-predicate first)
274 (begin ,@(cond-actions first))
275 ,(expand-clauses rest))))))
277 (define-macro (cond . clauses)
279 (error "cond requires at least one clause.")
280 (expand-clauses clauses)))
284 (define (expand-and-expressions expressions)
285 (let ((first (car expressions))
286 (rest (cdr expressions)))
290 ,(expand-and-expressions rest)
293 (define-macro (and . expressions)
294 (if (null? expressions)
296 (expand-and-expressions expressions)))
300 (define (expand-or-expressions expressions)
301 (if (null? expressions)
303 (let ((first (car expressions))
304 (rest (cdr expressions))
306 `(let ((,val ,first))
309 ,(expand-or-expressions rest))))))
311 (define-macro (or . expressions)
312 (expand-or-expressions expressions))
317 (define-macro (backwards . body)
318 (cons 'begin (reverse body)))
320 ; Test for the while macro.
324 (display counter) (newline)
325 (set! counter (- counter 1))))
327 ; Basic iterative summation. Run this on large numbers to
328 ; test garbage collection and tail-call optimization.
331 (define (sum-iter total count maxcount)
332 (if (fix:> count maxcount)
334 (sum-iter (fix:+ total count) (fix:+ count 1) maxcount)))
338 ; Recursive summation. Use this to compare with tail call
339 ; optimized iterative algorithm.
340 (define (sum-recurse n)
343 (fix:+ n (sum-recurse (fix:- n 1)))))
349 "This program is free software; you can redistribute it and/or modify
350 it under the terms of the GNU General Public License as published by
351 the Free Software Foundation; either version 3 of the License, or
352 (at your option) any later version.
354 This program is distributed in the hope that it will be useful,
355 but WITHOUT ANY WARRANTY; without even the implied warranty of
356 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
357 GNU General Public License for more details.
359 You should have received a copy of the GNU General Public License
360 along with this program. If not, see http://www.gnu.org/licenses/.
365 "Welcome to scheme.forth.jl!
367 Copyright (C) 2016 Tim Vaughan.
368 This program comes with ABSOLUTELY NO WARRANTY; for details type '(license)'.