David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
Class 6: Programming with Data David Evans cs1120 Fall 2009.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Class 30: Models of Computation.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
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 Lecture 16: Quicker Sorting.
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.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
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.
Functional Programming in Scheme
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 CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
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.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
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
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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 8: Cons car cdr sdr wdr.
(Thunking about Thunks)
Lecture 4: Metacircles Eval Apply David Evans
Functional Programming
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 27: Universal Turing Machines CS150: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
CS 326 Programming Languages, Concepts and Implementation
Lecture 37: A Universal Computer
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
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 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Class 26: Modeling Computing CS150: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data

2 CS150 Fall 2005: Lecture 4: Programming with Data Menu Evaluation Question from Monday Programming with Data Take Pictures

3 CS150 Fall 2005: Lecture 4: Programming with Data (square 4) Eval Rule 3a: Application square 4 Eval Rule 2: Names (lambda (x) (* x x)) Eval Rule 1: Primitive 4 Eval Rule 3b: Application Apply Rule 2: Compound Application (* 4 4) Eval Rule 3a: Application * 4 4 Eval Rule 1: Primitive 4 4 # Eval Rule 3b: Application Apply Rule 1: Primitive Application 16

4 CS150 Fall 2005: Lecture 4: Programming with Data Ways to Design Programs 1.Think about what you want to do, and turn that into code. 2.Think about what you need to represent, and design your code around that. Which is better?

5 CS150 Fall 2005: Lecture 4: Programming with Data History of Scheme Scheme [Guy Steele & Gerry Sussman, 1975] Guy Steele co-designed Scheme and created the first Scheme interpreter for his 4 th year project More recently, Steele specified Java [1995] –“Conniver” [1973] and “Planner” [1967] Based on LISP [John McCarthy, 1958] –Based on Lambda Calculus –Alonzo Church, 1930s –Last few lectures in course

6 CS150 Fall 2005: Lecture 4: Programming with Data LISP “Lots of Insipid Silly Parentheses” “LISt Processing language” Lists are pretty important – hard to write a useful Scheme program without them.

7 CS150 Fall 2005: Lecture 4: Programming with Data Making Lists

8 CS150 Fall 2005: Lecture 4: Programming with Data Making a Pair > (cons 1 2) (1. 2) cons constructs a pair 12

9 CS150 Fall 2005: Lecture 4: Programming with Data Splitting a Pair > (car (cons 1 2)) 1 > (cdr (cons 1 2)) 2 car extracts first part of a pair cdr extracts second part of a pair 12 car cdr

10 CS150 Fall 2005: Lecture 4: Programming with Data 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 matter unless you have an IBM 704 Think of them as first and rest (define first car) (define rest cdr) (The DrScheme “Pretty Big” language already defines these, but they are not part of standard Scheme)

11 CS150 Fall 2005: Lecture 4: Programming with Data Pairs are fine, but how do we make threesomes?

12 CS150 Fall 2005: Lecture 4: Programming with Data 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)))

13 CS150 Fall 2005: Lecture 4: Programming with Data 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)))

14 CS150 Fall 2005: Lecture 4: Programming with Data 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 ? is a pair where the second part is a …?

15 CS150 Fall 2005: Lecture 4: Programming with Data Lists List ::= (cons Element List) A list is a pair where the second part is a list. One big problem: how do we stop? This only allows infinitely long lists!

16 CS150 Fall 2005: Lecture 4: Programming with Data 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!

17 CS150 Fall 2005: Lecture 4: Programming with Data Null List ::= (cons Element List) List ::= A list is either: a pair where the second part is a list or, empty (null) null

18 CS150 Fall 2005: Lecture 4: Programming with Data List Examples > null () > (cons 1 null) (1) > (list? null) #t > (list? (cons 1 2)) #f > (list? (cons 1 null)) #t

19 CS150 Fall 2005: Lecture 4: Programming with Data More List Examples > (list? (cons 1 (cons 2 null))) #t > (car (cons 1 (cons 2 null))) 1 > (cdr (cons 1 (cons 2 null))) (2)

20 CS150 Fall 2005: Lecture 4: Programming with Data Recap 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 (car pair)First part of a pair (cdr pair)Second part of a pair

21 CS150 Fall 2005: Lecture 4: Programming with Data Problem Set 2: Programming with Data Representing a card car cdr Pair of rank (Ace) and suit (Spades)

22 CS150 Fall 2005: Lecture 4: Programming with Data Problem Set 2: Programming with Data Representing a card: (cons ) Representing a hand (list (make-card Ace clubs) (make-card King clubs) (make-card Queen clubs) (make-card Jack clubs) (make-card 10 clubs)

23 CS150 Fall 2005: Lecture 4: Programming with Data Charge You know everything you need for PS2 Friday, next week - lots of examples of: –Programming with data –Programming with procedures –Recursive definitions But, if you understand the Scheme evaluation rules, you know it all already! Please don’t leave until I take your picture!