David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.

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.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
David Evans CS200: Computer Science University of Virginia Computer Science Class 30: Models of Computation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
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 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.
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 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 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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
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.
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.
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.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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 Lecture 5: Recursion Beware the Lizards!
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 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: 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.
Lecture 13: Cost of Sorts CS150: Computer Science
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
Class 34: Models of Computation CS200: Computer Science
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 CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr

3 February 2003CS 200 Spring Menu History of Scheme –LISP Lists List Recursion

3 February 2003CS 200 Spring Confusion Is Good! It means you are learning new ways of thinking.

3 February 2003CS 200 Spring History of Scheme Scheme [1975] –Guy Steele and Gerry Sussman –Originally “Schemer” –“Conniver” [1973] and “Planner” [1967] Based on LISP –John McCarthy (late 1950s) Based on Lambda Calculus –Alonzo Church (1930s) –Last few lectures in course

3 February 2003CS 200 Spring LISP “Lots of Insipid Silly Parentheses” “LISt Processing language” Lists are pretty important – hard to write a useful Scheme program without them.

3 February 2003CS 200 Spring Making Lists

3 February 2003CS 200 Spring Making a Pair > (cons 1 2) (1. 2) 1 2 cons constructs a pair

3 February 2003CS 200 Spring Splitting a Pair > (car (cons 1 2)) 1 > (cdr (cons 1 2)) cons car cdr car extracts first part of a pair cdr extracts second part of a pair

3 February 2003CS 200 Spring Why “ car ” and “ cdr ”? Original (1950s) LISP on IBM 704 –Stored cons pairs in memory registers –car = “Contents of the Address part of the Register” –cdr = “Contents of the Decrement part of the Register” (“could-er”) Doesn’t matters unless you have an IBM 704 Think of them as first and rest (define first car) (define rest cdr)

3 February 2003CS 200 Spring Implementing cons, car and cdr Using PS2: (define cons make-point) (define car x-of-point) (define cdr y-of-point) As we implemented make-point, etc.: (define (cons a b) (lambda (w) (if (w) a b))) (define (car pair) (pair #t) (define (cdr pair) (pair #f)

3 February 2003CS 200 Spring Pairs are fine, but how do we make threesomes?

3 February 2003CS 200 Spring Threesome? (define (threesome a b c) (lambda (w) (if (= w 0) a (if (= w 1) b c)))) (define (first t) (t 0)) (define (second t) (t 1)) (define (third t) (t 2)) Is there a better way of thinking about our triple?

3 February 2003CS 200 Spring Triple A triple is just a pair where one of the parts is a pair! (define (triple a b c) (cons a (cons b c))) (define (t-first t) (car t)) (define (t-second t) (car (cdr t))) (define (t-third t) (cdr (cdr t)))

3 February 2003CS 200 Spring Quadruple A quadruple is a pair where the second part is a triple (define (quadruple a b c d) (cons a (triple b c d))) (define (q-first q) (car q)) (define (q-second q) (t-first (cdr t))) (define (q-third t) (t-second (cdr t))) (define (q-fourth t) (t-third (cdr t)))

3 February 2003CS 200 Spring Multuples A quintuple is a pair where the second part is a quadruple A sextuple is a pair where the second part is a quintuple A septuple is a pair where the second part is a sextuple An octuple is group of octupi A list (any length tuple) is a pair where the second part is a …?

3 February 2003CS 200 Spring Lists List ::= (cons Element List ) A list is a pair where the second part is a list. One little problem: how do we stop? This only allows infinitely long lists!

3 February 2003CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN From Lecture 6

3 February 2003CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::=

3 February 2003CS 200 Spring Lists List ::= (cons Element List ) List ::= A list is either: a pair where the second part is a list or, empty It’s hard to write this!

3 February 2003CS 200 Spring Null List ::= (cons Element List ) List ::= A list is either: a pair where the second part is a list or, empty ( null ) null

3 February 2003CS 200 Spring List Examples > null () > (cons 1 null) (1) > (list? null) #t > (list? (cons 1 2)) #f > (list? (cons 1 null)) #t

3 February 2003CS 200 Spring More List Examples > (list? (cons 1 (cons 2 null))) #t > (car (cons 1 (cons 2 null))) 1 > (cdr (cons 1 (cons 2 null))) (2)

3 February 2003CS 200 Spring List Recursion

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

3 February 2003CS 200 Spring Defining Recursive Procedures 1.Be optimistic. –Assume you can solve it. –If you could, how would you solve a bigger problem. 2.Think of the simplest version of the problem, something you can already solve. 3.Combine them to solve the problem. on Lists Be very optimistic For lists, assume we can solve it for the cdr For lists, the simplest version is usually null (the zero-length list) Combine something on the car of the list with the recursive evaluation on the cdr. Remember to test null? before using car or cdr.

3 February 2003CS 200 Spring Defining Sumlist (define sumlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) 0 + > (sumlist (list )) 10 > (sumlist null) 0

3 February 2003CS 200 Spring Defining Productlist (define productlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) 1 * > (productlist (list )) 24 > (productlist null) 1

3 February 2003CS 200 Spring Defining Length (define length (lambda (lst) (if (null? lst) ( (car lst) (length (cdr lst)) 0 + > (length (list )) 4 > (length null) 0 1

3 February 2003CS 200 Spring Defining insertl (define insertl (lambda (lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval)))))

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

3 February 2003CS 200 Spring Charge Next Time: lots more things you can do with lists (including the peg board puzzle!) PS3 Out Today –Use lists to make fractals –You have seen everything you need for it after today –Due next week Wednesday