2
Prelude> (fmap . const ) 2 Just 3
2
Prelude> 2 <$ Just 3
Just 2
Prelude> :t (<$)
(<$) :: Functor f => a -> f b -> f a
Prelude> :t fmap . const
fmap . const :: Functor f => b -> f a -> f b

in functor,

(<$)        =  fmap . const   

why I get different result for Maybe? did not find a implement of <$ in Maybe. Thanks.

duplode
  • 33,731
  • 7
  • 79
  • 150
echo
  • 2,666
  • 1
  • 25
  • 17
  • `( (<$>) . const ) 2 Just 3` = `(const 2 <$> Just) 3` is *not* in `Maybe`, but in `((->) r)`, where `fmap = (.)` so it becomes `(const 2 . Just) 3 = const 2 (Just 3)`. `2 <$ Just 3 = fmap (const 2) (Just 3) = Just ( (const 2) 3)`. – Will Ness Jul 30 '17 at 07:39

2 Answers2

6

(fmap . const) 2 Just 3 is equivalent to ((fmap . const) 2 Just) 3, where 2 <$ Just is const 2 and const 2 3 is 2.

You meant:

(fmap . const) 2 $ Just 3
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Is there a common name for this function (`<$`)? Maybe `constMap`? –  Jun 29 '17 at 20:09
  • 1
    @ftor https://stackoverflow.com/questions/7746894/are-there-pronounceable-names-for-common-haskell-operators – Ry- Jun 29 '17 at 20:24
  • `2 <$ Just` is `const 2 . Just`. – Will Ness Jul 30 '17 at 07:39
  • @WillNess: … which is `const 2`. – Ry- Jul 30 '17 at 07:41
  • skipping of derivation steps belongs in scientific papers, not in textbooks. :) – Will Ness Jul 30 '17 at 07:43
  • @WillNess: It’s not a derivation. It’s a simplification of parts of the expression at a time so the OP can follow what’s happening. – Ry- Jul 30 '17 at 07:44
  • skipping of simplification steps belongs in scientific papers, not in textbooks. – Will Ness Jul 30 '17 at 07:46
  • @WillNess: It’s not a textbook. My answer also uses the RyanHC implementation where `Functor ((->) r)` has `x <$ _ = const x`. – Ry- Jul 30 '17 at 07:47
  • SO answers are addressed to newcomers, as textbooks are. – Will Ness Jul 30 '17 at 07:50
  • @WillNess: Cool. Still not a textbook despite potentially sharing a target audience, and this remains exactly the answer I would give a newcomer. – Ry- Jul 30 '17 at 07:51
  • then they wd still have hard time following it. – Will Ness Jul 30 '17 at 07:52
  • @WillNess: No, I’m pretty sure addressing it at a semantic level rather than “expand the GHC implementation” is easier to follow. I’ll be happy to add words on the former if a newcomer asks. :) – Ry- Jul 30 '17 at 07:53
  • the ["default definition `fmap . const`"](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Functor.html#v:-60--36-) looks like a semantic level definition, and the ["RyanHC implementation"](https://stackoverflow.com/questions/44833632/in-maybe-definition/44833660?noredirect=1#comment77755703_44833660) looks like a specific implementation, a clever shortcut, to me. (what is RyanHC BTW?) --- of course *" `2 <$ Just` is `const 2`"* on its own is completely baffling to me, that's why I commented in the first place. I can't understand it except by filling in the missing step. – Will Ness Jul 30 '17 at 09:51
  • @WillNess: Different people have different intuitive explanations I guess :) – Ry- Jul 30 '17 at 10:19
  • yes, some are comfortable with skipping steps, some are not. experts more among the first group, newbies the second. – Will Ness Jul 30 '17 at 11:16
6

The problem is that you typed (fmap . const) 2 Just 3. I believe what you meant was rather (fmap . const) 2 (Just 3). In the former, the function fmap . const is applied to three arguments, namely 2, Just and 3, and, in the latter, your expression is indeed equivalent to 2 <$ Just 3.

> (fmap . const) 2 (Just 3)
Just 2
> 2 <$ Just 3
Just 3
baxbaxwalanuksiwe
  • 1,474
  • 10
  • 20