Hi, Shishir,
It's because Haskell uses juxtaposition for argument passing. So the first case
fmap (\x -> x) Just 2
is equal to
(fmap (\x -> x) Just) 2
While
fmap (\x -> x+2) $ Just 2
is
fmap (\x -> x + 2) (Just 2)
I believe you want the latter.
Basically, the first example works because ((->) r) is an instance of Functor.
instance Functor ((->) r) where
fmap = (.)
So basically first example is:
((\x -> x) . Just) 2
Now you should see why it behaves this way.
Have a nice day!
Alexey Shmalko