From 3fb108272f762a4e3afa3f2f925db03b3128c272 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 9 Feb 2024 06:25:11 -0800 Subject: New function: cons-find. * eval.c (cons_find): Static function removed; a new one is implemented in lib.c. (eval_init): Register cons-find intrinsic. * lib.c (cons_find_rec): New static function. (cons_find): New function. * lib.h (cons_find): Declared. * tests/012/cons.tl: New file. * txr.1: Documented cons-find together with tree-find. Document that tree-find's test-fun argument is optional, defaulting to equal. --- lib.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index e1ca5e9e..580ee468 100644 --- a/lib.c +++ b/lib.c @@ -3471,6 +3471,23 @@ val tree_find(val obj, val tree, val testfun) return nil; } +static val cons_find_rec(val obj, val tree, val testfun) +{ + uses_or2; + if (funcall2(testfun, obj, tree)) + return t; + else if (consp(tree)) + return or2(cons_find_rec(obj, us_car(tree), testfun), + cons_find_rec(obj, us_cdr(tree), testfun)); + else + return nil; +} + +val cons_find(val obj, val tree, val testfun) +{ + return cons_find_rec(obj, tree, default_arg(testfun, equal_f)); +} + val countqual(val obj, val seq) { val self = lit("countqual"); -- cgit v1.2.3