Title: CS3 Week 8: Recursion
1CS3Week 8 Recursion
2Midterm 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).
3Question 1
- Shotgun questions
- ( 3 x 5) may not cause an error
- ( 3 'fred 5) will
4Question 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
5Question 3
- ranking cards
- Scores are all over the place (hallmark of a bad
question) - Accessors!
6Question 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.
7Question 5
8Schedule
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
9Problem find all the even numbers insentence 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))) ) ))
10gt (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)
11Why 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
12Patterns in recursion
- Most recursions fall into a kind of patterns
- Students say that this helps them
- As a corollary, some recursions dont!
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- 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
15What recursions arent 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 dont 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
16Advanced recursion
 columns (C)  columns (C)  columns (C)  columns (C)  columns (C)  columns (C)  columns (C)  columns (C)
rows(R) 0 1 2 3 4 5 ...
rows(R) 0 1 Â Â Â Â Â ...
rows(R) 1 1 1 Â Â Â Â ...
rows(R) 2 1 2 1 Â Â Â ...
rows(R) 3 1 3 3 1 Â Â ...
rows(R) 4 1 4 6 4 1 Â ...
rows(R) 5 1 5 10 10 5 1 ...
rows(R) ... ... ... ... ... ... ... ...
Pascals Triangle
- How many ways can you choose C things from R
choices? - Coefficients of the (xy)R look in row R
- etc.
17pair-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)