Skip to content

Commit

Permalink
Nice recursive process and ugly iterative process
Browse files Browse the repository at this point in the history
  • Loading branch information
dwinston committed Jun 21, 2014
1 parent 1017df2 commit 25b97bf
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ex1.11.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
;; recursive process
(define (f n)
(cond ((< n 3) n)
(else
(+
(f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3)))))))
(define f-recur f)

;; iterative process
;; uses an accumulator `accu` to know when done
(define (f n)
(define (f-iter n fn-1 fn-2 fn-3 accu)
(cond ((< n 3) n)
((= accu n)
(+ fn-1 (* 2 fn-2) (* 3 fn-3)))
(else
(f-iter n
(+ fn-1 (* 2 fn-2) (* 3 fn-3))
fn-1
fn-2
(+ 1 accu)))))
(f-iter n 2 1 0 3))
(define f-iter f)

(define (check-f-up-to n)
(cond ((< n 3) (= (f-recur n) (f-iter n)))
(else
(and (= (f-recur n) (f-iter n))
(check-f-up-to (- n 1))))))

(check-f-up-to 100)

;Value: #t

0 comments on commit 25b97bf

Please sign in to comment.