{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

CMPSC 461: Programming Language Concepts Lecture 13 Instructor: Prof. Swarat Chaudhuri

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

View Full Document
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)))])))
Exponential Fibonacci n n - 2 n - 3 n - 2 n - 1 The problem: the same subproblem being solved repeatedly.

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

View Full Document
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; }
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???

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

View Full Document
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 whose car matches obj. Returns #f if no match found (cond [ exp => function]) if exp is not #f, then apply function to it.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online