All this time, when any Haskell lecture spoke of "flat map", usually in relation to Monads, I thought it was called "flat" for a reason, i.e. it flattens out the container. So
[[1,2],[3,4]]
would be processed just as if it were
[1,2,3,4]
But now I discover that fmap and map are basically the same thing, the only difference being the application of one for functors and the other for just lists. And that was only done, in the end, to avoid confusing error messages when using map.
Is that true? And if so why did f
in fmap come to mean "flat", why not "functor map"?
f
infmap
doesn't meanflat
. The equivalent offlatMap
in Haskell is(>>=)
. Themap
function for lists was defined first so another name was needed for the more generalfmap
function. – Leefmap
is the generalization ofmap
to other functors besides the list functor. Whoever told you thatfmap
was short forflat map
was mistaken. – chepnerfmap
when saying "flat map"? It'd make more sense if it had been referring toconcatMap
(aka>>=
), which is often calledflatMap
in other languages and behaves in the way you expected. – sepp2k[1,2,3] >>= \x -> [x,x]
– duplode[[1,2],[3,4]] >>= \x -> [x]
we take every element of the list and bind it tox
(hencex=[1,2]
andx=[3,4]
), then we apply the function\x->[x]
to eachx
, obtaining the result list[ [[1,2]], [[3,4]] ]
. Finally, we flatten the last list to the result:[[1,2],[3,4]]
. – chi