I can't seem to think about calling / cc in Scheme

Does anyone have any good guidance on how this works? Something with visual aids would be nice, every guide I came across seems to say the same thing I need for it to accept it.

+6
continuations scheme call callcc
source share
5 answers

Here is a diagram left on our CS computer board. So you are going to pick up some apples, and you will get a sequel before you start. You wander through the woods picking apples when in the end you apply your sequel to your apples. Suddenly, you will be where you were before heading into the forest, with the exception of all 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 bar mitzvah and buried gold could be involved.

+10
source share

Look at the PLAI continuation part - it is very “practical oriented”, and it uses black hole visualization for continuations that can help you understand this.

+5
source share

There is no shortcut in the / cc training call. Read the chapters in Schema Programming Language or Teach Schema in Fixnum Days .

0
source share

I found that it helps to visualize the call stack. When evaluating an expression, keep track of the call stack at each step. (See for example http://4.flowsnake.org/archives/602 ). At first, this may not be intuitive, because in most languages, call stacks are implicit; you cannot manipulate it directly.

Now think of continuing as a function that saves the call stack. When this function is called (with an X value), it restores the stored call stack, then passes X to it.

0
source share

I never like the visual representation of the / cc call, as I can't reflect it back in the code (yes, bad imagination);)

In any case, I think it’s easier to start not with the / cc call, but with the / ec call (to continue), if you are already familiar with exceptions in other languages.

Here is some code that should evaluate the value:

 (lambda (x) (/ 1 x)) 

What if x is equal to "0"? In other languages, we can exclude, what about the circuit? We can leave him too!

 (lambda (x) (call/ec (cont) (if (= x 0) (cont "Oh noes!") (/ 1 x)))) 

call / ec (as well as call / cc) works like a "try" here. In imperative languages, you can easily jump out of a function by simply returning a value or excluding throwing. In functionality you cannot jump, you have to evaluate something. And the call / * comes to the rescue. What it does is an expression under "call / ec" as a function (this is called "cont" in my case) with one argument. When this function is called, it replaces the WHOLE / * argument with it.

So, when (cont "Oh noes!") Replaces the string (call/ec (cont) (if (= x 0) (cont "Oh noes!") (/ 1 x))) with "Oh noes!" .

call / cc and call / ec are almost equal to each other, except for a simpler implementation. This allows you to just jump up, while cc can be shot down from the outside.

0
source share

All Articles