Chapter 11 introduces the concept of continuation, which helps understand such notions as tail call, exceptions and exception handling, execution stack, and backtracking. A continuation is an explicit representation of the rest of the computation. Usually this is implicit in a program: after executing one statement, the computation will continue with the next statement; when returning from a method, the computation will continue where the method was called; and so on. Making the continuation explicit has the advantage that we can ignore it (and so model abnormal termination), and that we can have more than one continuation (and so model exception handling and backtracking).
Loading two continuation-based interpreters for a functional language with exceptions:
dotnet fsi Contfun.fsopen Contfun;;
eval1 ex1 [];; eval1 ex2 [("n", Int 10)];;
#q;;Loading two continuation-based interpreters for an imperative language with exceptions:
dotnet fsi Contimp.fsopen Contimp;;
run1 ex1;;run1 ex2;;run2 ex3;;
#q;;Loading a continuation-based interpreter for micro-Icon, a language in which an expression can have multiple results:
dotnet fsi Icon.fsopen Icon;;
run ex1;;run ex2;;run ex3and;;run ex3or;;
#q;;Compile and run a Java implementation of factorial in continuation-passing style:
cd Factorial/
javac Factorial.javajava Factorial 10javac Factorial2.javajava Factorial2 10Compile and run a C# implementation of factorial in continuation-passing style:
cd Factorial/
dotnet build Factorial.csprojdotnet run 10Compile and run example illustrating longjmp and setjmp in C (under Linux and MacOS):
We recommend using clang as compiler. See Platform
Dependencies on how to install across platforms.
clang testlongjmp.c -o testlongjmp./testlongjmp 10./testlongjmp 11