Permalink
Browse files

minor improvement

  • Loading branch information...
1 parent cc66732 commit 6560ae9f2183a677cbcf6da107eea7803965edfa @sarabander committed Aug 11, 2011
Showing with 13 additions and 1 deletion.
  1. +13 −1 1.2/Ex1.11.scm
View
@@ -1,3 +1,4 @@
+
;; Recursive process
(define (f1 n)
(cond ((< n 3) n)
@@ -19,6 +20,17 @@
(- count 1)))))
(iter 0 1 2 n))
+;; The same, with slight change to avoid 2 unnecessary calculations of c
+(define (f2 n)
+ (define (iter a b c count) ; zeroth, first, second, counter
+ (cond ((< count 0) n)
+ ((= count 0) c)
+ (else (iter b
+ c
+ (+ c (* 2 b) (* 3 a))
+ (- count 1)))))
+ (iter 0 1 2 (- n 2)))
+
;; Tests
(f2 30)
@@ -28,4 +40,4 @@
;; Evaluates instantly
(f2 200)
-;; (f1 200) takes eternity
+;; (f1 200) takes eternity

0 comments on commit 6560ae9

Please sign in to comment.