Talk:Continuation-passing style
From Wikipedia, the free encyclopedia
Contents |
[edit] Problems understanding CPS
I don't understand the example, because the function k that is inputed in the first example "(define (mysqrt x k) (k (sqrt x)))" takes in one argument, while the fuction given for k in the second example "(mysqrt 4 (lambda (x k) (k (+ x 2))))" needs 2 arguments. How does this work???
-- the number of args 'k' takes doesn't matter, so long as k is always called with that many args.. and anyway, in both examples here k only takes 1 arg (in the first example k is called with the sqrt of x, in the 2nd example k is called with x + 2).
I agree, the exposition and those examples were poor, by which I mean: wrong. I rewrote the exposition and replaced the examples some time ago. Please have a look, and if they now make sense to you go ahead and delete this Talk section.
[edit] Examples of compilers that use CPS internally
As continuation passing style renders return values virtually useless, it can also be used to eliminate the need for a runtime stack. Several compilers and interpreters for functional programming languages use this ability internally in novel ways.
Can anyone fill this in a bit with examples of specific compilers and/or interpreters that make use of CPS, and which novel ways they do so in? Ari 08:20, 26 December 2005 (UTC)
- I believe SML/NJ uses CPS internally. It did, at least, in the mid nineties. As I recall, this also makes the implementation of CML thread much more efficient. I'll see if I can dig out some references... Brighterorange 18:25, 26 December 2005 (UTC)
- yeah
[edit] CPS is a monad?
The article says "CPS is a monad." Can someone please make this clearer with "CPS is a monad because..." or something like that. Toby 21:48, 20 March 2006 (UTC)
- Ok, I found a paper explaing how it is a monad (or rather how continuations are monads). I'll add it on the "monads in functional programming" page and clarify / correct this sentance when I get back from work later on.--NotQuiteEXPComplete 00:05, 3 August 2006 (UTC)
- On second though I'll remove this comment since the statement that "CPS is a monad" is wrong. Continuations are monads, but saying this in an article on CPS doesn't make much sense. --NotQuiteEXPComplete 12:52, 3 August 2006 (UTC)
[edit] CPS as GOTO
Think about that for a moment. This certainally smells like a goto chain.
- Yes, it is much like GOTO, except that a call to a continuation can transfer control non-locally. — brighterorange (talk) 18:53, 25 September 2006 (UTC)
- Continuations are the functional equivalent of the GOTO statement in imperative languages. Same risk - functional spaghetti-code. Phraine 07:58, 26 December 2006 (UTC)