one slide summary list recursion examples recursive
play

One-Slide Summary List Recursion Examples & Recursive - PDF document

One-Slide Summary List Recursion Examples & Recursive Procedures Recursive functions that operate on lists have a similar structure. list-cruncher is a higher-order function that can be used to implement many others. Decisions in a


  1. One-Slide Summary List Recursion Examples & Recursive Procedures • Recursive functions that operate on lists have a similar structure. list-cruncher is a higher-order function that can be used to implement many others. • Decisions in a function can be abstracted out by adding a function argument. For example, find- closest-number is just find-closest plus a function defining what a close-number is. • The Fibonacci numbers are a recursively-defined sequence . • Almost all music uses a stack structure: starts on the tonic, repeats similar patterns in a structured way, ends on the tonic. #1 #2 Outline Similarities and Differences • Your Comments (define (sumlist p) (define (map f p) • list-cruncher (if (null? p) (if (null? p) 0 null • find-closest-number (+ (car p) (cons (f (car p) ) – Reminder: procedure definition strategy! (sumlist (cdr p))))) (map f (cdr p))))) • find-closest (define (list-cruncher lst) • Fibonacci numbers (if (null? lst) • Recursive Transition Networks base result ( combiner (car lst) – vs. Backus-Naur Form Grammars (recursive-call ... (cdr lst)))) • Musical Harmony #3 #4 Similarities and Differences How could this work? • I want to crunch all lists. How would I get (define (sumlist p) (define (map f p) (if (null? p) started? (if (null? p) 0 null (+ (car p) (cons (f (car p) ) (sumlist (cdr p))))) (map f (cdr p))))) (define (list-cruncher ? ... ? lst) (if (null? lst) base result ( combiner (car lst) (recursive-call ... (cdr lst)))) #5 #6

  2. One Ring To Rule Them All? Crunchy Center (define (list-cruncher base proc combiner lst) (define (list-cruncher base proc combiner lst) (if (null? lst) (if (null? lst) base base (combiner (proc (car lst)) (combiner (proc (car lst)) (list-cruncher base proc combiner (list-cruncher base proc combiner (cdr lst))))) (cdr lst))))) (define (sumlist p) • How would you define length using list-cruncher? (list-cruncher 0 (lambda (x) x) + p)) (define (length lst) (if (null? lst) 0 (define (map f p) (+ 1 (length (cdr lst))))) (list-cruncher null f cons p)) #7 #8 Crunchy Center 2 list-cruncher crunches length (define (list-cruncher base proc combiner lst) (define (list-cruncher base proc combiner lst) (if (null? lst) (if (null? lst) base base (combiner (proc (car lst)) (combiner (proc (car lst)) (list-cruncher base proc combiner (list-cruncher base proc combiner (cdr lst))))) (cdr lst))))) (define (length p) (if (null? p) 0 • How would you define filter using list-cruncher? (define (filter predicate lst) (+ 1 (length (cdr p))))) (if (null? lst) null (define (length p) (append (list-cruncher 0 (lambda (x) 1) + p)) (if (predicate (car lst)) (list (car lst)) null) (filter predicate (cdr lst))))) #9 #10 list-cruncher crunches filters Liberal Arts Trivia: Drama (define (list-cruncher base proc combiner lst) (if (null? lst) • In this 1948 play by Samuel Beckett has been base called “the most significant English-language (combiner (proc (car lst)) play of the 20 th century”. The minimal setting (list-cruncher base proc combiner (cdr lst))))) calls to mind “the idea of the ‘lieu vague’, a (define (filter predicate lst) location which should not be particularised”, (if (null? lst) null and the play features two characters who (append (if (predicate (car lst)) (list (car lst)) null) never meet the title character. (filter predicate (cdr lst))))) (define (filter pred lst) (list-cruncher null (lambda (carlst) (if (pred carlst) (list carlst) null)) append lst)) #11 #12

  3. Liberal Arts Trivia: History find-closest-number • At the height of its power, in the 16 th and 17 th • The function find-closest-number takes two century, this political organization spanned arguments. The first is a single number called three continents. It controlled much of the goal. The second is a non-empty list of Southeastern Europe, the Middle East and numbers. It returns the number in the input North Africa, and contained 29 provinces and list that is closest to the goal number. multiple vassal states. Noted cultural > (find-closest-number 150 (list 101 110 120 157 340 588)) achievements include architecture (vast inner 157 spaces confined by seemingly weightless yet > (find-closest-number 12 (list 4 11 23)) massive domes, harmony between inner and 11 > (find-closest-number 12 (list 95)) We'll do this outer spaces, articulated light and shadow, one together! 95 etc.), classical music, and cuisine. #13 #14 Recall The Strategy! find-closest-number hint One Approach for the Recursive Case: Be optimistic! You have two possible answers: the Assume you can define: current car of the list and the result of (find-closest-number goal numbers) the recursive call. Compare them both that finds the closest number to goal against the goal number, and return the from the list of numbers. one that is closer. What if there is one more number ? Can you write a function that finds the closest number to match from new- number and numbers? #15 #16 Optimistic Function Defining Recursive Procedures 2. Think of the simplest version of the (define (find-closest goal numbers) problem (almost always null), something ;; base case missing for now you can already solve. ( base case ) (if (< (abs (- goal (car numbers))) (abs (- goal ( find-closest-number goal (cdr numbers))))) Is null the base case for (car numbers) ( find-closest-number goal (cdr numbers)))) find-closest-number? #17 #18

  4. (define (find-closest-number goal numbers) (if (= 1 (length numbers)) find-closest-number defined (car numbers) (if (< (abs (- goal (car numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) (define ( find-closest-number goal numbers) (car numbers) (find-closest-number goal (cdr numbers))))) (if (= 1 (length numbers)) ;; base case (car numbers) > (find-closest-number 150 (list 101 110 120 157 340 588)) (if (< (abs (- goal (first numbers))) 157 (abs (- goal > (find-closest-number 0 (list 1)) (find-closest-number 1 goal (cdr numbers))))) > (find-closest-number 0 (list )) (car numbers) first: expects argument of type <non-empty list>; given () (find-closest-number (cdr numbers)))) #19 #20 Generalizing find-closest-number Generalizing find-closest-number • How would we implement • How would we implement find-closest-number-without-going-over? find-closest-number-without-going-over? • What about find-closest-word? • What about find-closest-word? • ... • ... The “closeness” metric should be a procedure parameter! #21 #22 find-closest Using find-closest (define (find-closest goal lst closeness ) (define (find-closest-number goal numbers) (if (= 1 (length lst)) (find-closest goal numbers (car lst) (lambda (a b) (abs (- a b))))) (if (< ( closeness goal (car lst)) ( closeness goal (find-closest goal (cdr lst) closeness))) (car lst) (define (find-closest-below goal numbers) (find-closest goal (cdr lst) closeness))) (find-closest goal numbers (lambda (a b) How can we implement find-closest-number (if (>= a b) (- a b) 99999)))) using find-closest? #23 #24

  5. find-closest find-closest (define (find-closest goal lst closeness) (define (find-closest goal lst closeness) (if (= 1 (length lst)) (if (= 1 (length lst)) (car lst) (car lst) ( pick-closest closeness goal (car lst) (if (< (closeness goal (car lst)) (find-closest goal (cdr lst) closeness)))) (closeness goal (find-closest goal (cdr lst) closeness))) (define ( pick-closest closeness goal num1 num2) (car lst) (if (< (closeness goal num1) (find-closest goal (cdr lst) closeness))) (closeness goal num2)) num1 How can we avoid num2)) evaluating find-closest twice? #25 #26 Seen Anything Like This? Liberal Arts Trivia: Philosophy (define (find-best-match sample tiles color-comparator) • This branch of philosophy, which Aristotle (if (= (length tiles) 1) called “First Philosophy”, investigates (car tiles) (define (pick-better-match (pick-better-match sample tile1 tile2 principles of reality transcending those of any color-comparator) sample particular science. It is concerned with (if (color-comparator sample (car tiles) (tile-color tile1) (tile-color tile2)) explaining the ultimate nature of being and (find-best-match tile1 the world (e.g., determinism and free will, sample tile2)) (cdr tiles) mind and matter, space and time). Its modern color-comparator) name comes from the fact that Aristotle's color-comparator)))) chapters about it were placed “beyond” his chapters on matter and force. find-best-match from PA1 (Photomosaics) is just find-closest ! pick-better-match is just pick-closest ! You could write all of PA1. #27 #28 Liberal Arts Trivia: Film Studies Liberal Arts Trivia: Painting • Born in 1965 to Muslim parents, this Indian • Name this 1930 oil- actor has starred in flims such as Kuch Kuch on-beaverboard Hota Hai , Kal Ho Naa Ho , Veer-Zaara , and painting by Grant Devdas . In 2008, Newsweek named him one of Wood. It is one of the the 50 most powerful people in the world. He most familiar images has replaced Amitabh “Big B” Bachchan as the of 20 th century host of Kaun Banega Crorepti, and has won American art and has India's Padma Shri, a life-sized wax statue at achieved an iconic Madame Tussaud's, and the French status. government's Ordre des Arts et des Lettres. #29 #30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend