Fixed if form when no alternative.
[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 ; Reverse the contents of a list
25 (define (reverse l)
26   (if (null? l)
27     ()
28     (append (reverse (cdr l)) (list (car l)))))
29
30 ;; LIBRARY FORMS
31
32 (define-macro (while condition . body)
33               `(begin
34                  (define (loop)
35                    (if ,condition
36                      (begin ,@body (loop))))
37                  (loop)))
38
39 ;; TESTING
40
41 (define-macro (backwards . body)
42               (cons 'begin (reverse body)))
43
44 (define method '(while (> counter 0)
45                        (display counter) (newline)
46                        (set! counter (- counter 1))))
47
48 (define (count)
49   (define counter 10)
50   (while (> counter 0)
51          (display counter) (newline)
52          (set! counter (- counter 1))))
53
54 ; Basic iterative summation.  Run this on large numbers to
55 ; test garbage collection and tail-call optimization.
56 (define (sum n)
57
58   (define (sum-iter total count maxcount)
59     (if (> count maxcount)
60       total
61       (sum-iter (+ total count) (+ count 1) maxcount)))
62   
63   (sum-iter 0 1 n))
64
65 ; Recursive summation. Use this to compare with tail call
66 ; optimized iterative algorithm.
67 (define (sum-recurse n)
68   (if (= n 0)
69     0
70     (+ n (sum-recurse (- n 1)))))