Download presentation
Presentation is loading. Please wait.
1
Slide 1 CS3 Week 8: Recursion
2
Slide 2 Midterm 1 You did great If you need a re- grade, see the person that graded that question Solutions are available on the portal (check announcements).
3
Slide 3 Question 1 [Shotgun questions] (+ 3 x 5) may not cause an error… –(+ 3 'fred 5) will
4
Slide 4 Question 2 [add-em] ( add-em '(1 4 2 0 934 -3 5)) 7 The conditional was tricky here: –Needed to check for 3 things to get sent to the base case
5
Slide 5 Question 3 [ranking cards] Scores are all over the place (hallmark of a bad question) Accessors!
6
Slide 6 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.
7
Slide 7 Question 5 [coins] Nice!
8
Slide 8 Schedule Oct 3Midterm #1 Oct 10Advanced recursion Oct 17Number-spelling Miniproject Oct 24Higher order procedures Oct 31More HOF Lists! (instead of sentences and words) Nov 7Recursion (advanced) Nov 14Midterm #2
9
Slide 9 Problem: find all the even numbers in sentence of numbers (define (find-evens sent) (cond ( ;base case ) ( ;rec case 1: odd ) ( ;rec case 2: even ) )) (define (find-evens sent) (cond ((empty? sent) ;base case '() ) ((odd? (first sent)) ;rec case 1 (find-evens (bf sent)) ) (else ;rec case 2: even (se (first sent) (find-evens (bf sent))) ) ))
10
Slide 10 > (find-evens '(2 3 4 5 6)) (se 2 (se 4 (se 6 ()) (2 4 6) (se 2 (se 4 (se 6 () sent = ( 2 3 4 5 6 ) sent = ( 3 4 5 6 ) sent = ( 4 5 6 ) sent = ( 5 6 ) sent = ( 6 ) sent = ( )
11
Slide 11 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
12
Slide 12 Patterns in recursion Most recursions fall into a kind of patterns –Students say that this helps them As a corollary, some recursions don’t!
13
Slide 13 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
14
Slide 14 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
15
Slide 15 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
16
Slide 16 Advanced recursion columns (C) r o w s (R) 012345... 01 111 2121 31331 414641 51510 51... 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.
17
Slide 17 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.