From 758a18092e28e95a6ec0cd7bd00b8948a9fabac3 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Sat, 18 Sep 2010 00:11:26 +0530 Subject: [PATCH] implementing some of the sec 2.2 examples in Racket --- src/sicp/{ch2_2.scm => ch2_2.rkt} | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) rename src/sicp/{ch2_2.scm => ch2_2.rkt} (70%) diff --git a/src/sicp/ch2_2.scm b/src/sicp/ch2_2.rkt similarity index 70% rename from src/sicp/ch2_2.scm rename to src/sicp/ch2_2.rkt index 8d03232..db58d8c 100644 --- a/src/sicp/ch2_2.scm +++ b/src/sicp/ch2_2.rkt @@ -2,6 +2,28 @@ (require (planet soegaard/sicp:2:1/sicp)) +;; count-leaves +(define (count-leaves x) + (cond + [(null? x) 0] + [(not (pair? x)) 1] + [else (+ (count-leaves (first x)) + (count-leaves (rest x)))])) + +;; scale tree +(define (scale-tree tree factor) + (cond [(null? tree) '()] + [(not (pair? tree)) (* tree factor)] + [else (cons (scale-tree (first tree) factor) + (scale-tree (rest tree) factor))])) + +(define (scale-tree-map tree factor) + (map (lambda (x) + (if (not (pair? x)) + (* x factor) + (scale-tree-map x factor))) + tree)) + (define (square-of-four tl tr bl br) (lambda (painter) (let ((top (beside (tl painter) (tr painter))) -- 2.45.2