1 / 12

Recursion: Linear and Tree Recursive Processes and Iteration

Recursion: Linear and Tree Recursive Processes and Iteration. CMSC 11500 Introduction to Computer Programming October 7, 2002. Roadmap. Recap: Lists and Structural Recursion Structural Recursion (cont’d) Returning lists from procedures Hierarchical lists: lists of lists

Download Presentation

Recursion: Linear and Tree Recursive Processes and Iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion: Linear and Tree Recursive Processes and Iteration CMSC 11500 Introduction to Computer Programming October 7, 2002

  2. Roadmap • Recap: Lists and Structural Recursion • Structural Recursion (cont’d) • Returning lists from procedures • Hierarchical lists: lists of lists • Tree-structured Recursion • Generative recursion (Problem-based) • Linear recursive processes • Iteration • Order of growth & exponentiation

  3. Recap: Lists: Self-referential data • Recap: Lists and Structural Recursion • Arbitrarily long data structures • Lists: Constructor: cons; Selectors: car, cdr • Data definition: 1) empty list ‘() 2) (cons x alist) • Self-referential definition • Manipulating lists: Structural Recursion • Self-referential procedures • Need cases for: • 1) Empty list • 2) Compound case: Consume first (car) value; Call function on rest of list (cdr)

  4. Returning Lists: Square-list Description: Take a list of numbers as input, return a list of numbers with each of the values squared (define (square-list numlist) (cond ((null? numlist) ‘()) (else (cons (square (car numlist)) (square-list (cdr numlist))))))

  5. Lists of Lists • Can create lists whose elements are lists • “closure” property of cons • Objects formed by cons can be combined with cons • Allows creation of hierarchical data structures • (cons (list 1 2) (list 3 4)) => ((1 2) 3 4) 3 4 1 2

  6. Tree Recursion • Before: Assumed all lists “flat”Now: Allow possibility of lists of lists (define (square-tree alist) (cond ((null? alist) ‘()) ((not (pair? alist)) (square alist)) (else (cons (square-tree (car alist)) (square-tree (cdr alist))))))

  7. Generative Recursion • Syntactically recursive: • Procedure calls itself • Self-reference: • Property of solution to problem • Rather than data structure of input • E.g. Factorial: • n! = n*(n-1)*(n-2)*….*3*2*1 • n! = n*(n-1)! • Function defined in terms of itself

  8. Recursion Example: Factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))) N=4 (* 4 (factorial 3)) (* 4 (* 3 (factorial 2))) (* 4 (* 3 (* 2 (factorial 1)))) (* 4 (* 3 (* 2 1)))) (* 4 (* 3 (* 2 1))) (* 4 (* 3 2)) (* 4 6) 24

  9. Recursion Components • Termination: • How you stop this thing???? • Condition& non-recursive step: e.g. (if (= n 1) 1) • Reduction in recursive step: e.g. (factorial (- n 1)) • Compound and reduction steps • “Divide and conquer” • Reduce problem to simpler pieces • Solve pieces and combine solutions

  10. Linear Recursive Processes • How does the procedure use resources? • Time: How many steps? • Space: What do we have to remember • Factorial: • Time: n multiplications • Space: Builds up deferred computations • Interpreter must remember them • Needs space for n remembered computations

  11. Factorial: Iterative Process (define factorial (fact-iter 1 1 n)) (define fact-iter (product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) (fact-iter 1 1 4) (fact-iter 1 2 4) (fact-iter 2 3 4) (fact-iter 6 4 4) (fact-iter 24 5 4) 24

  12. Iterative Process • Same computations as recursive factorial • Time: n multiplications • Space: constant: no deferred steps to remember • Key: • State of computation captured by variables • product, counter: how much has been done so far • If have state, could restart process

More Related