From: Jim Weirich Date: 2003-08-07T13:03:03+09:00 Subject: Re: Why does Ruby have callcc? On Wed, 2003-08-06 at 19:12, Ben Giddings wrote: > Like Hal said, I meant why do they have the strange syntax they do "in Ruby". > There just doesn't seem to be a good reason for a block. Continuations come out of a style of programming called Continuation Passing Style (CPS) where the continuation to a function is explicitly passed as an argument to the function. A continuation is essentially the code that will be executed when the function returns. By explicitly capturing a continuation and passing it as an argument, a normal recursive function can be turned in to a tail recursive function and there are interesting optimizations that can be done at that point. Dan Sugalski has some writeups in his "What the Heck is ... " series at: http://www.sidhe.org/~dan/blog. Since a continuation is related to a function invocation, when you ask for a continuation object you need to specify which function invocation the continuation is for. callcc addresses this by invoking the block, and passing the continuation of the block's invocation to the block itself. Since callcc "knows" it needs the continuation before the block is invoked, I suspect that it might be easier for the implementor than if the continuation of just *any* function invocation could be grabbed. I played around with a CPS solution to PragDave's Kata 2 (see http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v). You can find my writeups at http://onestepback.org/index.cgi/Tech/Programming/Kata. The third, fourth and fifth entries deal with CPS, tail recursion and callcc. -- -- Jim Weirich jweirich@one.net http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)