foldLeft v. foldRight - does it matter?

后端 未结 6 2052
迷失自我
迷失自我 2020-12-24 14:27

Previously, Nicolas Rinaudo answered my question on Scala\'s List foldRight Always Using foldLeft?

Studying Haskell currently, my understanding is that foldRig

6条回答
  •  星月不相逢
    2020-12-24 14:41

    I'm no Scala expert, but in Haskell, one of the most important differentiating features between foldl' (which really should be the default left fold) and foldr is that foldr will work on infinite data structures, where foldl' will hang indefinitely.

    In order to understand why this is, I recommend visiting foldl.com and foldr.com, expanding the evaluations a couple of times, and reconstructing the call tree. You'll quickly see where foldr is appropriate versus foldl'.

提交回复
热议问题