Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: Rules of Evaluation CS200: Computer Science

Similar presentations


Presentation on theme: "Lecture 3: Rules of Evaluation CS200: Computer Science"— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans
Lecture 3: Rules of Evaluation CS200: Computer Science University of Virginia Computer Science David Evans

2 Menu Unresolved Questions from Lecture 2 Evaluation Procedures
Origami Programming 23 January 2002 CS 200 Spring 2002

3 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

4 Primitives Means of Combination Means of Abstraction
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

5 Primitives Means of Combination Means of Abstraction
Pages in Revised5 Report on the Algorithmic Language Scheme Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 Means of Combination Expressions Program structure Means of Abstraction Definitions 48 pages total (includes formal specification and examples) 23 January 2002 CS 200 Spring 2002

6 Primitives Means of Combination Means of Abstraction
Pages in Revised5 Report on the Algorithmic Language Scheme Pages in C++ Language Specification (1998) Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 Means of Combination Expressions Program structure Means of Abstraction Definitions 48 pages total (includes formal specification and examples) 23 January 2002 CS 200 Spring 2002

7 Primitives Means of Combination Means of Abstraction
Pages in Revised5 Report on the Algorithmic Language Scheme Pages in C++ Language Specification (1998) Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 356 30 10 Means of Combination Expressions Program structure Expressions, Statements Program Structure 197 35 Means of Abstraction 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

8 Evaluation

9 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

10 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

11 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

12 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

13 Evaluation Rule 3: Application
If the expression is an application: 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. (expression0 expression1 expression2 … ) 23 January 2002 CS 200 Spring 2002

14 Rules for Application If the procedure to apply is a primitive, just do it. 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

15 Making Procedures lambda means “make a procedure” Expression ::=
(lambda (Parameters) Expression) Parameters ::= Parameters ::= Name Parameters 23 January 2002 CS 200 Spring 2002

16 Lambda Example: Tautology Function
make a procedure with no parameters with body #t (lambda () #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

17 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

18 A Squarish Example (define square (lambda (x) (* x x))) (square 4)
Its an application, use evaluation Rule 3: 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. 23 January 2002 CS 200 Spring 2002

19 (lambda (x) (* x x)) (define square (lambda (x) (* x x))) (square 4)
Its an application, use evaluation Rule 3: 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

20 (lambda (x) (* x x)) 4 (define square (lambda (x) (* x x))) (square 4)
Its an application, use evaluation Rule 3: 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 self-evaluating 4 23 January 2002 CS 200 Spring 2002

21 (lambda (x) (* x x)) (define square (lambda (x) (* x x))) (square 4)
Its an application, use evaluation Rule 3: 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 (lambda (x) (* x x)) to 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

22 (lambda (x) (* x x) ) Apply: square 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

23 (lambda (x) (* x x) ) (* 4 4) Apply: square 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. (* 4 4) 23 January 2002 CS 200 Spring 2002

24 (* 4 4) To be continued… Next time… evaluating 23 January 2002
CS 200 Spring 2002

25 Charge PS1 Due Friday Staffed Lab Hours Tonight Reading for next week:
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


Download ppt "Lecture 3: Rules of Evaluation CS200: Computer Science"

Similar presentations


Ads by Google