Hacker News new | ask | show | jobs
by kqr 531 days ago
> If I haven’t used fold-left or fold-right in a while, I sometimes forget which one computes what.

I'm glad I'm not the only one struggling with this! Though I have started remembering it a different way: I pretend the 'r' in 'foldr' stands for recursive. Thus it's easier to remember that

    foldr(º, [a, b, ...]) ~=
        a º (b º ...)
where the right term for each operator is given by the recursive call. In contrast, then, foldl must be the iterative variant where

    foldl(º, z, [a, b, ...]) ~=
        z º= a
        z º= b
        ...
        return z
2 comments

It's even easier if you just remember that left and right in fold means left associative and right associative application of the function.
In fold-left, the folding happens on the left.

> it's easier to remember that ...

Whoa, we have very different experiences remembering things.