X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=scheme-library.scm;h=9a9358c626c665f2ecd46c2c8fee9891e1000315;hb=f0a8c4a13b2c6d38fd8fe9e1dedb75997386a6e0;hp=207bb30062fe236189c9b49b402462b934af5255;hpb=480095585b96fbcc5a4fb58fb57188609aadb6e5;p=scheme.forth.jl.git diff --git a/scheme-library.scm b/scheme-library.scm index 207bb30..9a9358c 100644 --- a/scheme-library.scm +++ b/scheme-library.scm @@ -7,6 +7,12 @@ (define (null? args) (eq? args ())) +(define (caar l) (car (car l))) +(define (cadr l) (car (cdr l))) +(define (cdar l) (cdr (car l))) +(define (cddr l) (cdr (cdr l))) +(define (cadar l) (car (cdr (car l)))) + ; Join two lists together (define (join l1 l2) (if (null? l1) @@ -21,13 +27,56 @@ (car lists) (join (car lists) (apply append (cdr lists)))))) +; Reverse the contents of a list +(define (reverse l) + (if (null? l) + () + (append (reverse (cdr l)) (list (car l))))) + + +;; LIBRARY SPECIAL FORMS + +; let + +(define (let-vars args) + (if (null? args) + '() + (cons (caar args) (let-vars (cdr args))))) + +(define (let-inits args) + (if (null? args) + '() + (cons (cadar args) (let-inits (cdr args))))) + +(define-macro (let args . body) + `((lambda ,(let-vars args) + ,@body) ,@(let-inits args))) + +; while + +(define-macro (while condition . body) + (let ((loop (gensym))) + `(begin + (define (,loop) + (if ,condition + (begin ,@body (,loop)))) + (,loop)))) + +; cond -;; LIBRARY FORMS -(define-macro (let value . body ) - (list (list 'lambda (list (car value)) body)) (cdr value)) ;; TESTING +(define-macro (backwards . body) + (cons 'begin (reverse body))) + +; Test for the while macro. +(define (count) + (define counter 10) + (while (> counter 0) + (display counter) (newline) + (set! counter (- counter 1)))) + ; Basic iterative summation. Run this on large numbers to ; test garbage collection and tail-call optimization. (define (sum n)