Why does foldr use a helper function?

后端 未结 4 1956
旧时难觅i
旧时难觅i 2020-12-30 21:54

In explaining foldr to Haskell newbies, the canonical definition is

foldr            :: (a -> b -> b) -> b -> [a] -> b
foldr _ z          


        
4条回答
  •  甜味超标
    2020-12-30 22:06

    One tiny important detail not mentioned in other answers is that GHC, given a function definition like

    f x y z w q = ...
    

    cannot inline f until all of the arguments x, y, z, w, and q are applied. This means that it's often advantageous to use the worker/wrapper transformation to expose a minimal set of function arguments which must be applied before inlining can occur.

提交回复
热议问题