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