It’s folklore that if you’re summing a list of numbers, then you should always use strict foldl
. Is that really true though? foldr
is useful for lists when the function we use is lazy in its second argument. For (+) :: Int -> Int -> Int
this is tyically not the case, but in some sense that’s because Int
is “too strict”. An alternative representation of numbers is to represent them inductively. If we do this, sumation can be lazy, and foldr
can do things that foldl
simply can’t!
First, let’s define natural numbers inductively, and say how to add them:
data Nat = Zero | OnePlus Nat deriving Show
one :: Nat