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))))
27 (define (denominator x)
35 (make-rational (fix:+ (fix:* (numerator x) (denominator y))
36 (fix:* (denominator x) (numerator y)))
37 (fix:* (denominator x) (denominator y))))
40 (make-rational (fix:- (fix:* (numerator x) (denominator y))
41 (fix:* (denominator x) (numerator y)))
42 (fix:* (denominator x) (denominator y))))
45 (make-rational (fix:* (numerator x) (numerator y))
46 (fix:* (denominator x) (denominator y))))
49 (make-rational (fix:* (numerator x) (denominator y))
50 (fix:* (denominator x) (numerator y))))
53 (make-rational (denominator x) (numerator x)))
55 ; Type dispatch and promotion
57 (define (type-dispatch ops x)
62 (define (promote-dispatch ops x y)
66 ((cdr ops) x (fixnum->flonum y)))
68 ((cdr ops) (fixnum->flonum x) y)
74 (type-dispatch (cons fix:neg flo:neg) x))
77 (type-dispatch (cons fix:abs flo:abs) x))
79 (define (flo:1+ x) (flo:+ x 1.0))
80 (define (flo:1- x) (flo:- x 1.0))
83 (type-dispatch (cons fix:1+ flo:1+) n))
86 (type-dispatch (cons fix:1- flo:1-) n))
88 (define (apply-to-flonum op x)
89 (if (flonum? x) (op x) x))
92 (apply-to-flonum flo:round x))
94 (apply-to-flonum flo:floor x))
96 (apply-to-flonum flo:ceiling x))
98 (apply-to-flonum flo:truncate x))
102 (define (fix:/ x y) ; Non-standard definition while we don't have rationals
103 (if (fix:= 0 (fix:remainder x y))
105 (flo:/ (fixnum->flonum x) (fixnum->flonum y))))
107 (define (pair+ x y) (promote-dispatch (cons fix:+ flo:+) x y))
108 (define (pair- x y) (promote-dispatch (cons fix:- flo:-) x y))
109 (define (pair* x y) (promote-dispatch (cons fix:* flo:*) x y))
110 (define (pair/ x y) (promote-dispatch (cons fix:/ flo:/) x y))
112 (define (pair> x y) (promote-dispatch (cons fix:> flo:>) x y))
113 (define (pair< x y) (promote-dispatch (cons fix:< flo:<) x y))
114 (define (pair>= x y) (promote-dispatch (cons fix:>= flo:>=) x y))
115 (define (pair<= x y) (promote-dispatch (cons fix:<= flo:<=) x y))
116 (define (pair= x y) (promote-dispatch (cons fix:= flo:=) x y))
121 (define (fold-left proc init l)
124 (fold-left proc (proc init (car l)) (cdr l))))
126 (define (reduce-left proc init l)
131 (fold-left proc (proc (car l) (car (cdr l))) (cdr (cdr l))))))
134 (fold-left pair+ 0 args))
136 (define (- first . rest)
139 (pair- first (apply + rest))))
142 (fold-left pair* 1 args))
144 (define (/ first . rest)
147 (pair/ first (apply * rest))))
149 (define (quotient n1 n2)
150 (fix:quotient n1 n2))
152 (define (remainder n1 n2)
153 (fix:remainder n1 n2))
155 (define modulo remainder)
159 (define (test-relation rel l)
164 (if (rel (car l) (car (cdr l)))
165 (test-relation rel (cdr l))
169 (test-relation pair= args))
172 (test-relation pair> args))
175 (test-relation pair< args))
178 (test-relation pair>= args))
181 (test-relation pair<= args))
185 (define (zero? x) (pair= x 0.0))
186 (define (positive x) (pair> x 0.0))
187 (define (odd? n) (pair= (remainder n 2) 0))
188 (define (odd? n) (not (pair= (remainder n 2) 0)))
191 ; Current state of the numerical tower
192 (define (complex? x) #f)
193 (define (real? x) #t)
194 (define (rational? x) #t)
195 (define (integer? x) (= x (round x)))
196 (define (exact? x) (fixnum? x))
197 (define (inexact? x) (flonum? x))
201 ; Return number of items in list
203 (define (iter a count)
206 (iter (cdr a) (+ count 1))))
209 ; Join two lists together
213 (cons (car l1) (join (cdr l1) l2))))
215 ; Append an arbitrary number of lists together
216 (define (append . lists)
219 (if (null? (cdr lists))
221 (join (car lists) (apply append (cdr lists))))))
223 ; Reverse the contents of a list
227 (append (reverse (cdr l)) (list (car l)))))
230 ;; LIBRARY SPECIAL FORMS
234 (define (let-vars args)
237 (cons (caar args) (let-vars (cdr args)))))
239 (define (let-inits args)
242 (cons (cadar args) (let-inits (cdr args)))))
244 (define-macro (let args . body)
245 `((lambda ,(let-vars args)
246 ,@body) ,@(let-inits args)))
250 (define-macro (while condition . body)
251 (let ((loop (gensym)))
255 (begin ,@body (,loop))))
260 (define (cond-predicate clause) (car clause))
261 (define (cond-actions clause) (cdr clause))
262 (define (cond-else-clause? clause)
263 (eq? (cond-predicate clause) 'else))
265 (define (expand-clauses clauses)
268 (let ((first (car clauses))
269 (rest (cdr clauses)))
270 (if (cond-else-clause? first)
272 `(begin ,@(cond-actions first))
273 (error "else clause isn't last in cond expression."))
274 `(if ,(cond-predicate first)
275 (begin ,@(cond-actions first))
276 ,(expand-clauses rest))))))
278 (define-macro (cond . clauses)
280 (error "cond requires at least one clause.")
281 (expand-clauses clauses)))
285 (define (expand-and-expressions expressions)
286 (let ((first (car expressions))
287 (rest (cdr expressions)))
291 ,(expand-and-expressions rest)
294 (define-macro (and . expressions)
295 (if (null? expressions)
297 (expand-and-expressions expressions)))
301 (define (expand-or-expressions expressions)
302 (if (null? expressions)
304 (let ((first (car expressions))
305 (rest (cdr expressions))
307 `(let ((,val ,first))
310 ,(expand-or-expressions rest))))))
312 (define-macro (or . expressions)
313 (expand-or-expressions expressions))
318 (define-macro (backwards . body)
319 (cons 'begin (reverse body)))
321 ; Test for the while macro.
325 (display counter) (newline)
326 (set! counter (- counter 1))))
328 ; Basic iterative summation. Run this on large numbers to
329 ; test garbage collection and tail-call optimization.
332 (define (sum-iter total count maxcount)
333 (if (> count maxcount)
335 (sum-iter (+ total count) (+ count 1) maxcount)))
339 ; Recursive summation. Use this to compare with tail call
340 ; optimized iterative algorithm.
341 (define (sum-recurse n)
344 (+ n (sum-recurse (- n 1)))))
350 "This program is free software; you can redistribute it and/or modify
351 it under the terms of the GNU General Public License as published by
352 the Free Software Foundation; either version 3 of the License, or
353 (at your option) any later version.
355 This program is distributed in the hope that it will be useful,
356 but WITHOUT ANY WARRANTY; without even the implied warranty of
357 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
358 GNU General Public License for more details.
360 You should have received a copy of the GNU General Public License
361 along with this program. If not, see http://www.gnu.org/licenses/.
366 "Welcome to scheme.forth.jl!
368 Copyright (C) 2016 Tim Vaughan.
369 This program comes with ABSOLUTELY NO WARRANTY; for details type '(license)'.