[Haskell-beginners] Return a Foldable instance

Antoine Latter aslatter at gmail.com
Wed May 4 19:46:13 CEST 2011


On May 4, 2011 12:32 PM, "Federico Mastellone" <fmaste at gmail.com> wrote:
>
> On Wed, May 4, 2011 at 12:25 PM, Stephen Tetley
> <stephen.tetley at gmail.com> wrote:
> > On 4 May 2011 13:21, Federico Mastellone <fmaste at gmail.com> wrote:
> >> So, is it even possible to write a function with this type: getValues
> >> :: Foldable f => k -> MultiMap k v -> f v ???
> >>
> >
> > I hadn't thought about it, but it's not possible to write that either.
(!)
> >
> > You would need something like an Insertable class instead, plus Monoid
> > so you can get an initial empty Set or List:
> >
> > getValues :: (Monoid (f v), Insertable f) => k -> MultiMap k v -> f v
>
> But it won't be Foldable either, or is there a way to traverse a
> Monoid that I don't know?

With this signature the caller gets to pick the representation type; if they
want something foldable they can choose it themselves.

>
> >
> > class Insertable f where
> >  insert :: a -> f a -> f a
> >
> > instance Insertable [] where
> >  insert a xs = (a:xs)
> >
> >
> > -- Won't actually compile due to Ord constraint on Set
> > -- (though there are ways with GHC to around that).
> > --
> > instance Insert Set.Set where
> >  insert = Set.insert
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
>
>
> --
> Federico Mastellone
> Computer Science Engineer - ITBA
> ".. there are two ways of constructing a software design: One way is
> to make it so simple that there are obviously no deficiencies, and the
> other way is to make it so complicated that there are no obvious
> deficiencies. The first method is far more difficult."
>
> Tony Hoare, 1980 ACM Turing Award Lecture.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110504/7a07811f/attachment.htm>


More information about the Beginners mailing list