More about recursion push/pop stack: metaphor from stack of trays key is that when you recurse, you have to have some way of remembering where you came from so you "push" everything you need to remember on a stack then to get back, you "pop" the stack This happens all the time in language, music, art, math, geography parenthetical phrases key shifts magritte fractals Bach -- Little Harmonic Labyrinth Music has a "tonic" -- a key in which it begins. This creates tension, expectation, and relief when you end in the tonic. Can push 1, maybe 2 keys in a song to create nested tension/resolution LHL starts in C Goes through all the keys, ends on C again Our stack just isn't that deep! (play -- 4 minutes) Recursion has to have a way of bottoming out! base case if you don't have this, you end up with GOD It's hard to think recursively! Comes with practice. We're spending so much time on this because it's one of the most critical thought processes in creating effective programs. It's also incredibly useful in reasoning ABOUT programs. Example: finding someone in a telephone book There are TONS of people in the phone book why is it easy for us? (sorted) how would you tell a computer to find someone? (high level) Would it be useful if it were sorted? Think recursively! go to middle, recurse on left or right why is this better? Is it ALWAYS better? What if we only had to search once? Could we do better still? (define length (lambda (l) (if (null? l) 0 (+ 1 (length (cdr l))) ) ) ) (define sumlist (lambda (l) (if (null? l) 0 (+ (car l) (sumlist (cdr l))) ) ) ) (define productlist (lambda (l) (if (null? l) 1 (* (car l) (productlist (cdr l))) ) ) ) (define insertl (lambda (l func nullval) (if (null? l) nullval (func (car l) (insertl (cdr l) func nullval)) ) ) ) (define sumlist2 (lambda (l) (insertl l + 0))) (define productlist2 (lambda (l) (insertl l * 1))) (define length2 (lambda (l) (insertl l (lambda (firstelem everythingelse) (+ 1 everythingelse)) 0) ) ) (define ourmap (lambda (func l) (if (null? l) null (cons (func (car l)) (ourmap func (cdr l)))))) (define remove (lambda (cf l) (if (null? l) null (if (cf (car l)) (remove cf (cdr l)) (cons (car l) (remove cf (cdr l))))))) (define filter (lambda (cf l) (remove (lambda (x) (not (cf x))) l))) (define intsto (lambda (n) (if (equal? n 0) null (append (intsto (- n 1)) (list n) )))) (define rev (lambda (l) (if (null? l) null (append (rev (cdr l)) (list (car l)))))) (define get-kth-entry (lambda (l k) (if (null? l) (error "oh no you didn't") (if (equal? k 1) (car l) (get-kth-entry (cdr l) (- k 1))))))