On continuation-passing style and the factorial function

I wrote a post about an interesting thing I learned recently about continuation-passing style and the factorial function. It shows how a special representation of continuations leads to the iterative version of the factorial function.


I think I saw this pattern a few times usually implemented like this:

type Trampoline a
    = Done a
    | Jump (() -> Trampoline a)

evaluate : Trampoline a -> a
evaluate trampoline =
    case trampoline of
        Done value ->

        Jump f ->
            evaluate (f ())

Example: Trampoline.elm

Actually, trampolining is an orthogonal concern that’s not related to what I was writing about in the article.

However, I remember using trampolining when I was trying to improve the divModBy function in elm-natural, which you can find here.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.