1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; Standard Library Procedures and Macros ;;
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 (define (caar l) (car (car l)))
11 (define (cadr l) (car (cdr l)))
12 (define (cdar l) (cdr (car l)))
13 (define (cddr l) (cdr (cdr l)))
14 (define (cadar l) (car (cdr (car l))))
16 ; Join two lists together
20 (cons (car l1) (join (cdr l1) l2))))
22 ; Append an arbitrary number of lists together
23 (define (append . lists)
26 (if (null? (cdr lists))
28 (join (car lists) (apply append (cdr lists))))))
30 ; Reverse the contents of a list
34 (append (reverse (cdr l)) (list (car l)))))
41 (define (let-vars args)
44 (cons (caar args) (let-vars (cdr args)))))
46 (define (let-inits args)
49 (cons (cadar args) (let-inits (cdr args)))))
51 (define-macro (let args . body)
52 `((lambda ,(let-vars args)
53 ,@body) ,@(let-inits args)))
57 (define-macro (while condition . body)
58 (define loop (gensym))
62 (begin ,@body (,loop))))
67 (define-macro (backwards . body)
68 (cons 'begin (reverse body)))
70 ; Test for the while macro.
74 (display counter) (newline)
75 (set! counter (- counter 1))))
77 ; Basic iterative summation. Run this on large numbers to
78 ; test garbage collection and tail-call optimization.
81 (define (sum-iter total count maxcount)
82 (if (> count maxcount)
84 (sum-iter (+ total count) (+ count 1) maxcount)))
88 ; Recursive summation. Use this to compare with tail call
89 ; optimized iterative algorithm.
90 (define (sum-recurse n)
93 (+ n (sum-recurse (- n 1)))))