Continuation-passing Style - CPS and Tail Calls

CPS and Tail Calls

Note that in CPS, there is no implicit continuation — every call is a tail call. There is no "magic" here, as the continuation is simply explicitly passed. Using CPS without tail call optimization (TCO) will cause not only the constructed continuation to potentially grow during recursion, but also the call stack. This is usually undesirable, but has been used in interesting ways - see the Chicken Scheme compiler. As CPS and TCO eliminate the concept of an implicit function return, their combined use can eliminate the need for a run-time stack. Several compilers and interpreters for functional programming languages use this ability in novel ways.

Read more about this topic:  Continuation-passing Style

Famous quotes containing the words tail and/or calls:

    “And how do you know that you’re mad?”
    “To begin with,” the Cat said, “a dog’s not mad. You grant that?”
    “I suppose so,” said Alice.
    “Well then,” the Cat went on, “you see a dog growls when it’s angry, and wags its tail when it’s pleased. Now I growl when I’m pleased, and wag my tail when I’m angry. Therefore I’m mad.”
    Lewis Carroll [Charles Lutwidge Dodgson] (1832–1898)

    Whoever won’t fight when the President calls him, deserves to be kicked back in his hole and kept there.
    Lyndon Baines Johnson (1908–1973)