[Haskell-cafe] Mysterious factorial

Ralf Hinze ralf.hinze at comlab.ox.ac.uk
Wed Dec 30 13:15:36 EST 2009


> I would use:
> 
> foldm :: Monoid m => [m] -> m
> 
> Which is just a better implementation of mconcat / fold.  The reason I
> prefer this interface is that foldm has a precondition in order to
> have a simple semantics: the operator you're giving it has to be
> associative.  I like to use typeclasses to express laws.

That's a valid point. Unfortunately, Haskell is not Coq, so there
is no guarantee that the monoid laws are actually satisfied. And
using a type-class has the downside that you can have only one
instance per type. Maybe, you want to use both `foldm (+) 0' and
`foldm (*) 1'. So the bottom-line is that you should have both
versions.

Cheers, Ralf


More information about the Haskell-Cafe mailing list