Sorting: - recursive definition: sorting an empty list is the empty list else insert the first item into the sorted rest of the list - needs helper function to insert inserting into an empty list is the singleton list compare item to first in the list -- what do we do? Iteration and recursion: Computational processes - Last time, the question of why (define (fib n) (cond [(= n 0) 1] [(= n 1) 1] [else (+ (fib (- n 1)) (fib (- n 2)))])) is so much slower than (define (! n) (cond [(= n 0) 1] [else (* n (! (- n 1)))])) was asked... Let's answer that Do a couple of levels of substitution... Fibonacci numbers: two recursive calls per level ("tree", exponential growth) repeated computation Factorial: one recursive call per level no repeated computation list of n computations must be remembered, then computed after unrolling the function calls. How could we fix the Fibonacci numbers? memoization: remember the value of a function after you compute it different algorithm For that matter, can we "fix" the factorial? "Iteration" versus "Recursion" Iterative factorials Iterative exponentiation Iterative Fibonaccis Iteration: constant space, keep track of "current" value Recursion: space grows, need all values Can we do even better? Exponentiation...