summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2019-08-03 14:22:30 -0400
committerrsiddharth <s@ricketyspace.net>2019-08-03 14:22:30 -0400
commitedceb3f93561dab1c688e3748bc55aa357361639 (patch)
tree8124d72b937a19b71a468e41e3939f1056827433
parent533743ec841f9f8f0d56f1e345e1ceac6406f56a (diff)
net: Add (net ricketyspace sicp two thirty)
-rw-r--r--net/ricketyspace/sicp/two/thirty.scm33
1 files changed, 33 insertions, 0 deletions
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))