In order to test how church-encoded lists perform against user-defiend lists and native lists, I\'ve prepared 3 benchmarks:
data Lis
GHC 7.10 has call arity analysis, which lets us define foldl
in terms of foldr
and thus let left folds, including sum
, participate in fusion. GHC 7.8 also defines sum
with foldl
but it can't fuse the lists away. Thus GHC 7.10 performs optimally and identically to the Church version.
The Church version is child's play to optimize in either GHC versions. We just have to inline (+)
and 0
into fenumTil
, and then we have a patently tail-recursive go
which can be readily unboxed and then turned into a loop by the code generator.
The user-defined version is not tail-recursive and it works in linear space, which wrecks performance, of course.