summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-09 07:34:15 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-09 07:34:15 -0800
commitab58598e62eb7ca718d2ee083c1c2c2ede4d6db3 (patch)
tree245dd7aa545d26db0d62fefa61de13ff5310ed4c /lib.c
parent3fb108272f762a4e3afa3f2f925db03b3128c272 (diff)
downloadtxr-ab58598e62eb7ca718d2ee083c1c2c2ede4d6db3.tar.gz
txr-ab58598e62eb7ca718d2ee083c1c2c2ede4d6db3.tar.bz2
txr-ab58598e62eb7ca718d2ee083c1c2c2ede4d6db3.zip
New function: cons-count.
* eval.c (eval_init): Register cons-count intrinsic. * lib.c (cons_count_rec): New static function. (cons_count): New function. * lib.h (cons_count): Declared. * tests/012/cons.tl: New tests. * txr.1: Documented.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 580ee468..38028ea7 100644
--- a/lib.c
+++ b/lib.c
@@ -3596,6 +3596,25 @@ val count(val item, val seq, val testfun_in, val keyfun_in)
}
+static val cons_count_rec(val item, val tree, val testfun)
+{
+ val hc = if3(funcall2(testfun, item, tree), one, zero);
+
+ if (consp(tree)) {
+ val ac = cons_count_rec(item, us_car(tree), testfun);
+ val dc = cons_count_rec(item, us_cdr(tree), testfun);
+
+ return plus(plus(hc, ac), dc);
+ }
+
+ return hc;
+}
+
+val cons_count(val item, val tree, val testfun_in)
+{
+ return cons_count_rec(item, tree, default_arg(testfun_in, equal_f));
+}
+
val some_satisfy(val seq, val pred_in, val key_in)
{
val pred = default_arg(pred_in, identity_f);