Class 8: Recursing on Lists David Evans cs1120 Fall 2009.

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.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Class 6: Programming with Data David Evans cs1120 Fall 2009.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
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.
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)))
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
Plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Class 5: Constructing Procedures David Evans cs1120 Fall 2009.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
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 19: Environments.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
Class 7: List Procedures David Evans cs1120 Fall 2009.
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.
CS 550 Programming Languages Jeremy Johnson
Class 11: Two-argument recursion
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
CS 326 Programming Languages, Concepts and Implementation
Expanded Recursive Diagrams OCAML rapid tour, day 2
CS 270 Math Foundations of CS Jeremy Johnson
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 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 22: P = NP? CS200: Computer Science University of Virginia
The Metacircular Evaluator (Continued)
Lecture 9: The Great Lambda Tree of Knowledge and Power
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lisp.
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

Class 8: Recursing on Lists David Evans cs1120 Fall 2009

Menu Quiz Comments List Procedures – is-list? (Exercise 5.11) – list-sum  list-product, deep-list-sum Generalizing list procedures find-closest Monday: GEB Chapter 5 2

Quiz Comments About 2/3 of you have read the GEB reading – I really hope everyone reads this! – We’ll talk about it in Monday’s class Everyone should know the definition of a List (but only about ½ did) A List is either: – null – or, a Pair whose second part is a List 3 This definition is very important: all of our List procedures depend on it!

Common, Incorrect List Definition A List is a Pair whose second part is either null or a List. 4 If this is our List definition, there is no way to make a list with no elements.

Class Pace 5 (write-in)

Book Comments Many people want answers to the exercises (13 people mentioned this) – We will do some in class – If you ask, I’m happy to provide hints/answers or comments on your answers More diagrams, examples Editing problems: please do send me anything specific you notice! 6

is-list? 7 Define a procedure is-list? that takes one input and outputs true if the input is a List, and false otherwise. (is-list? (list 1 2 3))  true (is-list? (cons 1 (cons 2 null)))  true (is-list? null)  true (is-list? 3)  false (is-list? (cons 1 (cons 2 3)))  false

is-list?: Easy Way 8 (define is-list? list?) This is exactly what the built-in list? procedure does.

is-list? without using list? 9 null?: Value  Boolean outputs true if input is null pair?: Value  Boolean outputs true if input is a Pair (define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false))) (define (is-list? p) (or (null? p) (and (pair? p) (is-list? (cdr p)))))

list-sum Define a procedure, list-sum, that takes a list of numbers as input and outputs the sum of the numbers in the input list. 10 (list-sum (list 1 2 3))  6 (list-sum null)  0

list-sum 11 (define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p))))) Okay, what about list-product?

deep-list-sum 12 (define (deep-list-sum p) (if (null? p) 0 (+ (if (list? (car p)) (deep-list-sum (car p)) (car p)) (deep-list-sum (cdr p))))) Okay, what about list-product?

Tracing deep-list-sum 13 (require (lib "trace.ss")) (trace deep-list-sum) Use trace to see entrances and exits of procedure applications. |(deep-list-sum ((1 2) 3)) | (deep-list-sum (1 2)) | |(deep-list-sum (2)) | | (deep-list-sum ()) | | 0 | |2 | 3 | (deep-list-sum (3)) | |(deep-list-sum ()) | |0 | 3 |6 6

list-product 14 (define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p))))) Okay, what about list-length?

list-length 15 (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

Comparing List Procedures 16 (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p))))) (define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p))))) (define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p))))) (define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

Base Cases 17 (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p))))) (define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p))))) (define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p))))) (define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

Recursive Calls 18 (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p))))) (define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p))))) (define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p))))) (define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false))) What does each do with the car of the list?

find-closest-number Define a procedure find-closest-number that takes two inputs, a goal number, and a list of numbers, and produces the number in the list numbers list that is closest to goal: > (find-closest-number 150 (list )) 157 > (find-closest-number 12 (list )) 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?

Finding the Closest Strategy: If the first number is closer than the closest number of the rest of the numbers, use the first number. Otherwise, use the closet number of the rest of the numbers.

Optimistic Function (define (find-closest goal numbers) (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, something you can already solve. If there is only one number, that is the best match.

(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))))) (car numbers) (find-closest-number goal (cdr numbers)))) Same as before The Base Case

Testing > (find-closest-number 150 (list )) 157 > (find-closest-number 0 (list 1)) 1 > (find-closest-number 0 (list )) first: expects argument of type ; given () (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))))) (car numbers) (find-closest-number goal (cdr numbers))))

Charge Problem Set 2: Due Monday – Help hours Sunday 6-8:30 in Olsson 001 Monday: Even more Recursiveness! – GEB Chapter 5 26