Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fmap and map, I can't see the difference

Tags:

haskell

I'm trying to understand what the functors are, but so far I can't. What's the difference between these 2:

Prelude> fmap (+1) [1..9]
[2,3,4,5,6,7,8,9,10]

Prelude> map (+1) [1..9]
[2,3,4,5,6,7,8,9,10]
like image 942
Incerteza Avatar asked Apr 06 '14 16:04

Incerteza


1 Answers

For lists, there is no difference, map is just fmap specialised to lists.

fmap has a more general type:

fmap :: Functor f => (a -> b) -> f a -> f b

this means it can be used with any functor e.g.

fmap (+ 3) (Just 4)          -- Just 7
fmap (+ 4) (+ 3) 1           -- 8. Functions are functors where fmap = (.)
fmap read getLine :: IO Int  -- IO is a functor

while map has type

map :: (a -> b) -> [a] -> [b]

If you look at the source, the functor instance for lists defines fmap as map:

instance Functor [] where
    fmap = map
like image 140
Lee Avatar answered Oct 23 '22 04:10

Lee