From ab58598e62eb7ca718d2ee083c1c2c2ede4d6db3 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 9 Feb 2024 07:34:15 -0800 Subject: 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. --- lib.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib.c') 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); -- cgit v1.2.3