20-CS-4003-001 Organization of Programming Languages Fall 2018
Structured and Unstructured Flow

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Structured Alternatives to goto


  (define mult
    (lambda (l)
      (let ((result (call/cc (lambda (k) (set! halt k) '()))))
           (if (number? result)
                    (lambda (l)
                      (if (null? l)
                          (if (zero? (car l))
                              (halt 0)
                              (* (car l) (multiply (cdr l))))))))
                 (multiply l))))))

  (mult '(5 4 6 7 0 6 7))


   import java.util.*;

   class b {
      int multiply (Vector  v) throws Exception {
         if (v.isEmpty()) return 1;
         int n = v.get(0);
         if (n == 0) throw new Exception();
         return n * multiply(v);

   public class a {
      public static void main (String args[]) {
         Vector  v = new Vector  ();
         try {
            b b = new b();
         } catch (Exception e) {
 -  Scheme features call-with-current-continuation for making long jumps in code and time. Instead of a single stack, Scheme employs a heap - a tree of execution paths - to support this feature. When a continuation is "grabbed" and the point at which this happens is popped off the stack, the full stack path to that point remains. Calling the continuation returns control to that point but with a new value as specified by the user. The example to the left will perform no multiplies because finding a 0 in the input list invokes a continuation that was "grabbed" before the multiply was invoked.

Java has break and continue statements for control. It also has Exceptions which operate in a manner similar to that of call/cc in Scheme except that continuations are not saved (hence return points must never be popped from the system stack). The example to the left mimicks the Scheme code to the left using the Exception class.