[plt-scheme] Understanding continuations

From: Grant Rettke (grettke at acm.org)
Date: Wed Jul 30 17:50:44 EDT 2008

On Wed, Jul 23, 2008 at 5:50 AM, Anton van Straaten
<anton at appsolutions.com> wrote:
> As for the rest, yes, I think that modeling the whole thing in CPS is
> essential if you really want to understand what's going on when learning
> about this stuff.

Last night I sat down to try writing a "long running process" style
app using only CPS. Writing in in CPS was nice; it is so clear what is
happening.

Is the way that I modeled what is really happening? At this moment I'm
not sure how to map this to using call/cc.

#lang scheme

(define flag1 #f)
(define flag2 #f)

(define (a-process initial-k)
  (printf "Starting process~n")
  (let
      ([part1-fun
        (λ (part1-k) ; what to do next, may abort the process using this
          (printf "Checking flag 1~n")
          (if (not flag1)
              (let () (printf "Condition 1 not met, returning~n") (part1-k #f))
              (let ()
                (printf "Condition 1 met proceeding~n")
                (let ([part2-fun
                       (λ (part2-k) ; what to do next, may abort the
process using this
                         (printf "Checking flag 2~n")
                         (if (not flag2)
                             (let () (printf "Condition 2 not met,
returning~n") (part2-k #f))
                             (let ()
                               (printf "Condition 2 met proceeding~n")
                               (let
                                   ([part3-fun
                                     (λ (part3-k) ; what to do next,
process is finished, return its value using this
                                       (part3-k 42))])
                                 (set! a-process part3-fun) ; set
aprocess to be passing on 42
                                 (part3-fun part2-k)))))]) ; propagate
the continuation from part 2 to part 3, the final part
                  (set! a-process part2-fun) ; set aprocess to be
everything from the second check forward
                  (part2-fun part1-k)))))]) ; propagate the
continuation from part 1 to part 2
    (set! a-process part1-fun) ; set aprocess to be everything from
the first check forward
    (part1-fun initial-k))) ; propagate the continuation from the
entry point to part 1

(define (main)
  (printf "Calling for the first time~n")
  (a-process
   (λ (result1)
     (if result1
         (printf "It worked on the first try~n")
         (let ()
           (printf "Set flag1 #t~n")
           (set! flag1 #t)
           (a-process
            (λ (result2)
              (if result2
                  (printf "It worked on the second try~n")
                  (let ()
                    (printf "Set flag2 #t~n")
                    (set! flag2 #t)
                    (a-process
                     (λ (result3)
                       (printf "Done! It has to work here. Got: ~a~n" result3)
                       (a-process
                        (λ (final-result)
                          (printf "Calling again to be sure. Got:
~a~n" final-result))))))))))))))

(main)

Posted on the users mailing list.