Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<$ in Maybe definition

Tags:

haskell

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.

like image 772
echo Avatar asked Dec 06 '25 18:12

echo


2 Answers

(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
like image 115
Ry- Avatar answered Dec 08 '25 23:12

Ry-


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
like image 24
baxbaxwalanuksiwe Avatar answered Dec 08 '25 23:12

baxbaxwalanuksiwe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!