reading quiz list recursion practice examples
play

Reading quiz List Recursion: Practice & Examples #1 #2 - PDF document

Reading quiz List Recursion: Practice & Examples #1 #2 One-Slide Summary Outline Writing recursive functions that operate on Review: Procedure Problem Solving recursive data structures takes practice . There Review: list,


  1. Reading quiz List Recursion: Practice & Examples #1 #2 One-Slide Summary Outline • Writing recursive functions that operate on • Review: Procedure Problem Solving recursive data structures takes practice . There • Review: list, cons, car, cdr are standard approaches to such problems. • list? • list? , member , sumlist , intsto , map and filter • member are all important recursive functions that • sumlist operate on lists. You should know what they do • intsto and how to write them. • map • DrScheme can trace the execution of a recursive function to make it easier to • filter understand. • Tracing #3 #4 Teamwork! How To Write A Procedure • PS2 Partners Posted • Find out what it is supposed to do. – Meet @ lab hours? – What are the inputs ? What types of values? • PS1 Written Grades – What is the output ? A number? Procedure? List? Posted • Think about some example inputs and outputs – Holding Fee • Define your procedure – Pick them up – More on this next slide • Read the book! • Test your procedure #5 #6

  2. Defining A Procedure Procedure Skeleton • Be optimistic! • The vast majority of recursive functions look • Base case : Think of the simplest input to the like this: problem that you know the answer to. – For number inputs, this is often zero. (define (my-procedure my-input) – For list inputs, this is often the empty list (null). (if ( is-base-case? my-input) • Recursive step : Think of how you would solve the problem in terms of a smaller input. Do ( handle-base-case my-input) part of the work now, then make a recursive ( combine ( first-part-of my-input) call to handle the rest. (my-procedure ( rest-of my-input))))) – For numbers, this usually involves subtracting 1. – For lists, this usually involves cdr. #7 #8 Pairs and Lists More Power Needed! • cons makes a pair of two things – (cons 1 2) --> (1 . 2) – (pair? (cons 1 2)) --> #t • car and cdr get the first and second part – (car (cons “a” “b”)) --> “a” – (cdr (cons “y” “z”)) --> “z” • A list is either null or a pair where the second element is also a list – (cons 1 (cons 2 (cons 3 null))) --> (1 2 3) – (list 1 2 3) --> (1 2 3) – (null? (list 1 2)) --> #f – (append (list 1 2) (list 3 4)) -> (1 2 3 4) #9 #10 list? list? Hint • The list? function takes a single argument and • Here's a hint: returns #t if that argument is a list, #f otherwise. (define (list? something) – Recall: a list is either null or a pair where the second element is a list (if (null? something) #t – (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? #11 #12

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

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

  5. Definition of map Liberal Arts Trivia: Philosophy • Let's look in detail: • This branch of philosophy deals with the theory, nature and scope of knowledge. Key (define (map work-fun lst) questions include “what is knowledge?”, “how (if (null? lst) is knowledge acquired?”, “what do people null ;; base case know?”, “how do we know what we know?”, (cons ;; else make a list “what is the relationship between truth and belief?”. (work-fun (car lst)) ;; first part of result (map work-fun (cdr lst))))) ;; rest o'result #25 #26 Liberal Arts Trivia: Norse Myth Liberal Arts Trivia: Music • In Norse Mythology, this god is associated with • This musical instrument of the brass family light and beauty. His mother made every produces sound when the player's vibrating lips object on earth vow never to harm him, but cause the air column inside the instrument to she did not ask mistletoe. The other gods vibrate. It is usually characterized by a made a new pastime of hurling objects at him telescopic slide with which the player varies and watching them bounce off. The trickster the length of the tube to change the pitch. Loki heard of this, fashioned a spear from Glenn Miller, famous for his “big band” and mistletoe and had it thrown a him, with fatal songs like In the Mood and Chattanooga Choo results. Choo , played this instrument. #27 #28 Using map to get iteration Using map to get iteration • In C or Java: • In C or Java: for (x=1 ; x <= 5 ; x=x+1) { for (x=1 ; x <= 5 ; x=x+1) { display(x*x); display(x*x); } // output: 1 4 9 16 25 } // output: 1 4 9 16 25 • Recall that we have intsto: • Recall that we have intsto: – (intsto 3) -> (1 2 3) – (intsto 3) -> (1 2 3) – (intsto 7) -> (1 2 3 4 5 6 7) • Then we can do: • How can map and intsto to simulate for ? (map (lambda (x) (display (square x))) (intsto 5)) 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