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:

    “What a wonderful bird the frog are—
    When he stand he sit almost;
    When he hop, he fly almost.
    He ain’t got no sense hardly;
    He ain’t got no tail hardly either.
    When he sit, he sit on what he ain’t got almost.”
    —Unknown. The Frog (l. 1–6)

    “We realize that we are laggards from the past century, still living in what Marx kindly calls ‘the idiocy of rural life,’ and we know that our rural life is like that of the past, not like that of much of the present.”
    —For the State of Vermont, U.S. public relief program (1935-1943)