2{-| A [trampoline](http://en.wikipedia.org/wiki/Tail-recursive_function#Through_trampolining)
3makes it possible to recursively call a function without growing the stack.
5Popular JavaScript implementations do not perform any tail-call elimination, so
6recursive functions can cause a stack overflow if they go to deep. Trampolines
7permit unbounded recursion despite limitations in JavaScript.
9This strategy may create many intermediate closures, which is very expensive in
10JavaScript, so use this library only when it is essential that you recurse deeply.
13@docs trampoline, Trampoline
16import Native.Trampoline
18{-| A way to build computations that may be deeply recursive. We will take an
19example of a tail-recursive function and rewrite it in a way that lets us use
23 length list = length' 0 list
25 length' : Int -> [a] -> Int
29 hd::tl -> length' (accum+1) tl
31This finds the length of a list, but if the list is too long, it may cause a
32stack overflow. We can rewrite it as follows:
35 length list = trampoline (length' 0 list)
37 length' : Int -> [a] -> Trampoline Int
41 hd::tl -> Continue (\() -> length' (accum+1) tl)
43Now it uses a trampoline and can recurse without growing the stack!
45data Trampoline a = Done a | Continue (() -> Trampoline a)
47{-| Evaluate a trampolined value in constant space. -}
48trampoline : Trampoline a -> a
49trampoline = Native.Trampoline.trampoline
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list