In addition to continuations that represent the normal


Continuations

In addition to continuations that represent the "normal" continued execution of a program, we can use continuations in place of exceptions. For example, consider the following function f that raises an exception when the argument x is too small:

exception Too Small;

fun f(x) = if x<> then raise Too Small else x/2; (1 + f(y)) handle Too Small => 0;

If we use continuations, then f could be written as afunction with two extra arguments, one for normal exit and the other for "exceptional exit," to be called if the argument is too small:

fun f(x, k normal, k exn) = if x normal(x/2); f(y, (fn z => 1+z), (fn () => 0));

(a) Explain why the ?nal expressions in each program fragment will have the same value for any value of y.

(b) Why would tail call optimization be helpful when we use the second style of programming instead of exceptions?

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: In addition to continuations that represent the normal
Reference No:- TGS01269948

Expected delivery within 24 Hours