CS3 Week 8: Recursion Slide 1 Midterm 1 20 10 Std. Dev = 5.01 Mean = 21.8 N = 158.00 0 .0 29 .0 27 .0 25 .0 23 .0 21 .0 19 .0 17 .0 15 .0 13 .0 11 0 9. 0 7. 0 5. • You did great • If you need a regrade, see the person that graded that question • Solutions are available on the portal (check announcements). SCALED Slide 2 Question 1 • [Shotgun questions] 60 50 • (+ 3 x 5) may not cause an error… 40 30 – (+ 3 'fred 5) will 20 10 Std. Dev = 1.19 Mean = 5.4 N = 158.00 0 3.0 4.0 5.0 6.0 7.0 Q1 Slide 3 Question 2 [add-em] (add-em '(1 4 2 0 934 -3 5)) 7 50 40 30 • The conditional was tricky here: – Needed to check for 3 things to get sent to the base case 20 10 Std. Dev = 1.50 Mean = 5.5 N = 158.00 0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 Q2 Slide 4 Question 3 [ranking cards] • Scores are all over the place (hallmark of a bad question) • Accessors! 40 30 20 10 Std. Dev = 2.43 Mean = 4.8 N = 158.00 0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 Q3 Slide 5 Question 4 [day-span] • Question 4c – the short answers – was a good one (I think). • Some had trouble deciding between – conditional , – the base case, – making the problem smaller, – calling the function recursively – combining the recursive calls. 50 40 30 20 10 Std. Dev = 3.12 Mean = 6.7 N = 158.00 0 0.0 2.0 4.0 6.0 8.0 10.0 12.0 Q4C Slide 6 Question 5 [coins] • Nice! 80 60 40 20 Std. Dev = 1.83 Mean = 7.7 N = 158.00 0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 Q5 Slide 7 Schedule Oct 3 Midterm #1 Oct 10 Advanced recursion Oct 17 Number-spelling Miniproject Oct 24 Higher order procedures Oct 31 More HOF Lists! (instead of sentences and words) Nov 7 Recursion (advanced) Nov 14 Midterm #2 Slide 8 Problem: find all the even numbers in sentence of numbers (define (find-evens sent) (cond ((empty? ( sent) ;base case '() ) ( ((odd? (first sent)) ;rec case 1 1: odd (find-evens (bf sent)) ) ( (else ;rec case 2: even (se (first sent) (find-evens (bf sent))) ) )) Slide 9 > (find-evens '(2 3 4 5 6)) sent = ( 2 3 4 5 6 ) (se 2 sent = ( 3 4 5 6 ) sent = ( 4 5 6 ) (se 4 sent = ( 5 6 ) sent = ( 6 ) (se 6 sent = ( ) () (se 2 (se 4 (se 6 ()) (2 4 6) Slide 10 Why is recursion hard? • ONE function: – replicates itself, – knows how to stop, – knows how to combine the “replications” • There are many ways to think about recursion: you absolutely do not need to understand all of them. – "down-up": recursion as an extension of writing many specific functions – "many base cases": recursion as using a clone, once you have many base cases Slide 11 Patterns in recursion • Most recursions fall into a kind of patterns – Students say that this helps them • As a corollary, some recursions don’t! Slide 12 • Mapping – does something to every part of the input sentence – E.g., square-all • Counting – Counts the number of elements that satisfy a predicate – E.g., count-vowels, count-evens • Finding – Return the first element that satisfies predicate (or, return rest of sentence) – E.g., member, member-even Slide 13 • Filtering – Keep or discard elements of input sentence – E.g., keep-evens • Testing – A predicate that checks that every or any element of input satistfies a test – E.g., all-even? • Combining – Combines the elements in some way… – E.g., sentence-sum Slide 14 What recursions aren’t covered by these patterns? • Weird ones like reverse, or downup – ... bowling ... • "Advanced" recursions: – when it does more than one thing at a time – Ones that don’t traverse a single sentence • E.g., mad-libs takes a sentence of replacement words [e.g., ‘(fat Henry three)] and a sentence to mutate [e.g., ‘(I saw a * horse named * with * legs)] – Tree recursion: multiple recursive calls in a single recursive step Slide 15 Advanced recursion columns (C) 0 1 2 3 4 5 ... 0 1 r o w s 1 1 1 2 1 2 1 3 1 3 3 1 (R) 4 1 4 6 4 1 5 1 5 10 10 5 1 ... ... ... ... ... ... ... ... ... Pascal’s Triangle ... ... ... ... ... • How many ways can you choose C things from R choices? • Coefficients of the (x+y)^R: look in row R • etc. Slide 16 pair-all • Write pair-all, which takes a sentence of prefixes and a sentence of suffixes and returns a sentence pairing all prefixes to all suffixes. – (pair-all ‘(a b c) ‘(1 2 3)) (a1 b1 c1 a2 b2 c2 a3 b3 c3) – (pair-all ‘(spr s k) ‘(ite at ing ong)) (sprite sprat spring sprong site sat sing song kite kat king kong) Slide 17