From edceb3f93561dab1c688e3748bc55aa357361639 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 3 Aug 2019 14:22:30 -0400 Subject: net: Add (net ricketyspace sicp two thirty) --- net/ricketyspace/sicp/two/thirty.scm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 net/ricketyspace/sicp/two/thirty.scm (limited to 'net/ricketyspace') diff --git a/net/ricketyspace/sicp/two/thirty.scm b/net/ricketyspace/sicp/two/thirty.scm new file mode 100644 index 0000000..ffb22fa --- /dev/null +++ b/net/ricketyspace/sicp/two/thirty.scm @@ -0,0 +1,33 @@ +;;;; License: CC0-1.0 + +(define-module (net ricketyspace sicp two thirty) + #:export (square-tree + square-tree-steroids)) + +(define (square-tree tree) + (cond ((null? tree) '()) + ((not (pair? tree)) (* tree tree)) + (else (cons (square-tree (car tree)) + (square-tree (cdr tree)))))) + +(define (square-tree-steroids tree) + (map (lambda (sub-tree) + (if (not (pair? sub-tree)) + (* sub-tree sub-tree) + (square-tree-steroids sub-tree))) + tree)) + + +;;; Guile REPL +;;; +;;; scheme@(guile-user)> ,use (net ricketyspace sicp two thirty) +;;; scheme@(guile-user)> (square-tree +;;; (list 1 +;;; (list 2 (list 3 4) 5) +;;; (list 6 7))) +;;; $6 = (1 (4 (9 16) 25) (36 49)) +;;; scheme@(guile-user)> (square-tree-steroids +;;; (list 1 +;;; (list 2 (list 3 4) 5) +;;; (list 6 7))) +;;; $7 = (1 (4 (9 16) 25) (36 49)) -- cgit v1.2.3