CSC151.01 2006S, Class 11: Recursion Admin: * Reminder: You all got a free extension on HW5 until Friday. Questions on HW5? * Please reread the reading on recursion for tomorrow's class. * Upcoming EC: * Today's CS Extra * Today's community meal at 4:30 or so in Davis School * Thursday's Convo * Saturday's Symphonic Band and Percussion Ensemble Saturday at 2:00 p.m. in Sebring-Lewis * Saturday's Con Brio and G-Tones Concert (4 p.m. Herrick) * Saturday's Football Game. * Check Web for list of attendance EC * We'll try the HW4 demo again. * Partners for this week: * An and Howard * Brown and Rider * Berg and Cartagena * Brooks and Cloninger * Bonnin Cadogan and Miller * Harrington and Mertes * LaRue and Zamora (and Rich, today) * McArdle and Pan * McDonald and McFarlin * Potthoff and Thompson * Rich and Wood Overview: * Questions. * Lab. * Reflection. /HW4 Issues/ * Sometimes you're going to have to write stuff that other people will work with. * Almost no one did numbers right * Wanted: (define numbers (list 1 2 3)) * Got: (define numbers (list "1" "2" "3")) * (string-append "There were " (number->string (list-ref numbers 2)) " " (list-ref nouns 1) "s.") * Lesson: Converting types * Lesson: Your stuff will get posted on the web. (And, hey, if I'm okay with people seeing my mispellings, then you should be fine with that, too.) * If you object, please let me know. /HW5 Questions/ * Can I use conditionals? (E.g., for indefinite articles.) * Yes * How do I determine whether the first letter of a word is a vowel? * string-ref * compare characters with eqv? * combine comprisons with and and or. * How do I get started on #5? "Chop off after the second decimal point." * Use the tools at your disposal. * truncate (round, floor, ceiling) * How do you get rid of less? * Hint: Shift the decimal point. * Talk to other people! /Repetition through Recursion/ * Key idea in recursion: Procedures can call themselves. (define RECURSIVE-PROCEDURE (lambda (PARAMS) ... (RECURSIVE-PROCEDURE ...) ...)) * A little odd, but some benefits (define RECURSIVE-PROCEDURE (lambda (PARAMS) (if ITS-SIMPLE GIVE-THE-SIMPLE-ANSWER USE-THE-PROCEDURE-TO-ANSWER-A-SIMPLER-PROBLEM))) * Problem: Sum a list * What lists are easy to sum? * The one element list. The sum is that element. * Lists that have more than one element are harder to sum. * How do I use the sum procedure to write the sum procedure? * Not using the same list. * Use a simpler list (say the cdr) (define sum (lambda (values) (if (null? (cdr values)) (car values) (+ (car values) (sum (cdr values)))))) (sum (list 1 2 3)) => ... (sum (list 2 3)) => ... (sum (list 3)) /Reflection/ * Which do you prefer, sum or new-sum? * sum * new-sum * Which do you prefer, new-sum or newer-sum? *