A real life example of continuations: I have a secretary. I need her to xerox twelvel cover letters for the job fairs this weekend. I can either send her to xerox them, bring them back and sign them, or teach her to forge my signature. EVERY function can be written in tail recursive form. Generally, fn foo(a) = f(foo(g(a))) becomes fn foo'(a, cont) = foo(g(a), fn x => cont(f(x)) ) | foo'(basecase, cont) = cont basecase fn foo2 (a) = foo(a, fn x => x) foo(a) => foo'(a, fn x => x) => foo'(g(a), fn x => f(x)) => foo'(g(g(a)), fn x => f(f(x))) => ..... => f(f(f(g(g(g(x)))))) Proving correctness: Must show foo'(a, cont) = cont(foo(a)) for all A Example: fun map (g, nil) = nil | map (g, h::t) = g(h)::map(g, t) foo == map a == h::t basecase == nil f == ::t g == g(#1 a) Tail Recursive, continuation passing form: fun map2 (g, nil, cont) = cont nil | map2 (g, (h::t), cont) = map2(g, t, fn x => cont(g(h)::x)) - map2 (fn x:int => x * x, [1,2,3,4,5], fn x => x); val it = [1,4,9,16,25] : int list Proof: show that map2(g, h::t, cont) == cont(map(g, h::t)) base case: h::t = nil map2(g, nil, cont) => cont nil map(g,nil) => nil Done Inductive case: assume: map2(g, t, cont) == cont(map(g, t)) show: map2(g, h::t, cont') == cont'(map(g, h::t)) map2(g,h::t,cont') => map2(g, t, fn x => cont'(g(h)::x)) let cont'' = fn x => cont'(g(h)::x) == map2(g, t, cont'') => cont''(map(g,t)) => cont'(g(h)::map(g,t)) => cont'(map(g,h::t)) Hence, map2(g, h::t, cont) == cont(map(g, h::t)) Hence map2(g, h::t, fn x => x) = map(g, h::t)