I read that, map
can be defined using foldr
, i.e. it is a primitive recursive function. At least for lists.
Now my question: Why is Functor not a sub type class of Foldable? And if fmap
can only be defined in terms of foldr
for lists, what makes them special?
Looking at a definition of map
with foldr:
myMap f xs = foldr step [] xs
where step x ys = f x : ys
I could use Monoids to get to:
myMap f xs = foldr step mempty xs
where step x ys = f x : ys
But sadly I'm not much enough of a Haskell magician to get away with cons
.