]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/utils.rkt
Solution to 4.33. This had been difficult to get right, though conceptually it was
[sicp.git] / src / sicp / utils.rkt
index b81f60128ce1f9e56f9a86be14773799bca960f7..f84c739ba80d5fbba7b81e81c282fd5462844055 100644 (file)
     a
     (gcd b (remainder a b))))
 
-(provide square)    
\ No newline at end of file
+;; naive fibonacci definition
+(define (fib n)
+  (cond 
+    ((or (= n 0) (= n 1)) 1)
+    (else (+ (fib (- n 1))
+             (fib (- n 2))))))
+
+(define (range low high (step 1))
+  (cond 
+    ((or (and (< low high)
+              (positive? step))
+         (and (> low high)
+          (negative? step)))
+     (cons low (range (+ low step) high step)))
+    (else '())))
+
+(define (accumulate op initial coll)
+  (if (empty? coll)
+      initial
+      (op (car coll)
+          (accumulate op initial (cdr coll)))))
+
+(provide square fib range accumulate)    
\ No newline at end of file