one slide summary list
play

One-Slide Summary List Writing recursive functions that operate on - PDF document

One-Slide Summary List Writing recursive functions that operate on Recursion: recursive data structures takes practice . There are standard approaches to such problems. list? , member , sumlist , intsto , map and filter Practice are all


  1. One-Slide Summary List • Writing recursive functions that operate on Recursion: recursive data structures takes practice . There are standard approaches to such problems. • list? , member , sumlist , intsto , map and filter Practice are all important recursive functions that & operate on lists. You should know what they do Examples and how to write them. • DrScheme can trace the execution of a recursive function to make it easier to understand. #1 #2 Outline Bookkeeping • Review: Procedure Problem Solving • PS2 Partners Posted • Review: list, cons, car, cdr – Meet during lab hours? • list? • PS1 Written Grades • member Posted • sumlist – Holding Fee – Pick them up • intsto • Feynman Point? • map – Read the book! • filter • Tracing #3 #4 How To Write A Procedure Defining A Procedure • Be optimistic! • Find out what it is supposed to do. • Base case : Think of the simplest input to the – What are the inputs ? What types of values? problem that you know the answer to. – What is the output ? A number? Procedure? List? – For number inputs, this is often zero. • Think about some example inputs and outputs – For list inputs, this is often the empty list (null). • Define your procedure • Recursive step : Think of how you would solve the problem in terms of a smaller input. Do – More on this next slide part of the work now, then make a recursive • Test your procedure call to handle the rest. – For numbers, this usually involves subtracting 1. – For lists, this usually involves cdr. #5 #6

  2. Pairs and Lists Procedure Skeleton • cons makes a pair of two things – (cons 1 2) --> (1 . 2) • The vast majority of recursive functions look – (pair? (cons 1 2)) --> #t like this: • car and cdr get the first and second part – (car (cons “a” “b”)) --> “a” (define (my-procedure my-input) – (cdr (cons “y” “z”)) --> “z” (if ( is-base-case? my-input) • A list is either null or a pair where the second element is also a list ( handle-base-case my-input) ( combine ( first-part-of my-input) – (cons 1 (cons 2 (cons 3 null))) --> (1 2 3) – (list 1 2 3) --> (1 2 3) (my-procedure ( rest-of my-input))))) – (null? (list 1 2)) --> #f – (append (list 1 2) (list 3 4)) -> (1 2 3 4) #7 #8 More Power Needed! list? • The list? function takes a single argument and returns #t if that argument is a list, #f otherwise. – Recall: a list is either null or a pair where the second element is a list – (list? null) --> #t – (pair? (cons 1 2)) --> #t – (list? (cons 1 null)) --> #t – (list? 5) --> #f – (list? (cons 1 2)) --> #f • Write it now on paper. Base case? Recursion? #9 #10 list? Hint Definition of list? • Here's a hint: • Here it is: Base (define (list? something) Base Case! Case! (define (list? something) (if (null? something) #t (if (null? something) #t Inductive Inductive (if (pair? something) Step! Step! ...)) (list? (cdr something)) #f) )) #11 #12

  3. Liberal Arts Trivia: Economics Liberal Arts Trivia: German Lit • This 1930 Tariff Act raised US tariffs on • This tragic closet play is considered by many to imported goods to record levels. Over 1000 US be one of the greatest works of German Economists signed a petition against it, and literature. It centers on a man who makes a after it passed many other contributed pact with the Devil in exchange for knowledge increased their tariffs in retribution. US in his quest to discover the essence of life exports and imports dropped by half and many (“was die Welt im Innersten zusammenhält”) view this Act as a major catalyst for the Great The man's name officially means “Lucky” in Depression. Latin, but now has negative connotations. #13 #14 member Definition of member • Write a function member that takes two (define (member elt lst) arguments: an element and a list. It returns #f (if (null? lst) if the list does not contain the element. #f ;; empty list contains nothing Otherwise it returns the sublist starting with (if (eq? elt (car lst)) that element. lst ;; we found it! – (member 2 (list 1 2 3)) -> (2 3) (member elt (cdr lst))))) ;; keep looking – (member 5 (list 1 2 3)) -> #f – (member 1 (list 1 2 3)) -> (1 2 3) • Where is the base case? Where is the inductive step? – (member 3 (list 1 2 3)) -> (3) – (eq? 3 5) -> #f (eq? 2 2) -> #t #15 #16 sumlist Definition of sumlist • Write a procedure sumlist that takes as input a • And here it is ... list of numbers. It returns the sum (addition) (define (sumlist lst) of all of the elements of the list. It returns 0 (if (null? lst) for the empty list. 0 ;; base case – (sumlist (list 1 2 3)) -> 6 (+ (car lst) ;; else add current element – (sumlist null) -> 0 (sumlist (cdr lst))))) ;; to rest of list #17 #18

  4. intsto Definition of intsto ? • The function intsto takes a single non-negative (define (intsto x) integer as an argument. It produces a list of all (if (< x 1) of the integers between 1 and its argument. null ;; base case – (intsto 3) -> (1 2 3) (cons ;; else make a list – (intsto 7) -> (1 2 3 4 5 6 7) x ;; list contains x – (intsto 0) -> null (intsto (- x 1))))) ;; and recursive result • What's wrong? #19 #20 Correct Definition of intsto Higher-Order Functions: map (define (intsto x) • The map function takes two arguments: a work function and a list. It applies the work function (if (< x 1) to every element of the list in order and null ;; base case returns a list of the result. (append ;; else make a list – (map sqrt (list 9 16 36)) -> (3 4 6) (intsto (- x 1)) ;; recursive result – (map square (list 1 2 3)) -> (1 4 9) (list x)))) ;; followed by x – (map abs (list 2 -3 4)) -> (2 3 4) – (map string-length (list “I” “Claudius”)) -> (1 8) – (map sqrt null) -> null • Huzzah! #21 #22 Mission Impossible: Write map Definition of map • You can do it! • Let's look in detail: – (map square (list 1 2 3)) -> (1 4 9) (define (map work-fun lst) – (map abs (list 2 -3 4)) -> (2 3 4) (if (null? lst) – (map sqrt null) -> null null ;; base case (cons ;; else make a list (work-fun (car lst)) ;; first part of result (map work-fun (cdr lst))))) ;; rest o'result #23 #24

  5. Liberal Arts Trivia: Philosophy Liberal Arts Trivia: Norse Myth • This branch of philosophy deals with the • In Norse Mythology, this god is associated with theory, nature and scope of knowledge. Key light and beauty. His mother made every questions include “what is knowledge?”, “how object on earth vow never to harm him, but is knowledge acquired?”, “what do people she did not ask mistletoe. The other gods know?”, “how do we know what we know?”, made a new pastime of hurling objects at him “what is the relationship between truth and and watching them bounce off. The trickster belief?”. Loki heard of this, fashioned a spear from mistletoe and had it thrown a him, with fatal results. #25 #26 Liberal Arts Trivia: Music Using map to get iteration • This musical instrument of the brass family • In C or Java: produces sound when the player's vibrating lips for (x=1 ; x <= 5 ; x=x+1) { cause the air column inside the instrument to display(x*x); vibrate. It is usually characterized by a } // output: 1 4 9 16 25 telescopic slide with which the player varies • Recall that we have intsto: the length of the tube to change the pitch. – (intsto 3) -> (1 2 3) Glenn Miller, famous for his “big band” and – (intsto 7) -> (1 2 3 4 5 6 7) songs like In the Mood and Chattanooga Choo • How can map and intsto to simulate for ? Choo , played this instrument. #27 #28 Using map to get iteration filter • In C or Java: • The filter function takes two arguments: a predicate and a list. A predicate is a function for (x=1 ; x <= 5 ; x=x+1) { that returns true or false. Filter returns the display(x*x); sublist consisting of those elements that satisfy } // output: 1 4 9 16 25 the predicate. • Recall that we have intsto: – (filter is-odd? (list 1 2 3 4)) -> (1 3) – (intsto 3) -> (1 2 3) – (filter null? (list 1 null null “hi”)) -> (null null) • Then we can do: – (filter (lambda (x) (< x 5)) (list 1 9 2 0)) -> (1 2 0) (map (lambda (x) (display (square x))) (intsto 5)) – (filter null? (list “susan” “b” “anthony”)) -> null – (filter is-odd? null) -> null Expect me on tests or extra credit later! #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