# notes05 - Normal vs applicative order To illustrate this...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Normal vs. applicative order. To illustrate this point we use a modiﬁed Scheme evaluator that lets us show the process of applicative or normal order evaluation. We deﬁne functions using def instead of define. Then, we can evaluate expressions using (applic (...)) for applicative order or (normal (...)) for normal order. (Never mind how this modiﬁed evaluator itself works! Just take it on faith and concentrate on the results that it shows you.) In the printed results, something like (* 2 3) ==> 6 indicates the ultimate invocation of a primitive function. But (f 5 9) ----> (+ (g 5) 9) indicates the substitution of actual arguments into the body of a function deﬁned with def. (Of course, whether actual argument values or actual argument expressions are substituted depends on whether you used applic or normal, respectively.) > > f > g > (load "lectures/1.1/order.scm") (def (f a b) (+ (g a) b)) ; define a function (def (g x) (* 3 x)) ; another one (applic (f (+ 2 3) (- 15 6))) ; show applicative-order evaluation (f (+ 2 3) (- 15 6)) (+ 2 3) ==> 5 (- 15 6) ==> 9 (f 5 9) ----> (+ (g 5) 9) (g 5) ----> (* 3 5) ==> 15 (+ 15 9) ==> 24 24 > (normal (f (+ 2 3) (- 15 6))) ; show normal-order evaluation (f (+ 2 3) (- 15 6)) ----> (+ (g (+ 2 3)) (- 15 6)) (g (+ 2 3)) ----> (* 3 (+ 2 3)) (+ 2 3) ==> 5 (* 3 5) ==> 15 (- 15 6) ==> 9 (+ 15 9) ==> 24 24 (continued on next page) ; Same result, different process. 271 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online