You are on a desert island. There are ravenous lions and the bomb is about to go off. Your life now depends on teaching a complete ass fuck idiot what continuations are and how/when/where to use call/cc. Go!
The current status of /prog/ is such that I can post a dumb thread like this and actually improve the overall quality of the board, so no, I will not go back to /g/.
Name:
Anonymous2010-01-04 20:56
>>1
Give bomb to as fuck idiot and point him in the direction of the lions; make sure to move as far as possible to the other side of the island until the kaboom. Use whatever doesn't survive as food.
Ass fuck idiot fails to reach enlightenment in time, and transforms into the snake. Press 'R' to restart.
Name:
Anonymous2010-01-04 21:06
Say you're in the kitchen in front of the refrigerator, thinking about a
sandwitch. You take a continuation right there and stick it in your
pocket. Then you get some turkey and bread out of the refrigerator and
make yourself a sandwitch, which is now sitting on the counter. You
invoke the continuation in your pocket, and you find yourself standing
in front of the refrigerator again, thinking about a sandwitch. But
fortunately, there's a sandwitch on the counter, and all the materials
used to make it are gone. So you eat it. :-)
Name:
Anonymous2010-01-04 21:33
Continuations are like burritos.
First off, call/cc is a function and you can call it, like a burrito delivery service.
When you call it, you need to say where to deliver the burrito. If you tell them to deliver it to you in jail next week, you can escape from jail by eating the burrito which will warp you back to the phone. However because they'll find out if you're gone too long, you order a burrito from inside jail, and tell them to put it in the warphole created by the other burrito. Then when you're back at the phone you also have a burrito, which you can use to go back to jail before they count heads again. If you're smart, you keep copies of the burritos around so you can live an exciting double live.
Name:
Anonymous2010-01-04 21:43
I'll give it my best dummies explanation, but I will probably fail.
A continuation is basically the work left to do when we have the result of an expression. If we have an expression (+ 3 (+ 2 1)), then when we are evaluating the subexpression (+ 2 1) we still have to add 3 to get the result. call/cc makes the continuation of the current expression available to us as a function.
Example.
(+ 3 (call/cc (lambda (k) (+ 2 1))))
At this point, the continuation k, is equivalent to a function (lambda (k) (+ 3 k)) and if k is called at any point in the body of the lambda, it will return immediately. If it is not called in the body of the lambda, it is implicitly called at the end. Thus the above expression is equivalent to
(+ 3 (call/cc (lambda (k) (k (+ 2 1)))))
With scheme, continuations are first class so you could pass them into/out of functions, store them away, use them in data structures, etc. I've seen several interesting uses of continuations, but one of the nicest was non-blind backtracking. Other uses are documented in the paper 'call with current continuation patterns'
Two further examples.
Say we wanted to multiply all the numbers of the list, but if the list contained a negative number we wanted to return that instead. We could do (define (multiply listofnums)
(call/cc (lambda (escape)
(foldl (lambda (x y)
(if (negative? x)
(escape x)
(* x y)))
1
listofnums))))
>(multiply '(1 2 3 4 5 6))
720
>(multiply '(1 2 -4 4 5 6))
-4
Another example is to use it for backtracking, the following is not efficient and you should really use iterators instead.
>>6 I LOVE YOU!I LOVE YOUR POST!I READ IT FIVE TIMES!KEEP POSTING!
Name:
Anonymous2010-01-05 3:45
Remember that that only start "back" from a point, not evaluate-and-rebind in place and hop back. Things will be reexecuted which makes it hard to use with stateful code. It quite easy to hand-evaluate call/cc's closures - just replace the entirety of the call/cc call with the argument and work on from the point of the first binding.