DOC PREVIEW
UVA CS 150 - Lecture 4: Programming with Data

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1David Evanshttp://www.cs.virginia.edu/evansCS150: Computer ScienceUniversity of VirginiaComputer ScienceLecture 4: Programming with Data2CS150 Fall 2005: Lecture 4: Programming with DataMenu• Evaluation Question from Monday• Programming with Data• Take Pictures3CS150 Fall 2005: Lecture 4: Programming with Data(square 4)Eval Rule 3a: Applicationsquare4Eval Rule 2: Names(lambda (x) (* x x))Eval Rule 1: Primitive4Eval Rule 3b: ApplicationApply Rule 2: Compound Application(* 4 4)Eval Rule 3a: Application*44Eval Rule 1: Primitive Eval Rule 1: Primitive44#<primitive:*>Eval Rule 3b: ApplicationApply Rule 1: Primitive Application164CS150 Fall 2005: Lecture 4: Programming with DataWays to Design Programs1. 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?5CS150 Fall 2005: Lecture 4: Programming with DataHistory of Scheme• Scheme [Guy Steele & Gerry Sussman, 1975]Guy Steele co-designed Scheme and created the first Scheme interpreter for his 4thyear projectMore 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 course6CS150 Fall 2005: Lecture 4: Programming with DataLISP“Lots of Insipid Silly Parentheses”“LISt Processing language”Lists are pretty important – hard to write a useful Scheme program without them.27CS150 Fall 2005: Lecture 4: Programming with DataMaking Lists8CS150 Fall 2005: Lecture 4: Programming with DataMaking a Pair> (cons 1 2)(1 . 2)cons constructs a pair1 29CS150 Fall 2005: Lecture 4: Programming with DataSplitting a Pair> (car (cons 1 2))1> (cdr (cons 1 2))2car extracts first part of a paircdr extracts second part of a pair1 2carcdr10CS150 Fall 2005: Lecture 4: Programming with DataWhy “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)11CS150 Fall 2005: Lecture 4: Programming with DataPairs are fine, but how do we make threesomes?12CS150 Fall 2005: Lecture 4: Programming with DataTripleA 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)))313CS150 Fall 2005: Lecture 4: Programming with DataQuadrupleA 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)))14CS150 Fall 2005: Lecture 4: Programming with DataMultuples• 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 …?15CS150 Fall 2005: Lecture 4: Programming with DataListsList::= (consElement List)A listis a pair where the second part is a list.One big problem: how do we stop?This only allows infinitely long lists!16CS150 Fall 2005: Lecture 4: Programming with DataListsList::= (consElement List)List::=A listis either:a pair where the second part is a listor, emptyIt’s hard to write this!17CS150 Fall 2005: Lecture 4: Programming with DataNullList::= (consElement List)List::=A listis either:a pair where the second part is a listor, empty (null)null18CS150 Fall 2005: Lecture 4: Programming with DataList Examples> null()> (cons 1 null)(1)> (list? null)#t> (list? (cons 1 2))#f> (list? (cons 1 null))#t419CS150 Fall 2005: Lecture 4: Programming with DataMore List Examples> (list? (cons 1 (cons 2 null)))#t> (car (cons 1 (cons 2 null)))1> (cdr (cons 1 (cons 2 null)))(2)20CS150 Fall 2005: Lecture 4: Programming with DataRecap• A listis either:a pair where the second part is a listor 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 pair21CS150 Fall 2005: Lecture 4: Programming with DataProblem Set 2:Programming with Data• Representing a cardcarcdrPair of rank (Ace) and suit (Spades)22CS150 Fall 2005: Lecture 4: Programming with DataProblem Set 2:Programming with Data• Representing a card: (cons <rank> <suit>)• Representing a hand(list (make-card Ace clubs)(make-card King clubs)(make-card Queen clubs)(make-card Jack clubs)(make-card 10 clubs)23CS150 Fall 2005: Lecture 4: Programming with DataCharge• 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


View Full Document

UVA CS 150 - Lecture 4: Programming with Data

Documents in this Course
Objects

Objects

6 pages

Load more
Download Lecture 4: Programming with Data
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 4: Programming with Data and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 4: Programming with Data 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?