Added display primitives.
[scheme.forth.jl.git] / scheme-library.scm
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; Standard Library Procedures and Macros ;; 
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4
5 ;; LISTS
6
7 (define (null? args)
8   (eq? args ()))
9
10 ; Join two lists together
11 (define (join l1 l2)
12   (if (null? l1)
13     l2
14     (cons (car l1) (join (cdr l1) l2))))
15
16 ; Append an arbitrary number of lists together
17 (define (append . lists)
18   (if (null? lists)
19     ()
20     (if (null? (cdr lists))
21       (car lists)
22       (join (car lists) (apply append (cdr lists))))))
23
24
25 ;; LIBRARY FORMS
26 (define-macro (let value . body )
27               (list (list 'lambda (list (car value)) body)) (cdr value))
28
29 ;; TESTING
30
31 ; Basic iterative summation.  Run this on large numbers to
32 ; test garbage collection and tail-call optimization.
33 (define (sum n)
34
35   (define (sum-iter total count maxcount)
36     (if (> count maxcount)
37       total
38       (sum-iter (+ total count) (+ count 1) maxcount)))
39   
40   (sum-iter 0 1 n))
41
42 ; Recursive summation. Use this to compare with tail call
43 ; optimized iterative algorithm.
44 (define (sum-recurse n)
45   (if (= n 0)
46     0
47     (+ n (sum-recurse (- n 1)))))