[Haskell-cafe] Generalizing catMaybes

Henning Thielemann lemming at henning-thielemann.de
Sat Jan 8 12:14:29 CET 2011

On Fri, 7 Jan 2011, Brandon S Allbery KF8NH wrote:

> Hash: SHA1
> On 1/7/11 21:56 , Tony Morris wrote:
>>  I am wondering if it possible to generalise catMaybes:
>> (Something f, SomethingElse t) => t (f a) -> t a
>> I have being doing some gymnastics with Traversable and Foldable and a
>> couple of other things from category-extras to no avail. Perhaps
>> someone else's brain is molded into an appropriate shape to reveal an
>> answer!
> Looks to me like you want something like:
>> mtraverse :: (Traversable t, Monoid m) => t m -> m
>> mtraverse xs = traverse mappend (mempty:xs)
> or possibly the same kind of thing using MonadPlus instead of Monoid.

This is not type-correct, isn't it?

Foldable.fold has the type, that you propose.

For me, the solutions of Dave Menendez make most sense: Generalize Maybe 
to Foldable and List to MonadPlus.

More information about the Haskell-Cafe mailing list