Docsity

Lecture Slides for Programming with Data | CS 1120, Study notes of Computer Science

Toggle sidebar
bg1
Greg Humphreys
http://www.cs.virginia.edu/~humper
CS150: Computer Science
University of Virginia
Computer Science
Lecture 4:
Programming
with Data
CS150 Fall 2005: Lecture 4: Programming with Data 2
Menu
• Evaluation Example
• Programming with Data
CS150 Fall 2005: Lecture 4: Programming with Data 3
(square 4) Eval Rule 3a: Application
square 4
Eval Rule 2: Names
(lambda (x) (* x x))
Eval Rule 1: Primitive
4Eval Rule 3b: Application
Apply Rule 2: Compound Application
(* 4 4)Eval Rule 3a: Application
*44
Eval Rule 1: Primitive Eval Rule 1: Primitive
4
4
#<primitive:*> Eval Rule 3b: Application
Apply Rule 1: Primitive Application
16
CS150 Fall 2005: Lecture 4: Programming with Data 4
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?
CS150 Fall 2005: Lecture 4: Programming with Data 5
History of Scheme
• Scheme [Guy Steele & Gerry Sussman, 1975]
Guy Steele co-designed Scheme and created the
first Scheme interpreter for his 4th 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
CS150 Fall 2005: Lecture 4: Programming with Data 6
LISP
“Lots of Insipid Silly Parentheses”
“LISt Processing language”
Lists are pretty important – hard to
write a useful Scheme program
without them.
pf3
pf4

Partial preview of the text

Download Lecture Slides for Programming with Data | CS 1120 and more Study notes Computer Science in PDF only on Docsity!

Greg Humphreys http://www.cs.virginia.edu/~humper CS150: Computer Science University of Virginia Computer Science

Lecture 4:

Programming

with Data

CS150 Fall 2005: Lecture 4: Programming with Data 2

Menu

• Evaluation Example

• Programming with Data

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

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?

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

LISP

“ L ots of I nsipid S illy P arentheses”

“ LIS t P rocessing language”

Lists are pretty important – hard to write a useful Scheme program without them.

CS150 Fall 2005: Lecture 4: Programming with Data 7

Making Lists

CS150 Fall 2005: Lecture 4: Programming with Data 8

Making a Pair

( cons 1 2) (1. 2)

cons cons tructs a pair

CS150 Fall 2005: Lecture 4: Programming with Data (^9)

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

car cdr CS150 Fall 2005: Lecture 4: Programming with Data (^10)

Why “car” and “cdr”?

  • Original (1950s) LISP on IBM 704
    • Stored cons pairs in memory registers
    • car = “ C ontents of the A ddress part of the R egister”
    • cdr = “ C ontents of the D ecrement part of the R egister” (“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)

Pairs are fine, but how do

we make threesomes?

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)))

CS150 Fall 2005: Lecture 4: Programming with Data 19

More List Examples

> (list? (cons 1 (cons 2 null)))

#t

> (car (cons 1 (cons 2 null)))

> (cdr (cons 1 (cons 2 null)))

CS150 Fall 2005: Lecture 4: Programming with Data 20

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

Problem Set 2:

Programming with Data

  • Representing a card car cdr Pair of rank (Ace) and suit (Spades) CS150 Fall 2005: Lecture 4: Programming with Data (^22)

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

Charge

  • You know everything you need for PS
  • 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!