summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2019-11-29 17:57:33 -0500
committerrsiddharth <s@ricketyspace.net>2019-11-29 17:57:33 -0500
commitc4e42b086215f617c7332cae31ffdaac882af407 (patch)
treed11635807904bac2723685a3862823fabf8767e2
parent16eedb8d4db0a3c3cef797eaa52564d53a2f6af8 (diff)
Add (net ricketyspace sicp two thirtyfive).
-rw-r--r--net/ricketyspace/sicp/two/thirtyfive.scm32
1 files changed, 32 insertions, 0 deletions
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)>
+