From c4e42b086215f617c7332cae31ffdaac882af407 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Fri, 29 Nov 2019 17:57:33 -0500 Subject: Add (net ricketyspace sicp two thirtyfive). --- net/ricketyspace/sicp/two/thirtyfive.scm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 net/ricketyspace/sicp/two/thirtyfive.scm (limited to 'net/ricketyspace') diff --git a/net/ricketyspace/sicp/two/thirtyfive.scm b/net/ricketyspace/sicp/two/thirtyfive.scm new file mode 100644 index 0000000..a7b4bbe --- /dev/null +++ b/net/ricketyspace/sicp/two/thirtyfive.scm @@ -0,0 +1,32 @@ +;;;; License: CC0-1.0 + +(define-module (net ricketyspace sicp two thirtyfive) + #:export (count-leaves-acc)) + + +(define (accumulate op initial sequence) + (if (null? sequence) + initial + (op (car sequence) + (accumulate op initial (cdr sequence))))) + + +(define (count-leaves-acc t) + (accumulate + 0 + (map (lambda (p) + (cond ((pair? p) + (count-leaves-acc p)) + (else 1))) + t))) + +;;; Guile REPL +;;; +;;; scheme@(guile-user)> ,use (net ricketyspace sicp two thirtyfive) +;;; scheme@(guile-user)> (count-leaves-acc '(1 3 (5 7) 9)) +;;; $11 = 5 +;;; scheme@(guile-user)> (count-leaves-acc '((7))) +;;; $12 = 1 +;;; scheme@(guile-user)> (count-leaves-acc '(1 (2 (3 (4 (5 (6 7))))))) +;;; $13 = 7 +;;; scheme@(guile-user)> + -- cgit v1.2.3