The process, a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative GCD procedure given above. Suppose we were to interpret this procedure using Normal-order evaluation, as discussed on section 1.1.5. (The normal-order-evaluation rule for if was described in Exercise 1.5.) Using the substitution method (for normal order), illustrate the process generated in evaluating (GCD 206) and indicate The remainder operations that is actually performed. How many remainder operations is actually performed in the Normal-order evaluation of (GCD 206 40)? In the Applicative-order evaluation?
Using applicative-order:
GCD (206, 40)
=GCD (40, 6)
=GCD (6,4)
=GCD (4,2)
=GCD (2,0)
A total of 4 recursion, so the use of the Applicative-order,remainder function was called 4 times.
With Normal-order
GCD (206, 40)
=GCD (+, R (206,40))//here to determine whether R (206,40) is zero, one remainder calculation
=GCD (
R (206,40),
R (+, R (206,40))
)//Determine if R (206,40) is zero, with two remainder
=GCD (
R (206, 40),
R
R (206,40),
R (R (206, 40))
)//judgment is zero, there are 4 times remainder calculation
)
=GCD (
R
R (206,40),
R (R (206, 40))
),//return result, 4 calculations
R
R (206, 40),
R
R (206,40),
R (R (206, 40))
),
)//judgment is zero, there are 7 times remainder calculation
)
A total of 1+2+4+7=14 times remainder call calculation
Exercise 1.20 greatest common divisor algorithm