I am attempting to implement the Functor fmap over a Data.Map.Map, but I am getting an error. I’m sure I don’t need to convert the Map to and from a List in order to get this working, but this is the best I’ve come up with so far.
class Functor' f where
fmap' :: (a -> b) -> f a -> f b
instance Functor' (Map.Map k) where
fmap' f m
| Map.null m = Map.empty
| otherwise = let x:xs = Map.toList m
mtail = Map.fromList xs
a = fst x
b = snd x
in Map.insert a (f b) (fmap f mtail)
The error:
No instance for (Ord k)
arising from a use of `Map.fromList'
In the expression: Map.fromList xs
In an equation for `mtail': mtail = Map.fromList xs
In the expression:
let
x : xs = Map.toList m
mtail = Map.fromList xs
a = fst x
....
in Map.insert a (f b) (fmap f mtail)
Any ideas?
The error is due to not assigning the Ord predicate to the type-variable k. Just do this: