CSC151 2007S, Class 23: Recursion Basics Admin: * Are there questions on Assignment 6? * Are there questions in preparation for the quiz? * The reading for Monday is available: Recursion with Helper Procedures. * We'll do things in a slightly different order today: * I'll chat for a few minutes about the topic * You'll take the quiz. * You'll do the lab. * Because of the quiz and the lecture, you won't be able to finish the lab in class. * WE'LL USE MONDAY'S CLASS FOR MORE! Overview: * The idea of recursion. * A sample recursive procedure: sum. * Another example: Filtering. * Lab. ==Quiz Q's== Q: What's the difference between let and let*? A: Similarities: Both have the same form (let ((name val) (name val) ... (name val)) exp) Both have the same purpose: Defining local names to use in the expression * Differ in the order of evaluation and binding * let evaluates *all* of the vals in the name/value pairs and then binds * let* evaluates one value at a time and then binds it before going on to the next pair * They behave differently if one of the later values depends on an earlier name. ==Recursion and Repetition (or vice versa)== * Repetition: Key algorithm design component * Right now, only a few techniques * foreach! with lists, computes (nothing), called for side effect * map, with lists, computes new list, no side effects (we hope) * "copy and paste" - does not support arbitrary repetition, ugly, inelegant, error-prone, and what Sam does too much of the time. * image-variant, with images, computes new image by doing something with each pixel * image-transform!, with images, modifies the underlying image by doing something to the color at each pixel * Requires function from color to color * image-compute-pixels!, with images, modifies the underlying image by applying something to each position to give a color for the pixel * Can work only on a section * Requires function from position to color * But what if we want other kinds of repetition * List as input, number as output * Length of a list * Sum of elements in the list * Number as input, list as output * N => (1 2 3 ... N) * List as input, different length list as output * Recusion lets us design any kind of repetition we want