Class 7: List Procedures 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.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
New Mexico Computer Science For All
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 40: Computing with Glue and Photons.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
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.
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.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
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.
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.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
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 CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
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 CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
David Evans CS200: Computer Science University of Virginia Computer Science Class 26: Halting Problem It is plain at any.
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.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
CS 550 Programming Languages Jeremy Johnson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
CS 270 Math Foundations of CS Jeremy Johnson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
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.
Class 37: Making Lists, Numbers and Recursion from Glue Alone
Lecture 9: The Great Lambda Tree of Knowledge and Power
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture # , , , , מבוא מורחב.
Class 33: Learning to Count CS200: Computer Science
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lisp.
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

Class 7: List Procedures David Evans cs1120 Fall 2009

List Recap A List is either: (1) a Pair whose second part is a List or (2) null Pair primitives: (cons a b)Construct a pair (car pair)First part of a pair (cdr pair)Second part of a pair 2

List Examples > null () > (cons 1 null) (1) > (list? null) #t > (list? (cons 1 2)) #f > (list? (cons 1 null)) #t 3

More List Examples > (list? (cons 1 (cons 2 null))) #t > (car (cons 1 (cons 2 null))) 1 > (cdr (cons 1 (cons 2 null))) (2) 4

List Procedures Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures. Think of the simplest version of the problem, something you can already solve. This is the base case. For lists, this is usually when the list is null. Consider how you would solve the problem using the result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list. 5

list-trues Define a procedure that takes as input a list, and produces as output the number of non- false values in the list. (list-trues null)  0 (list-trues (list 1 2 3))  3 (list-trues (list false (list 2 3 4)))  1 6

list-trues Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures. Think of the simplest version of the problem, something you can already solve. This is the base case. For lists, this is usually when the list is null. Consider how you would solve the problem using the result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list. 7

list-trues Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures. Think of the simplest version of the problem, something you can already solve. This is the base case. For lists, this is usually when the list is null. Consider how you would solve the problem using the result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list. 8 (define (list-trues p) (if (null? p) 0 ))

list-trues Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures. Think of the simplest version of the problem, something you can already solve. This is the base case. For lists, this is usually when the list is null. Consider how you would solve the problem using the result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list. 9 (define (list-trues p) (if (null? p) 0 (+ (if (car p) 1 0) (list-trues (cdr p)))))

Quiz 10

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. 11 (list-sum (list 1 2 3))  6 (list-sum null)  0

list-length Define a procedure, list-length, that takes a list as input and outputs the number of elements in the input list. 12 (list-length (list 1 2 3))  3 (list-length (list 1 (list 2 3)))  2 (list-length null)  0

Charge We’ll repeat the Quiz on Friday if it seems too few people have done the readings well Problem Set 2: Due Monday – It is much longer than PS1, don’t wait to get started – Help hours tonight in Olsson 001 Friday: Recursive Procedures Practice 13