Slide 1 CS3: Lecture 8 Advanced recursion. Slide 2 Schedule Oct 10Advanced recursion Oct 17Number-spelling Miniproject Oct 24Higher order procedures Oct.

Slides:



Advertisements
Similar presentations
4/16/2015 MATH 224 – Discrete Mathematics Counting Basic counting techniques are important in many aspects of computer science. For example, consider the.
Advertisements

©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part C Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
Chapter 11 Indexing and Hashing (2) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
CSCE 2100: Computing Foundations 1 Combinatorics Tamara Schneider Summer 2013.
CS3 Fall 2005 Lecture 11: Finish higher order functions Midterm Prep.
Preliminaries Attendance sheets –I remembered! Survey links –HW1 time survey –Anonymous feedback survey HW discussion (4PM, Commons 9)
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 14: Number Spelling Mini-project.
Slide 1 CS3 Week 8: Recursion. Slide 2 Midterm 1 You did great If you need a re- grade, see the person that graded that question Solutions are available.
Slide 1. Slide 2 Administrivia Mid-semester survey this week (Thur/Fri) –you NEED to do this. Interviews should be starting in a few weeks. I apologize.
CS3 Fall 2005 Lecture 10: More on higher order functions.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 12: Homework stuff and Accumulating Recursion.
Slide 1 CS3 Fall 2005 Lecture week 13: Introduction to the Big Project.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 19: HOF Problems.
CS3: Introduction to Symbolic Programming Spring 2006Nate Titterton Lecture 4: "Difference Between Dates" and data abstraction.
Slide 1 Midterm 1 Midterm 1: Feb 28 th (next week). –In the lecture slot, plus 20 minutes (4:10-5:30 pm, 120 Latimer) –Everything we’ve covered, including.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
Slide 1. Slide 2 Midterm 1 You did great If you need a regrade, see the person that graded that question Solutions available on the portal soon.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 13: Bugs and Two Stage Recursion.
Administrivia Make sure you have completed the Mid Semester Survey –Scheduled the week before Spring Break –this will be worth some points Reading is.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 11: Accumulating Recursion.
Feb 28More complicated recursions March 7Tree recursion, etc. Number-spelling Miniproject (#2) March 14Higher order procedures March 21Spring Break March.
CS3: Introduction to Symbolic Programming Spring 2006Nate Titterton Lecture 2: Introduction, and Conditionals.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 8: Recursion.
Midterm Logistics Where? 4 Leconte ( When? Monday, 4:10 to 5:40 What to do? –“Review problems”
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 9: Recursion Rocks!
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions.
Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 15: Procedures as Arguments.
Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
1 9/11/2015 MATH 224 – Discrete Mathematics Iterative version Recursive version Normally i is initialized to 0 and j to n–1 for an array of size n. Recursive.
Arrays II Robin Burke IT 130. Outline Homework #6 Portfolio Array review Array examples Array algorithms Multi-dimensional arrays.
CS61A Lecture Colleen Lewis. Clicker Query What else are you doing this summer? A)Taking another class B)Working C)Taking another class and.
Fall 2002CMSC Discrete Structures1 One, two, three, we’re… Counting.
Warm-up Simplify. 5x x – a + 2b – (a – 2b) Multiply.
1 CSC 321: Data Structures Fall 2013 Counting and problem solving  mappings, bijection rule  sequences, product rule, sum rule  generalized product.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
CDA 3100 Spring Special Thanks Thanks to Dr. Xiuwen Liu for letting me use his class slides and other materials as a base for this course.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
2/24/20161 One, two, three, we’re… Counting. 2/24/20162 Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter.
Warm-up Time-limit….. 5 P 2 = 11 ! = 7 C 3 = 7!___ = 3!(7-3)! 5 C C 3 =
GRE SENTENCE EQUIVALENCE STRATEGY. SENTENCE EQUIVALENCE STRATEGY Read the Sentence & look for Clues Predict an Answer Select the two Choices that most.
CS 360 Lab 1 Professor: Krzysztof Nowak TA: Mark Boady.
CS3: Introduction to Symbolic Programming Spring 2006Nate Titterton Lecture 2: Introduction, and Conditionals.
Non Linear Data Structure
CSC 321: Data Structures Fall 2015 Counting and problem solving
A quick and efficient way to expand binomials
COP4020 Programming Languages
PROGRAMMING IN HASKELL
Patterns, Patterns, and more Patterns!
Multiplying and Dividing Integers
Nicholas Shahan Spring 2016
Properties of the Real Numbers Part I
Objective The student will be able to:
Data Representation – Chapter 3
Lecture #8 מבוא מורחב.
Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.
Year 2 Spring Term Week 9 Lesson 2
Announcements Nate’s office hours Card key problems?
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Year 2 Spring Term Week 9 Lesson 2
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

Slide 1 CS3: Lecture 8 Advanced recursion

Slide 2 Schedule Oct 10Advanced recursion Oct 17Number-spelling Miniproject Oct 24Higher order procedures Oct 31More HOF Lists! (instead of sentences and words) Nov 7Recursion (extra advanced) Nov 14Midterm #2 Nov 21The big project!

Slide 3 Next lab: Catch-up day

Slide 4 Number Spelling miniproject Read Simply Scheme, page 233, which has hints Another hint (principle): don't force "everything" into the recursion. –Special cases may be easier to handle before you send yourself into a recursion

Slide 5 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 6 "Tail" recursions Accumulating recursions are sometimes called "Tail" recursions (by TAs, me, etc).

Slide 7 Tail recursions and efficiency A tail recursion has no combiner, so it can end as soon as a base case is reached –Compilers can do this efficiently An embedded recursion needs to combine up all the recursive steps to form the answer –The poor compiler has to remember everything

Slide 8 Tail or embedded? (1/3) (define (length sent) (if (empty? sent) 0 (+ 1 (length (bf sent)))))

Slide 9 Embedded! (length '(a b c d))  (+ 1 (length '(b c d))) (+ 1 (+ 1 (length '(c d)))) (+ 1 (+ 1 (+ 1 (length '(d))))) (+ 1 (+ 1 (+ 1 (+ 1 (length '()))))) (+ 1 (+ 1 (+ 1 (+ 1 0)))) (+ 1 (+ 1 (+ 1 1))) (+ 1 (+ 1 2)) (+ 1 3) 4

Slide 10 Tail or embedded? (2/3) (define (sent-max sent) (if (empty? sent) '() (sent-max-helper (bf sent) (first sent)))) (define (sent-max-helper sent max-so-far) (if (empty? sent) max-so-far (sent-max-helper (bf sent) (first sent))))

Slide 11 Tail or embedded? (3/3) (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))) ) ))

Slide 12 > (find-evens '( ))  (se 2 (se 4 (se 6 ())  (2 4 6) (se 2 (se 4 (se 6 () sent = ( ) sent = ( ) sent = ( ) sent = ( 5 6 ) sent = ( 6 ) sent = ( )

Slide 13 Advanced recursions (1/3) "when it does more than one thing at a time" Ones that traverse multiple sentences –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) ]

Slide 14 Advanced recursions (2/3) Recursions that have an inner and an outer recursion (no-vowels '(I like to type))  ("" lk t typ) (l33t '(I like to type))  (i 1i/<3 +0 +yP3) (strip-most-popular-letter '(cs3 is the best class))  (c3 i th bet cla) (occurs-in? 'abc 'abxcde)  #f

Slide 15 Advanced recursions (3/3) Tree recursion: multiple recursive calls in a single recursive step There are many, many others

Slide 16 Advanced recursion columns (C) r o w s (R) 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 17 (define (pascal C R) (cond ((= C 0) 1) ;base case ((= C R) 1) ;base case (else ;tree recurse (+ (pascal C (- R 1)) (pascal (- C 1) (- R 1)) )))

Slide 18 > (pascal 2 5) (+ (pascal 2 5) (pascal 2 4) (pascal 1 4) (+ (pascal 2 3) (pascal 1 3) (pascal 0 3) (+ (pascal 2 2) (pascal 1 2) (pascal 0 2)  1 (pascal 1 2) (pascal 0 2)  1 (+ (pascal 1 1) (pascal 0 1)  1 (+ (pascal 1 1)  1 (pascal 0 1)  1 (+ (pascal 1 1)  1 (pascal 0 1)  1

Slide 19 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 20 binary Write binary, a procedure to generate the possible binary numbers given n bits. (binary 1)  (0 1) (binary 2)  ( ) (binary 3)  ( )