tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit a3bdeaf28b0527317ce3dbf9ce5b8c95fa98782d
parent 8043c65788a09f6d2f4d907e138007ffb8d563c4
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Wed,  9 Oct 2019 23:57:55 -0700

Clean up list manipulation functions

Diffstat:
tibs/lib.tsp | 49+++++++++++++++++++++++++------------------------
1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/tibs/lib.tsp b/tibs/lib.tsp @@ -94,6 +94,12 @@ (cons (car lst) (apply list* (cdr lst))) (car lst))) +(define (length lst) + (recur f ((lst lst) (x 0)) + (if lst + (f (cdr lst) (+ x 1)) + x))) + (define (last lst) (if (cdr lst) (last (cdr lst)) @@ -105,11 +111,16 @@ (car lst) (nth (- i 1) (cdr lst))))) -(define (length lst) - (recur f ((lst lst) (x 0)) - (if lst - (f (cdr lst) (+ x 1)) - x))) +(define (apply proc args) + (eval (map (lambda (x) ; prevent args from being evaluated twice + (list 'quote x)) + (cons proc args)))) + +(define (map proc lst) + (if lst + (cons (proc (car lst)) + (map proc (cdr lst))) + nil)) (define (reverse l) (recur f ((in l) (out nil)) @@ -117,25 +128,10 @@ (f (cdr in) (cons (car in) out)) out))) -(define (apply fn args) - (eval (map (lambda (x) - (list 'quote x)) - (cons fn args)))) - -(define (map fn lst) - (if lst - (cons (fn (car lst)) - (map fn (cdr lst))) - nil)) - -(define (append l1 l2) - (cond ((nil? l1) l2) - (else (cons (car l1) (append (cdr l1) l2))))) - -(define (assoc key table) - (cond ((nil? table) nil) - ((= key (caar table)) (car table)) - (else (assoc key (cdr table))))) +(define (append x y) + (if x + (cons (car x) (append (cdr x) y)) + y)) (define (zip x y) (cond ((and (nil? x) (nil? y)) nil) @@ -143,6 +139,11 @@ (cons (cons (car x) (car y)) (zip (cdr x) (cdr y)))))) +(define (assoc key table) + (cond ((nil? table) nil) + ((= key (caar table)) (car table)) + (else (assoc key (cdr table))))) + (define (member? mem lst) (cond ((nil? lst) nil) ((= mem (car lst)) t)