[Haskell-beginners] Return a Foldable instance

Stephen Tetley stephen.tetley at gmail.com
Wed May 4 17:25:41 CEST 2011


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

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



More information about the Beginners mailing list