[Haskell-beginners] Return a Foldable instance

Chaddaï Fouché chaddai.fouche at gmail.com
Wed May 4 18:22:48 CEST 2011


On Wed, May 4, 2011 at 2:21 PM, 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 ???
>

It isn't since there's nothing in the Foldable class that allows you
to build one (only to consume one, do a fold over it, no "unfold").

But since this signature is _not_ the one you really want (it says you
will deliver any Foldable your caller asks for), this isn't a problem,
what you really want is :

> data ExFoldable a = forall f . (Foldable f) => ExFoldable (f a)
>
> instance Foldable ExFoldable where
>   foldr f z (Exfoldable xs) = foldr f z xs
>
> getValues :: k -> MultiMap k v -> Exfoldable v
> getValues k mm = ExFoldable (yourLookupFunction mm k)
>

or something like that.
With this signature you're really saying you will deliver a Foldable
without telling which one, like in OO.

Now I'm not sure if this is such a good idea since as other have
pointed, delivering a list is a pretty good and mostly simple option
where the list itself is often optimized away. But if you really want
it, Haskell can do it.

(You'll need to activate some extensions to allow this)

-- 
Jedaï



More information about the Beginners mailing list