CS3 Week 8: Recursion - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS3 Week 8: Recursion

Description:

If you need a re-grade, see the person that graded that question ... (sprite sprat spring sprong site sat sing. song kite kat king kong) ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 18
Provided by: natetit
Category:
Tags: cs3 | king | kong | recursion | week

less

Transcript and Presenter's Notes

Title: CS3 Week 8: Recursion


1
CS3Week 8 Recursion
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
Question 1
  • Shotgun questions
  • ( 3 x 5) may not cause an error
  • ( 3 'fred 5) will

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
Question 3
  • ranking cards
  • Scores are all over the place (hallmark of a bad
    question)
  • Accessors!

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
Question 5
  • coins
  • Nice!

8
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
9
Problem 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))) ) ))
10
gt (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)

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
Patterns 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

15
What 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

16
Advanced 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.

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)
Write a Comment
User Comments (0)
About PowerShow.com