3 ms·
In his explicit recursion example, the author questions why the compiler doesn't convert it into a tail call. The issue here is that the speed he gained in the
by tathougies 8y ago
In his explicit recursion example, the author questions why the compiler doesn't convert it into a tail call. The issue here is that the speed he gained in the tall call version had nothing to do with the tail call. It has to do with the fact that the accumulating parameter is strict. The explicitly recursive version is lazy, which means there is a thunk structure of (1 + 1 + ... ) being built up in memory, which is then evaluated. This overhead of building up this thunk, and then evaluating it (awful cache characteristics) is what he's seeing. By adding a bit of strictness, the author could likely get the optimizer to convert it into a strict tail call.
- gowld 8y agohttps://wiki.haskell.org/Foldr_Foldl_Foldl%27 https://wiki.haskell.org/Foldr_Foldl_Foldl%27 explains this in detail.