Transcript slides

Lecture 4:
Evaluation
Rules
Recursion
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/~evans
Menu
• Review Rules of Evaluation
– Finish example from Wednesday
• PS1/PS2
• Recursive Procedures
25 January 2002
CS 200 Spring 2002
2
Evaluation Rules
1. Primitives. If the expression is a > 2
2
primitive, it is self-evaluating.
2. Names. If the expression is a name, it
evaluates to the value associated with
that name.
> (define womble 4)
> womble
4
25 January 2002
CS 200 Spring 2002
3
Evaluation Rules
3. Application. 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.
Apply Rule 1: Primitives. If the procedure to apply is a
primitive, just do it.
Apply Rule 2: Compounds. 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.
25 January 2002
CS 200 Spring 2002
4
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.
25 January 2002
CS 200 Spring 2002
5
(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))
25 January 2002
CS 200 Spring 2002
6
(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
25 January 2002
CS 200 Spring 2002
7
(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.
25 January 2002
CS 200 Spring 2002
8
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.
25 January 2002
CS 200 Spring 2002
9
Apply: square
(lambda (x) (* x x) )
to
4
(* 4 4)
25 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
10
Evaluate: (* 4 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.
Evaluate: *
Rule 1: If the expression is a primitive, it is selfevaluating
<primitive:*>
Evaluate: 4
Rule 1: If the expression is a primitive, …
4
Evaluate: 4
Rule 1: If the expression is a primitive, …
4
25 January 2002
CS 200 Spring 2002
11
Evaluate: (* 4 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.
Apply: <primitive:*> to 4 4
16
25 January 2002
Application Rule 1: If the procedure
to apply is a primitive, just do it.
CS 200 Spring 2002
12
(square 4)
Eval Rule 3a: Application
square
Eval Rule 2: Names
4
Eval Rule 1: Primitive
(lambda (x) (* x x))
4
Eval Rule 3b: Application
Apply Rule 2: Compound Application
(* 4 4)
4
*
Eval Rule 3a: Application
4
Eval Rule 1: Primitive
Eval Rule 1: Primitive
#<primitive:*> 4
4
Eval Rule 3b: Application
Apply Rule 1: Primitive Application
16
25 January 2002
CS 200 Spring 2002
13
Eval and Apply
are defined in
terms of each
other.
Without Eval,
there would be
no Apply,
Without Apply
there would be
no Eval!
25 January 2002
Eval
Apply
CS 200 Spring 2002
14
All of Scheme
• Once you understand Eval and Apply, you
can understand all Scheme programs!
• Except:
– We have special Eval rules for special forms
(like if)
Evaluation Rule 4: If it is a special form, do
something special.
25 January 2002
CS 200 Spring 2002
15
Evaluating Special Forms
• Eval 4-if. If the expression is
(if Expression0 Expression1 Expression2)
evaluate Expression0. If it evaluates to #f, the
value of the if expression is the value of
Expression1. Otherwise, the value of the if
expression is the value of Expression2.
• Eval 4-lambda. Lambda expressions selfevaluate. (Do not do anything until it is
applied.)
25 January 2002
CS 200 Spring 2002
16
More Special Forms
• Eval 4-define. If the expression is
(define Name Expression)
associate the Expression with Name.
• Eval 4-begin. If the expression is
(begin Expression0 Expression1 …
Expressionk)
evaluate all the sub-expressions. The
value of the begin expression is the value
of Expressionk.
25 January 2002
CS 200 Spring 2002
17
Really All of Scheme
Now, you really know all of
Scheme!
The rest of this course is
about Computer Science.
25 January 2002
CS 200 Spring 2002
18
Problem Sets
25 January 2002
CS 200 Spring 2002
19
Problem Set 1
• Meant to be a bit overwhelming
– Question 4 was accidentally more overwhelming then it
was supposed to be
• Lots of new ideas, lots of reading, lots of provided
code
• Not much code to write yourself
• By the middle of this course, you will be able to
write the whole photomosaic program yourself!
• There is no deadline on making posters – produce
a cool photomosaic and I’ll print a big poster
25 January 2002
CS 200 Spring 2002
20
Problem Set 2
• Unlike PS1, you should be able to understand all the
provided code
– Except, don’t worry about the trigonometry
• Main ideas: recursion, procedures
– We will cover them more in class next week…
– …but, don’t wait until everything is covered to do the
problem set
• Later problem sets will expect you to write more
code on your own, but not introduce as many new
ideas as PS1.
• Ideally, PS8 would be “Do something cool using
things you have learned from this class.” (But it will
be a bit more specific for real.)
25 January 2002
CS 200 Spring 2002
21
Gosper Curves
Gosper Curve Level 0
Gosper Curve Level 1
Gosper Curve Level 50
25 January 2002
CS 200 Spring 2002
22
> (show-gosper smiley 0)
> (show-gosper smiley 7)
Curves are functions. We can transform them to make new curves.
25 January 2002
CS 200 Spring 2002
23
Recursive Procedures
25 January 2002
CS 200 Spring 2002
24
Defining Recursive Procedures
1. Be optimistic.
– Assume you can solve it.
– If you could, how would you solve a bigger
problem.
2. Think of the simplest version of the
problem, something you can already
solve. (This is the base case.)
3. Combine them to solve the problem.
25 January 2002
CS 200 Spring 2002
25
Example
Define (find-closest-number goal numbers)
that evaluates to the number in the list
numbers list that is closest to goal:
> (find-closest-number 200 (list 101 110 120 201 340 588))
201
> (find-closest-number 12 (list 1 11 21))
11
> (find-closest-number 12 (list 95))
95
25 January 2002
CS 200 Spring 2002
26
Find Closest Number
Be optimistic!
Assume you can define:
(find-closest-number goal numbers)
that finds the closest number to goal from
the list of numbers.
What if there is one more number?
Can you write a function that finds the
closest number to match from newnumber and numbers?
25 January 2002
CS 200 Spring 2002
27
Find Best Match
Strategy:
If the new number is better, than the best
match with the other number, use the new
number. Otherwise, use the best match of
the other numbers.
25 January 2002
CS 200 Spring 2002
28
Optimistic Function
(define (find-closest-number-extra
goal new-number numbers)
(if (< (abs (- new-number goal))
(abs (- (find-closest-number goal
numbers)
goal)))
new-number
(find-closest-number goal numbers)))
25 January 2002
CS 200 Spring 2002
29
Defining Recursive Procedures
2. Think of the simplest version of the
problem, something you can already
solve.
If there is only one number, that is the
best match.
25 January 2002
CS 200 Spring 2002
30
The Base Case
Same as before
(define (find-closest-number-extra
goal new-number numbers)
(if (empty? numbers)
new-number ;;; No others, new-number is best
(if (< (abs (- new-number goal))
(abs (- (find-closest-number
goal numbers)
goal)))
new-number
(find-closest-number goal numbers))))
25 January 2002
CS 200 Spring 2002
31
The Base Case
(define (find-closest-number-extra
goal new-number numbers)
(if (empty? numbers)
new-number ;;; No others, new-number is
;;; best
(if (< (abs (- new-number goal))
(abs (- (find-closest-number
But…we haven’t
goal numbers)
defined this yet!
goal)))
new-number
(find-closest-number goal numbers))))
25 January 2002
CS 200 Spring 2002
32
find-closest-number
(define (find-closest-number goal numbers)
(find-closest-number-extra goal
(first numbers)
(rest numbers)))
25 January 2002
CS 200 Spring 2002
33
Testing
> (find-closest-number 200
(list 101 110 120 201 340 588))
201
> (find-closest-number 0 (list 1))
1
> (find-closest-number 0 (list ))
car: expects argument of type <pair>; given ()
25 January 2002
CS 200 Spring 2002
34
Seen Anything Like This?
(define (find-best-match sample
remaining-tiles color-comparator)
(if (null? remaining-tiles)
Base Case
#f
(pick-better-match
sample
(first remaining-tiles)
(find-best-match sample (rest remaining-tiles)
color-comparator)
color-comparator)))
25 January 2002
CS 200 Spring 2002
35
Charge
• Both these examples have funny base
cases
– What to do when there are no numbers or no
tiles is not clear
• Many problems can be solved this way:
– More examples in GEB Ch 5
– More examples in class Monday
– More examples on PS2 and PS3
• Reading assignments for the weekend
• Start on PS2 early
25 January 2002
CS 200 Spring 2002
36