[英]Precedence Confusion about <$> and <*> in Haskell
來自http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors的兩個示例,
1)。 (+) <$> (+3) <*> (*100) $ 5
(+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and
(*100), resulting in 8 and 500. Then, + gets called with 8 and 500,
resulting in 508.
從第一個示例來看, <*>
優先級似乎比<$>
優先級高。
2)。 (++) <$> Just "johntra" <*> Just "volta"
(++) <$> Just "johntra" <*> Just "volta", resulting in a value
that's the same as Just ("johntra"++),and now Just ("johntra"++) <*>
Just "volta" happens, resulting in Just "johntravolta".
從第二個示例來看, <$>
優先級似乎比<*>
優先級高。
那么它們具有相同的優先級嗎? 有人可以給我一些解釋/參考嗎?
<$>
和<*>
具有相同的優先級,並且具有左關聯性。 $
的最低優先級為零。 您可以使用ghci
探索有關它們的信息:
λ> :i (<$>)
(<$>) :: Functor f => (a -> b) -> f a -> f b
-- Defined in ‘Data.Functor’
infixl 4 <$>
λ> :i (<*>)
class Functor f => Applicative (f :: * -> *) where
...
(<*>) :: f (a -> b) -> f a -> f b
...
-- Defined in ‘Control.Applicative’
infixl 4 <*>
現在,您可以算出類型,以查看它們的類型檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.