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