toMaybe [Was: Prelude function suggestions]

Henning Thielemann iakd0 at clusterf.urz.uni-halle.de
Fri Jul 30 04:40:16 EDT 2004


On Thu, 29 Jul 2004, Arie Peterson wrote:

> Henning Thielemann wrote:
> > Or say you want to implement a function
> >
> > f :: Set (Maybe a) -> Maybe (Set a)
> >
> > where the result is Nothing if any element of the Set is Nothing, and Just
> > the set containing the Just values otherwise:
> >
> > f s = toMaybe (not (Nothing `elementOf` s)) (mapSet fromJust s)
> 
> You might write f as
> 
>   f = fromList . sequence . toList
> 
> (where sequence is from Control.Monad) if there are 'toList' and
> 'fromList' functions, or maybe adapt 'sequence' to work with sets (or even
> an appropriate generalisation of lists and sets).

It was recently pointed out that Sets can't be monads,
 http://www.haskell.org/pipermail/haskell-cafe/2004-March/005995.html
 so 'sequence' can't be used immediately, but maybe some different
generalization. 

What you propose works in principle, BUT since the Set structure must be
rebuilt, an Ord context is necessary whereas my implementation doesn't
need one.

Regards,
 Henning




More information about the Libraries mailing list