sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit c4e42b086215f617c7332cae31ffdaac882af407
parent 16eedb8d4db0a3c3cef797eaa52564d53a2f6af8
Author: rsiddharth <s@ricketyspace.net>
Date:   Fri, 29 Nov 2019 17:57:33 -0500

Add (net ricketyspace sicp two thirtyfive).

Diffstat:
net/ricketyspace/sicp/two/thirtyfive.scm | 32++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+), 0 deletions(-)

diff --git a/net/ricketyspace/sicp/two/thirtyfive.scm 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)> +