tisp

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

commit e1e8900fbf0bbc6eb69c77760783fbb1f5eb8fe1
parent 3c8aae2ef20ea5559f04bb1d99dc666c6c75456e
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Thu, 17 Oct 2019 18:59:48 -0700

Add more general memp function

Rename member? to member, implement member with memp, have member return
list not boolean.

Diffstat:
test.c | 19++++++++++++-------
tibs/lib.tsp | 9++++++---
2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/test.c b/test.c @@ -290,13 +290,18 @@ char *tests[][2] = { { "(assoc 3 '((1 b)))", "()" }, { "(assoc 4/3 (list (list 1 pi) (list 4/3 1/2 3) (list 2 3)))", "(4/3 1/2 3)" }, - { "member?", NULL }, - { "(member? 'foo '(foo bar baz))", "t" }, - { "(member? 'bar '(foo bar baz))", "t" }, - { "(member? 4 '(12 38 4 8))", "t" }, - { "(member? 3.2 '(4/3 2 8 2 3.14 3.2))", "t" }, - { "(member? \"quux\" (list 4.2 3 'quux))", "()" }, - { "(member? 'qux '(foo bar baz))", "()" }, + { "member", NULL }, + { "(memp even? (list 1 3 19 4 7 8 2))", "(4 7 8 2)" }, + { "(memp negative? (list 1/3 pi 3.2e-9 0 4 -7 2))", "(-7 2)" }, + { "(memp (lambda (x) (> x 8)) '(1/3 1/2 5/3 8 9))", "(9)" }, + { "(memp (lambda (x) (= x \"fry\")) " + "'(\"fry\" \"nibbler\" \"prof\"))", "(\"fry\" \"nibbler\" \"prof\")" }, + { "(member 'foo '(foo bar baz))", "(foo bar baz)" }, + { "(member 'bar '(foo bar baz))", "(bar baz)" }, + { "(member 4 '(12 38 4 8))", "(4 8)" }, + { "(member 3.2 '(4/3 2 8 2 3.14 3.2))", "(3.2)" }, + { "(member \"quux\" (list 4.2 3 'quux))", "()" }, + { "(member 'qux '(foo bar baz))", "()" }, { "numbers", NULL }, diff --git a/tibs/lib.tsp b/tibs/lib.tsp @@ -144,10 +144,13 @@ ((= key (caar table)) (car table)) (else (assoc key (cdr table))))) -(define (member? mem lst) +(define (memp proc lst) (cond ((nil? lst) nil) - ((= mem (car lst)) t) - (else (member? mem (cdr lst))))) + ((proc (car lst)) lst) + (else (memp proc (cdr lst))))) + +(define (member mem lst) + (memp (lambda (x) (= mem x)) lst)) ;;; Math (define pi (* 4 (arctan 1.)))