notes03s - 1 CS61ANotes03Efficiency[Solutionsv1.0]

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS61A Notes 03 – Efficiency [Solutions v1.0] Recursive vs. Iterative Processes QUESTIONS: Will the following generate a recursive or iterative process? 1. (define (foo x) (* (- (+ (/ x 3) 4) 6) 2)) It’s not a recursive procedure, so it’s pretty pointless to ask what kind of process it generates 2. (define (foo x) (if (= x 0) 0 (+ x (foo (- x 1)))) Recursive 3. (define (helper1 x) (if (= x 0) 1 (helper1 (- x 1)))) <== Iterative! (define (helper2 x) (if (= x 0) 1 (+ 1 helper2 (- x 1)))) <== Recursive! a. (define (bar x) (if (even? x) (helper1 (- x 1)) (helper1 (- x 2)))) Iterative b. (define (bar x) (if (even? x) (helper2 (- x 1)) (helper2 (- x 2)))) Recursive c. (define (bar x) (if (= x 0) (helper2 x) (helper1 x))) Iterative (when x is 0, (helper2 0) returns immediately!) d. (define (bar x) (if (= x 0) (helper1 x) (helper2 x))) Recursive e. (define (bar x) (cond ((= x 0) 1) ((= (helper2 x) 3) 5) (else (helper1 x)))) Recursive f. (define (bar x) (helper2 (helper1 x))) Recursive Chung Wu; CS61A, Spring 2004 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Yoshimi Battles The Recursive Robots, Pt. 2 1. Consider the “ subset-sum
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

notes03s - 1 CS61ANotes03Efficiency[Solutionsv1.0]

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online