(continued from Class 3)
Expression ::= ProcedureExpression
ProcedureExpression ::= (lambda (Parameters) Expression)
Parameters ::= Name Parameters
Evaluation Rule 4: A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.
(lambda () true)
(lambda (x) (* x x))
(lambda (a) (lambda (b) (+ a b)))
((lambda () true) 1120) ((lambda (x) (+ x 1000)) 120) ((lambda (a) (lambda (b) (+ a b))) 5) (((lambda (a) (lambda (b) (+ a b))) 5) 6)
Expression ::= IfExpression
IfExpression ::= (if ExpressionPredicate ExpressionConsequent ExpressionAlternate)
Evaluation Rule 5: If. To evaluate an if expression, (a) evaluate the ___________________; then, (b) if the value of the predicate expression is ___________ then the value of the if expression is the value of the alternate expression; otherwise, the value of the if expression is the value of the _____________________________________.
(if false false true) (if (> 4 3) 4 3) ((lambda (a b) (if (> a b) a b)) 5 6)
Show how the Rules of Evaluation would evaluate:
(define square (lambda (x) (* x x))) (square 4)
You will need more space for this and it will be very tedious, but it is worth doing once. Of course, you know the final value, but the important thing is to understand how following the Scheme evaluation rules steps will produce that value. You should be confident that you can determine the value of any Scheme expression just by following the evaluation rules systematically.
Evaluation Rule 3a does not say in what order the subexpressions should be evaluated in. For example, we could evaluate them left to right, or right to left, or in any other order. Does it ever matter in which order the subexpressions of an application are evaluated? (Tough question, but try to think of a Scheme expression where it would make a difference.)
You must be logged in to post a comment.