For the life of me, I can't understand continuations. I think the problem stems from the fact that I don't understand is what they are for. All the examples that I've found in books or online are very trivial. They make me wonder, why anyone would even want continuations?
Here's a typical impractical example, from TSPL, which I believe is quite recognized book on the subject. In english, they describe the continuation as "what to do" with the result of a computation. OK, that's sort of understandable.
Then, the second example given:
(call/cc
(lambda (k)
(* 5 (k 4)))) => 4
How does this make any sense?? k
isn't even defined! How can this code be evaluated, when (k 4)
can't even be computed? Not to mention, how does call/cc
know to rip out the argument 4
to the inner most expression and return it? What happens to (* 5 ..
?? If this outermost expression is discarded, why even write it?
Then, a "less" trivial example stated is how to use call/cc
to provide a nonlocal exit from a recursion. That sounds like flow control directive, ie like break/return
in an imperative language, and not a computation.
And what is the purpose of going through these motions? If somebody needs the result of computation, why not just store it and recall later, as needed.
转载于:https://stackoverflow.com/questions/16529475/scheme-continuations-for-dummies