Lec14 - CMPSC 461: Programming Language Concepts Lecture 13...

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

View Full Document Right Arrow Icon
CMPSC 461: Programming Language Concepts Lecture 13 Instructor: Prof. Swarat Chaudhuri
Background image of page 1

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

View Full DocumentRight Arrow Icon
Problem 7: the trouble with general recursion Tail recursion is more memory- efficient (doesn’t need to store arguments in a stack). (define foo (lambda (n) (if (< n 2) n (+ foo (- n 1)) (foo (- n 2)))])))
Background image of page 2
Exponential Fibonacci n n - 2 n - 3 n - 2 n - 1 The problem: the same subproblem being solved repeatedly.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Tail recursion, on the other hand Makes you iterate from 1 to n (define (tr-fibonacci n) (letrec ([aux (lambda (m a b) (if (= m 0) a (aux (- m 1) b (+ a b))))]) (aux n 0 1)))) for (m = n; m > 0; m--) { a = b; b = a + b; }
Background image of page 4
Memoization Recursion could be just as good if you could reuse results. How do we do this?? Answer: Put results in a table. General pattern in algorithms: dynamic programming. How do we do this???
Background image of page 5

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

View Full DocumentRight Arrow Icon
Need a table data structure You can use hashtable etc. Association list: list of pairs ’((39 . #t) (40 . #f)) (assq lst obj): returns the first matching pair
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/30/2008 for the course CMPSC 461 taught by Professor Chaudhuri during the Fall '08 term at Pennsylvania State University, University Park.

Page1 / 18

Lec14 - CMPSC 461: Programming Language Concepts Lecture 13...

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

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