Created
September 3, 2008 20:40
-
-
Save BaseCase/8659 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Exercise 1.11 - Given a function f, define two versions of | |
; a procedure that computes f, one which generates a | |
; tree recursive process, and one that generates an | |
; iterative process. | |
;recursive version: | |
(define (f n) | |
(if (< n 3) | |
n | |
(+ (f (- n 1)) | |
(* 2 (f (- n 2))) | |
(* 3 (f (- n 3)))))) | |
;iterative version: | |
(define (f n) | |
(define (f-comp a b c) | |
(+ a | |
(* 2 b) | |
(* 3 c))) | |
(define (f-iter count nm1 nm2 nm3) | |
(if (= 0 count) | |
(f-comp nm1 nm2 nm3) | |
(f-iter (dec count) (f-comp nm1 nm2 nm3) nm1 nm2))) | |
(if (< n 3) | |
n | |
(f-iter (- n 3) 2 1 0))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment