Trampoline.md

May 18, 2018 ยท View on GitHub

Module Elm.Trampoline

A trampoline makes it possible to recursively call a function without growing the stack.

Popular JavaScript implementations do not perform any tail-call elimination, so recursive functions can cause a stack overflow if they go too deep. Trampolines permit unbounded recursion despite limitations in JavaScript.

This strategy may create many intermediate closures, which is very expensive in JavaScript, so use this library only when it is essential that you recurse deeply.

Note that in Purescript, there is tail-call elimination, so you may not need this if you arrange your code to use tail-calls. If you do need a trampoline module, you could also consider Control.Monad.Trampoline.

  • In Elm 0.17, this module was moved from core to its own package. *

Trampoline

data Trampoline a

A computation that has been broken up into a bunch of smaller chunks. The programmer explicitly adds "pause points" so each chunk of computation can be run without making the stack any deeper.

done

done :: forall a. a -> Trampoline a

When you do not want a computation to go through the trampoline.

jump

jump :: forall a. (Unit -> Trampoline a) -> Trampoline a

When you want a computation to be delayed so that it is handled by the trampoline.

evaluate

evaluate :: forall a. Trampoline a -> a

Evaluate a trampolined value in constant space.