Forum: Ruby nice explanation of call/cc

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-02-12 12:43
(Received via mailing list)

In functional languages exceptions tend to be handled a little
differently. Whereas C++ has a throw keyword, there is no equivalent
in Scheme, say. Instead
the call-with-current-continuation function acts like catch. The
argument to call-with-current-continuation is a function (lambda
(throw) ...) and the argument to this function (itself a function),
here called throw, does the throwing. For example consider the scheme

    (lambda (throw) (+ 1 (throw 7))))

[  a = callcc {|c| c[7] + 1} in ruby - martin ]

The result is 7, not 8. The 7 is thrown 'past' the (+ 1 ...) and is
caught by the surrounding call-with-current-continuation. The
important difference from C++ is that if any subexpression is to throw
some value, it must be 'handed' a suitable throw function.
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 Pit Capitain (Guest)
on 2007-02-12 17:11
(Received via mailing list)
Martin DeMello schrieb:
>> From

Martin, thanks for the link. Its nice that we (still) can easily
implement both versions of the code in Ruby, using catch/throw and

Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-02-13 17:20
(Received via mailing list)
I picked up a book on Scheme and seeing call/cc there very much
surprised me. I had seen it Ruby before, I guess it came directly from
Scheme. Anyway there's a neat chapter in that book on
continuation-passing style, which was much different than I expected
it to be. Unfortunately I just relocated and the book's packed in a
box, but the basic idea revolved around that aspect of call/cc. It
didn't actually seem that different from the sequences of methods with
dots style that allows you to feed the output of one method into the
next method, but the syntax was very different (and probably there
were some significant differences to the internals as well).
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-02-13 17:28
(Received via mailing list)
Continuation passing style is actually most used as a compilation
stage in several functional languages  - the program is first
transformed to cps and that is then used to generate executable code.
I don't think I've seen any language encourage it as an actual coding
style. I agree, it's a very interesting approach, and well worth
reading up on.

This topic is locked and can not be replied to.