Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't seem to wrap my mind around call/cc in Scheme

Does anyone have a good guide as to how it works? Something with visual aids would be nice, every guide I've come across all seem to say the same thing I need a fresh take on it.

like image 996
Brian Avatar asked Mar 14 '11 17:03

Brian


People also ask

What is CC user call?

call/cc (call with current continuation) is a universal control operator (well-known from the programming language Scheme) that captures the current continuation as a first-class object and pass it as an argument to another continuation.

How does Scheme continuation feature work?

In a Scheme implementation, the caller's state is saved in a record on the garbage-collected heap, called a partial continuation. It's called a continuation because it says how to resume the caller when we return into it--i.e., how to continue the computation when control returns.

What is call CC in racket?

call/cc. The (main) way to capture a continuation in Racket is call-with-current-continuation or call/cc in short. call/cc is a procedure of one argument that must be a procedure (call it "p") of one argument. This argument is the continuation (which is a procedure of one argument, right?).


2 Answers

Here's the diagram that was left on our CS lab's whiteboard. So you're going to fetch some apples, and you grab a continuation before you begin. You wander through the forest, collecting apples, when at the end you apply your continuation on your apples. Suddenly, you find yourself where you were before you went into the forest, except with all of your apples.

call/cc

(display
  (call/cc (lambda (k)
             (begin
               (call-with-forest
                 (lambda (f)
                   (k (collect-apples f))))
               (get-eaten-by-a-bear)))))

=> some apples (and you're not eaten by a bear)

I think a Bar Mitzvah and buried gold might have been involved.

like image 124
erjiang Avatar answered Nov 06 '22 08:11

erjiang


Have a look at the continuation part of PLAI -- it's very "practical oriented", and it uses a "black-hole" visualization for continuations that can help you understand it.

like image 32
Eli Barzilay Avatar answered Nov 06 '22 08:11

Eli Barzilay