commit 0f4cf7164be8002ed2cea767c54d13737f55da83
parent 7c9bf31bcfecfc1ff89adfa2c9d681319c998a12
Author: Ed van Bruggen <edvb@uw.edu>
Date: Thu, 14 Nov 2019 16:43:20 -0800
Add count list function
Diffstat:
2 files changed, 10 insertions(+), 0 deletions(-)
diff --git a/test.c b/test.c
@@ -255,6 +255,11 @@ char *tests[][2] = {
{ "(nth (list 3 5/2 .332 -2) 2)", "0.332" },
{ "(nth '(a b c) 0)", "a" },
{ "(nth (list 'foo 'bar 'zar 'baz) 3)", "baz" },
+ { "(count 3 '(1 2 3 4))", "1" },
+ { "(count 1/2 (list 1/2 1/3 2/4 8 9.0))", "2" },
+ { "(count 'a '(b c a a f h a b c a))", "4" },
+ { "(count 3.2 nil)", "0" },
+ { "(count \"Bobandy\" '(1/2 1/4 \"Jim\"))", "0" },
{ "apply", NULL },
{ "(apply list '(1 2 3))", "(1 2 3)" },
diff --git a/tibs/lib.tsp b/tibs/lib.tsp
@@ -119,6 +119,11 @@
(car lst)
(nth (cdr lst) (- i 1)))))
+(define (count x lst)
+ (cond ((nil? lst) 0)
+ ((= x (car lst)) (+ 1 (count x (cdr lst))))
+ (else (count x (cdr lst)))))
+
(define (apply proc args)
(eval (map (lambda (x) ; prevent args from being evaluated twice
(list 'quote x))