Expressing Imperative Programs Functionally (4 of 4)
lDefine
the functions according to the state transformations in boxes.
x <= n
yes
no
input n
output a
fac(n)
f1(a, n, x)
f2(a, n, x)
f1(a, n, x)
This arc is regarded as the same as the one above.
fac(n)
= f1(1, n, 1);
f1(a, n, x) = x <= n ?
f2(a, n, x)
: a;
f2(a, n, x) = f1(a*x, n, x+1);
This
is called ÒMcCarthyÕs transformation
principle in the
text.