Transcript slides

Lecture 3:
Rules of
Evaluation
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/~evans
Menu
• Unresolved Questions
from Lecture 2
• Evaluation
• Procedures
• Origami Programming
23 January 2002
CS 200 Spring 2002
2
Language Elements
When learning a foreign language, which of
primitives, means of combination or means of
abstraction is hardest to learn?
• Primitives
– There are a lot of them
– Learning the real meaning is hard
• Means of Combination
– They are complex
– But, all languages have similar ones!
• Chomsky Universal Grammar
• Means of Abstraction
– Few of these, but tricky to learn differences across languages
23 January 2002
CS 200 Spring 2002
3
Pages in Revised5 Report
on the Algorithmic
Language Scheme
Primitives
Means of
Combination
Means of
Abstraction
48 pages total (includes
formal specification and
examples)
23 January 2002
CS 200 Spring 2002
4
Pages in Revised5 Report
on the Algorithmic
Language Scheme
Primitives
Means of
Combination
Standard Procedures
Primitive expressions
Identifiers, numerals
Expressions
Program structure
Definitions
Means of
Abstraction
18
2
1
2
2
½
48 pages total (includes
formal specification and
examples)
23 January 2002
CS 200 Spring 2002
5
Pages in Revised5 Report Pages in C++ Language
on the Algorithmic
Specification (1998)
Language Scheme
Primitives
Means of
Combination
Standard Procedures
Primitive expressions
Identifiers, numerals
Expressions
Program structure
Definitions
Means of
Abstraction
18
2
1
2
2
½
48 pages total (includes
formal specification and
examples)
23 January 2002
CS 200 Spring 2002
6
Pages in Revised5 Report Pages in C++ Language
on the Algorithmic
Specification (1998)
Language Scheme
Primitives
Means of
Combination
Means of
Abstraction
Standard Procedures
Primitive expressions
Identifiers, numerals
18 Standard Procedures
2 Primitive expressions
1 Identifiers, numerals
356
30
10
Expressions
Program structure
2 Expressions, Statements
2 Program Structure
197
35
Definitions
½ Declarations, Classes
173
48 pages total (includes
formal specification and
examples)
776 pages total (includes no
formal specification or
examples)
Core language issues list has 313 items!
23 January 2002
CS 200 Spring 2002
7
Evaluation
Expressions and Values
• (Almost) every expression has a value
– Have you seen any expressions that don’t
have values?
• When an expression with a value is
evaluated, its value is produced
23 January 2002
CS 200 Spring 2002
9
DrScheme
When you press “Execute”, DrScheme will evaluate all
the expressions in the Definitions window
When you type return in the Interactions
Window, DrScheme evaluates the expression
at the last prompt, and prints its value in blue.
23 January 2002
CS 200 Spring 2002
10
Evaluation Rule 1: Primitives
If the expression is a primitive,
it is self-evaluating.
>2
2
> #t
#t
>+
#<primitive:+>
23 January 2002
CS 200 Spring 2002
11
Evaluation Rule 2: Names
If the expression is a name, it evaluates
to the value associated with that name.
> (define two 2)
> two
2
23 January 2002
CS 200 Spring 2002
12
Evaluation Rule 3: Application
3. If the expression is an application:
a) Evaluate all the subexpressions of the
combination (in any order)
b) Apply the value of the first subexpression to
the values of all the other subexpressions.
(expression0 expression1 expression2 … )
23 January 2002
CS 200 Spring 2002
13
Rules for Application
1. If the procedure to apply is a primitive,
just do it.
2. If the procedure is a compound
procedure, evaluate the body of the
procedure with each formal parameter
replaced by the corresponding actual
argument expression value.
23 January 2002
CS 200 Spring 2002
14
Making Procedures
• lambda means “make a procedure”
Expression ::=
(lambda (Parameters) Expression)
Parameters ::=
Parameters ::= Name Parameters
23 January 2002
CS 200 Spring 2002
15
Lambda Example: Tautology Function
(lambda
()
#t)
make a procedure
with no parameters
with body #t
> ((lambda () #t) 200)
#<procedure>: expects no arguments, given 1: 200
> ((lambda () #t))
#t
> ((lambda (x) x) 200)
200
23 January 2002
CS 200 Spring 2002
16
You’ve Already Used Lambda!
(define (closer-color?
sample color1 color2)
Expr)
is a shortcut for:
(define closer-color?
(lambda (sample color1 color2)
Expr))
23 January 2002
CS 200 Spring 2002
17
A Squarish Example
(define square (lambda (x) (* x x)))
(square 4)
Its an application, use evaluation Rule 3:
a) Evaluate all the subexpressions of the
combination (in any order)
b) Apply the value of the first subexpression to
the values of all the other subexpressions.
23 January 2002
CS 200 Spring 2002
18
(define square (lambda (x) (* x x)))
(square 4)
Its an application, use evaluation Rule 3:
a)
b)
Evaluate all the subexpressions of the combination (in any order)
Apply the value of the first subexpression to the values of all the other
subexpressions.
Evaluate: square
It’s a name, use evaluation Rule 2:
If the expression is a name, evaluate the
expression associated with that name.
(lambda (x) (* x x))
23 January 2002
CS 200 Spring 2002
19
(define square (lambda (x) (* x x)))
(square 4)
Its an application, use evaluation Rule 3:
a)
b)
Evaluate all the subexpressions of the combination (in any order)
Apply the value of the first subexpression to the values of all the other
subexpressions.
Evaluate: square
(lambda (x) (* x x))
Evaluate: 4
Rule 1: If the expression is a primitive, it is selfevaluating
4
23 January 2002
CS 200 Spring 2002
20
(define square (lambda (x) (* x x)))
(square 4)
Its an application, use evaluation Rule 3:
a)
b)
Evaluate all the subexpressions of the combination (in any order)
Apply the value of the first subexpression to the values of all the other
subexpressions.
Apply: square
to
(lambda (x) (* x x))
4
Application Rule 2. If the procedure is a compound procedure,
evaluate the body of the procedure with each formal parameter
replaced by the corresponding actual argument expression value.
23 January 2002
CS 200 Spring 2002
21
Apply: square
(lambda (x) (* x x) )
to
4
Body of the procedure
Application Rule 2. If the procedure
is a compound procedure, evaluate
the body of the procedure with each
formal parameter replaced by the
corresponding actual argument
expression value.
23 January 2002
CS 200 Spring 2002
22
Apply: square
(lambda (x) (* x x) )
to
4
(* 4 4)
23 January 2002
Body of the procedure
Application Rule 2. If the
procedure is a compound
procedure,
evaluate the body of the
procedure with each formal
parameter replaced by the
corresponding actual argument
expression value.
CS 200 Spring 2002
23
To be continued…
• Next time… evaluating
(* 4 4)
23 January 2002
CS 200 Spring 2002
24
Charge
• PS1 Due Friday
• Staffed Lab Hours Tonight
– 5-9 in Cocke
– 6-9 in Small Hall
• Reading for next week:
– GEB Chapter 5
– SICP Section 1.2
23 January 2002
CS 200 Spring 2002
25