foldl1 (*) [2,1,2]
foldl
is a folding function.
How many arguments does that folding function take?
foldl1
and foldl
?
foldr1
?
> foldl1 (*) [2,1,2] 4
foldl
is a folding function.
How many arguments does that folding function take?
foldl
's
third argument.
That accumulated value is called "state" by some because it represents the state of the computation, but that seems a little imprecise to me because it typically doesn't contain any representation of how much of the list has been processed and thus isn't the full state of the computation.
I've sometimes named that accumulated value parameter
thusFar
because it represents what we've computed thus far in the folding.
foldl1
and foldl
?
foldl1
(and foldr1
) can be thought of as reductions—they produce a value of the same type as in the list they're folding.
foldl
(and foldr
) can fold a list of values into a different type. Examples:
Bool
s can be folded into a String
.
Int
s can be folded into a Bool
.
EC: What is the type of foldr1
?
:t foldr1
shows
foldr1 :: Foldable t => (a -> a -> a) -> t a -> abut as slide 135 suggests, we think of that as
foldr1 :: (a -> a -> a) -> [a] -> a