【发布时间】:2015-06-18 09:09:20
【问题描述】:
为什么f <$> g <$> x 等同于(f . g) <$> x 虽然<$> 不是右关联的?
(这种等价在a popular idiom 和普通$ 中有效,但目前$ 是右结合的!)
<*> 与<$> 具有相同的关联性和优先级,但行为不同!
例子:
Prelude Control.Applicative> (show . show) <$> Just 3
Just "\"3\""
Prelude Control.Applicative> show <$> show <$> Just 3
Just "\"3\""
Prelude Control.Applicative> pure show <*> pure show <*> Just 3
<interactive>:12:6:
Couldn't match type `[Char]' with `a0 -> b0'
Expected type: (a1 -> String) -> a0 -> b0
Actual type: (a1 -> String) -> String
In the first argument of `pure', namely `show'
In the first argument of `(<*>)', namely `pure show'
In the first argument of `(<*>)', namely `pure show <*> pure show'
Prelude Control.Applicative>
Prelude Control.Applicative> :i (<$>)
(<$>) :: Functor f => (a -> b) -> f a -> f b
-- Defined in `Data.Functor'
infixl 4 <$>
Prelude Control.Applicative> :i (<*>)
class Functor f => Applicative f where
...
(<*>) :: f (a -> b) -> f a -> f b
...
-- Defined in `Control.Applicative'
infixl 4 <*>
Prelude Control.Applicative>
根据<$> 的定义,我预计show <$> show <$> Just 3 也会失败。
【问题讨论】:
标签: haskell syntax infix-notation applicative infix-operator