Slide 1

advertisement
Slide 1
Midterm 1
• You did great
• If you need a
regrade, see the
person that graded
that question
• Solutions available
on the portal soon.
30
20
10
Std. Dev = 3.81
Mean = 22.3
N = 84.00
0
8.0
10.0 12.0 14.0 16.0 18.0 20.0 22.0 24.0 26.0 28.0
Total Score (out of 28)
Slide 2
Schedule
Feb 28
Midterm #1
March 7 Tree recursion, etc.
Number-spelling Miniproject
March 14 Higher order procedures
March 21 Spring Break
March 28 More HOF
Lists! (instead of sentences and words)
April 4
Recursion (advanced)
April 11
Midterm #2
Slide 3
Number Spelling
• Read Simply Scheme, page 233, which
has hints
• Another hint (principle): don't force
"everything" into the recursion.
Slide 4
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 5
> (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 6
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.
– Knowing recursion WILL help with all sorts of ways
while programming, even if you don’t often use it.
Slide 7
Recursive patterns
• Most recursive functions that operate on a
sentence fall into:
– Mapping: square-all
– Counting: count-vowels, count-evens
– Finding: member, first-even
– Filtering: keep-evens
– Testing: all-even?
– Combining: sum-evens
Slide 8
What recursions aren’t covered by
these patterns?
• Weird ones like reverse, or downup
• Ones that don’t traverse a single sentence
– E.g., mad-libs takes a sentence of
replacement words [e.g., ‘(fat Henry
and a sentence to mutate [e.g.,
three)]
‘(I saw a * horse named * with * legs)]
• Tree recursion: multiple recursive calls in a
single recursive step
Slide 9
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 10
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 11
Download