X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=testing.scm;fp=testing.scm;h=0b7d95b36f489914ffd7ecb431f86e94eab1d7e5;hb=7e44c5ca79a16106aefa315ccf38e74dcd365554;hp=0000000000000000000000000000000000000000;hpb=7bea53082737088667ae438fcdba69137c43674d;p=scheme.forth.jl.git diff --git a/testing.scm b/testing.scm new file mode 100644 index 0000000..0b7d95b --- /dev/null +++ b/testing.scm @@ -0,0 +1,40 @@ +;; Some simple procedures useful for implementation testing. + +; Basic iterative summation. Run this on large numbers to +; test garbage collection and tail-call optimization. +(define (sum n) + + (define (sum-iter total count maxcount) + (if (> count maxcount) + total + (sum-iter (+ total count) (+ count 1) maxcount))) + + (sum-iter 0 1 n)) + +; Recursive summation. Use this to compare with tail call +; optimized iterative algorithm. +(define (sum-recurse n) + (if (= n 0) + 0 + (+ n (sum-recurse (- n 1))))) + +(define (null? args) + (eq? args ())) + +; Join two lists together +(define (join l1 l2) + (if (null? l1) + l2 + (cons (car l1) (join (cdr l1) l2)))) + +; Append an arbitrary number of lists together +(define (append . lists) + (if (null? lists) + () + (if (null? (cdr lists)) + (car lists) + (join (car lists) (apply append (cdr lists)))))) + +; Macro definitions +(define-macro (let value . body ) + (list (list 'lambda (list (car value)) body)) (cdr value))