Lecture 8: Recursing Lists CS150: Computer Science

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 9: Mostly Not About Matter, Music, and Mayhem Richard Feynmans van Alan Alda playing.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 26: Printing and Stuff.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard Feynman’s.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Class 7: List Procedures David Evans cs1120 Fall 2009.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 8: Recursing Recursively Richard Feynman’s.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursion Beware the Lizards!
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
(Thunking about Thunks)
Lecture 4: Metacircles Eval Apply David Evans
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Lecture 7: List Recursion CS200: Computer Science
CS 326 Programming Languages, Concepts and Implementation
Halting Functions for Tree-Like Structures
Class 19: Think Globally, Mutate Locally CS150: Computer Science
6.001 SICP Data abstractions
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture #8 מבוא מורחב.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 26: The Metacircular Evaluator Eval Apply
topics mutable data structures
Lecture 9: The Great Lambda Tree of Knowledge and Power
6.001 SICP Data abstractions
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 2 מבוא מורחב.
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
CS 403: Programming Languages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 8: Recursing Lists CS150: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Defining Recursive Procedures Be optimistic! Assume you can solve it. Think of the simplest version of the problem, something you can already solve. (This is the base case.) Consider how you would solve an instance of the problem using the result for a slightly smaller instance. (recursive case) Combine them to solve the problem.

Defining Recursive Procedures on Lists Be very optimistic! Think of the simplest version of the problem (almost always null), something you can already solve. (base case) Consider how you would solve an instance of the problem using the result for a slightly smaller instance (the cdr of the list). (recursive case) Combine them to solve the problem.

list? (define (list? p) (if (null? p) #t (if (pair? p) (list? (cdr p))

To enable tracing: (require (lib "trace.ss")) Tracing list? To enable tracing: (require (lib "trace.ss")) > (trace list?) (list?) > (list? null) |(list? ()) |#t #t > (list? (list 1 2 3)) |(list? (1 2 3)) |(list? (2 3)) |(list? (3)) |(list? ()) |#t #t

sumlist (define (sumlist p) (if (null? p) (+ (car p) (sumlist (cdr p)))))

Tracing sumlist (define (sumlist p) (if (null? p) 0 (+ (car p) (sumlist (cdr p))))) > (trace +) > (sumlist (list 1 2 3 4)) |(+ 4 0) |4 |(+ 3 4) |7 |(+ 2 7) |9 |(+ 1 9) |10 10

map (define (map f p) (if (null? p) null (cons (f (car p)) (map f (cdr p)))))

Tracing map > (map (lambda (x) (* x 2)) (list 1 2 3)) |(map #<procedure> (1 2 3)) | (map #<procedure> (2 3)) | |(map #<procedure> (3)) | | (map #<procedure> ()) | | () | |(6) | (4 6) |(2 4 6) (2 4 6)

Similarities and Differences (define (map f p) (if (null? p) null (cons (f (car p)) (map f (cdr p))))) (define (sumlist p) (if (null? p) (+ (car p) (sumlist (cdr p))))) (define (list-cruncher ? ... ? lst) (if (null? lst) base result (combiner (car lst) (recursive-call ... (cdr lst))))

Similarities and Differences (define (map f p) (if (null? p) null (cons (f (car p)) (map f (cdr p))))) (define (sumlist p) (if (null? p) (+ (car p) (sumlist (cdr p))))) (define (list-cruncher ? ... ? lst) (if (null? lst) base result (combiner (car lst) (recursive-call ... (cdr lst))))

list-cruncher (define (list-cruncher base proc combiner lst) (if (null? lst) base (combiner (proc (car lst)) (list-cruncher base proc combiner (cdr lst))))) (define (sumlist p) (list-cruncher 0 (lambda (x) x) + p)) (define (map f p) (list-cruncher null f cons p))

Can list-cruncher crunch length? (define (list-cruncher base proc combiner lst) (if (null? lst) base (combiner (proc (car lst)) (list-cruncher base proc combiner (cdr lst))))) (define (length p) (if (null? p) 0 (+ 1 (length (cdr p)))))

Can list-cruncher crunch length? (define (list-cruncher base proc combiner lst) (if (null? lst) base (combiner (proc (car lst)) (list-cruncher base proc combiner (cdr lst))))) (define (length p) (if (null? p) 0 (+ 1 (length (cdr p))))) (define (length p) (list-cruncher 0 (lambda (x) 1) + p))

Can list-cruncher crunch list?? (define (list-cruncher base proc combiner lst) (if (null? lst) base (combiner (proc (car lst)) (list-cruncher base proc combiner (cdr lst))))) (define (list? p) (if (null? p) #t (if (pair? p) (list? (cdr p)) #f)))

list-or-not-cruncher (define (list-or-not-cruncher base nonlist proc combiner lst) (if (null? lst) base (if (not (pair? lst)) nonlist (combiner (proc (car lst)) (list-or-not-cruncher base nonlist proc combiner (cdr lst)))))) (define (list? p) (list-or-not-cruncher #t #f (lambda (x) x) (lambda (f r) r) p)) This works, but is not an elegant or simple way of defining list?!

find-closest-number Define find-closest-number, a procedure that takes two parameters, a goal and a list of numbers, and produces the number in the list numbers list that is closest to goal: > (find-closest-number 150 (list 101 110 120 157 340 588)) 157 > (find-closest-number 12 (list 1 11 21)) 11 > (find-closest-number 12 (list 95)) 95

Find Closest Number Be optimistic! Assume you can define: (find-closest-number goal numbers) that finds the closest number to goal from the list of numbers. What if there is one more number? Can you write a function that finds the closest number to match from new-number and numbers?

Find Closest Strategy: If the new number is better, than the best match with the other number, use the new number. Otherwise, use the best match of the other numbers.

Optimistic Function (define (find-closest goal-number numbers) ;; base case missing for now (if (< (abs (- goal (car numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) (car numbers) (find-closest-number goal (cdr numbers))))

Defining Recursive Procedures 2. Think of the simplest version of the problem (almost always null), something you can already solve. (base case) Is null the base case for find-closest-number?

The Base Case (define (find-closest-number goal numbers) (if (= 1 (length numbers)) (car numbers) (if (< (abs (- goal (first numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) (find-closest-number (cdr numbers))))

> (find-closest-number 150 (list 101 110 120 157 340 588)) 157 (define (find-closest-number goal numbers) (if (= 1 (length numbers)) (car numbers) (if (< (abs (- goal (car numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) > (find-closest-number 150 (list 101 110 120 157 340 588)) 157 > (find-closest-number 0 (list 1)) 1 > (find-closest-number 0 (list )) first: expects argument of type <non-empty list>; given ()

Generalizing find-closest-number How would we implement find-closest-number-without-going-over? What about find-closest-word? ... The “closeness” metric should be a procedure parameter

Charge Read GEB “Little Harmonic Labyrinth” and Chapter 5 before Monday’s class PS3 accepted until beginning of Monday’s class