4 ms·
Usually you want `foldl'` (with ' at the end), the strict version of `foldl`. It prevents the creation of intermediate thunks, so effectively a tail recursive i
by farrellm23 1y ago
Usually you want `foldl'` (with ' at the end), the strict version of `foldl`. It prevents the creation of intermediate thunks, so effectively a tail recursive iteration over the list in constant space.
`foldr` I almost never use, but it would be for: the return value is a lazy list and I will only need to evaluate a prefix.
- zoul 1y agoThe naming conventions are nicely sadistic.
- WorldMaker 1y agoThis is what happens when you let Mathematicians name things on chalkboards. They don't want to run out of chalk and they get tired of spelling whole words very easily so they use short names and silly symbols. The name foldl' is "just" "fold left prime". Remember ' means "prime" from calculus class and thinking that was silly even then? Accidentally infected Haskell at a young age.
- Rendello 1y ago> This is what happens when you let Mathematicians name things on chalkboards. Doubly so with APL!
- kccqzy 1y agoYeah it's definitely unusual to allow ' to be part of the name of a variable, especially considering that it is, like C, the quote for character types.
- WorldMaker 1y agoIn Haskell's case that is indirectly Lisp's fault. Lisp heard of Algol (C's design-by-committee "grandparent" on the family tree) identifier restrictions and thought they were silly. (I think Lisp is also often classified as Haskell's great-grandparent on the family tree? Lisp -> Scheme -> ML -> Haskell, I think?) As with most of that FP family the boundary between "operator" and "identifier" is real thin to nonexistent. Aside: I've got half a feeling you could implement an APL-like directly in Haskell as a DSL using Unicode-named functions. I've seen Unicode Haskell files that look like APL (and GHC supports a surprising amount of that almost out of the box, as I recall).