[Haskell-beginners] Type explanation of foldl?

Lawrence Bottorff borgauf at gmail.com
Sat Jan 2 17:51:57 UTC 2021


I've got this

myFoldl f init [] = init
myFoldl f init (x:xs) = myFoldl f newInit xs
  where newInit = f init x

which when evaluated gives this for its type

> :t myFoldl
myFoldl :: (t -> a -> t) -> t -> [a] -> t

. . . not totally sure what that all means, e.g., how the t as a generic
class is behaving is very mysterious. Obviously, the final result is of
type t . . . and that must relate to the incoming argument init, correct?
(BTW, does a type declaration like this one reflect in any way the
recursion going on?)  But then with Prelude foldl I'm really clueless

> :t foldl
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b

I'm guessing the t is again a generic type of the Foldable class, but then.
. .  Can someone walk me through this?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210102/5efcd099/attachment.html>


More information about the Beginners mailing list