Lecture 7: List Recursion CS200: Computer Science

Slides:



Advertisements
Similar presentations
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
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 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.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
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.
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 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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
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 12: Decrypting Work Circle Fractal.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
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 Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
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 CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
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.
CS 152: Programming Language Paradigms February 12 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 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 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
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 21: Inheritance CS200: Computer Science University of Virginia
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 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture #8 מבוא מורחב.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Class 33: Learning to Count CS200: Computer Science
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
CS 403: Programming Languages
Lisp.
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 7: List Recursion CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Confusion Is Good! It means you are learning new ways of thinking. 30 January 2004 CS 200 Spring 2004

Confusing Things Passing procedures as parameters All the functions you have dealt with since kindergarten only took values as parameters Procedures that produce new procedures All the functions you have dealt with since kindergarten only produced values as results Recursive Procedures Defining things in terms of themselves requires a leap of faith! 30 January 2004 CS 200 Spring 2004

Review A list is either: a pair where the second part is a list or null (note: book uses nil) Pair primitives: (cons a b) Construct a pair <a, b> (car pair) First part of a pair (cdr pair) Second part of a pair 30 January 2004 CS 200 Spring 2004

Defining Recursive Procedures Be optimistic. Assume you can solve it. If you could, how would you solve a bigger problem. Think of the simplest version of the problem, something you can already solve. (This is the base case.) Combine them to solve the problem. 30 January 2004 CS 200 Spring 2004

Defining Recursive Procedures on Lists Be optimistic. Assume you can solve it. If you could, how would you solve a bigger problem. Think of the simplest version of the problem, something you can already solve. Combine them to solve the problem. Be very optimistic Assume we can solve it for the cdr The simplest version is usually null Combine something on the car of the list with the recursive evaluation on the cdr. 30 January 2004 CS 200 Spring 2004

Gauss Sum (gauss-sum n) = 1 + 2 + … + n (define (gauss-sum n) (if (= n 0) (+ n (gauss-sum (- n 1))))) 30 January 2004 CS 200 Spring 2004

Defining Sumlist + (define sumlist (lambda (lst) (if (null? lst) > (sumlist (list 1 2 3 4)) 10 > (sumlist null) (define sumlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) + 30 January 2004 CS 200 Spring 2004

Defining Productlist 1 * (define productlist (lambda (lst) > (productlist (list 1 2 3 4)) 24 > (productlist null) 1 (define productlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) 1 * 30 January 2004 CS 200 Spring 2004

Defining Length + 1 (define length (lambda (lst) (if (null? lst) > (length (list 1 2 3 4)) 4 > (length null) (define length (lambda (lst) (if (null? lst) ( (car lst) (length (cdr lst)) + 1 30 January 2004 CS 200 Spring 2004

Defining insertl (define insertl (lambda (lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval))))) 30 January 2004 CS 200 Spring 2004

Definitions (define (sumlist lst) (insertl lst + 0)) (define insertl (lambda (lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval))))) (define (sumlist lst) (insertl lst + 0)) (define (productlist lst) (insertl lst * 1)) (define (length lst) (insertl lst (lambda (head rest) (+ 1 rest)) 0)) 30 January 2004 CS 200 Spring 2004

Charge PS2 Due Monday More list recursion practice Monday (& PS3) Take advantage of remaining lab hours: now – 4:30, Sunday: 3-5:30 Don’t freak out if you can’t get everything! It is possible to get a green star without completing question 5 and 6 (but you should include your attempts and describe what you understand) More list recursion practice Monday (& PS3) 30 January 2004 CS 200 Spring 2004