两个例子都来自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.
Run Code Online (Sandbox Code Playgroud)
从第一个示例来看,它的<*>优先级似乎高于<$>.
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".
Run Code Online (Sandbox Code Playgroud)
从第二个例子来看,它的<$>优先级似乎比<*>.
那么它们的优先级相同吗?有人可以给我一些解释/参考吗?
实际上,它们都具有相同的优先级(infixl 4:(<*>)和(<$>)),您可以从左到右阅读 -
(+) <$> (+3) <*> (*100) $ 5
= ((+) <$> (+3)) <*> (*100) $ 5
= (\ a b -> (a+3) + b) <*> (\ a -> a*100) $ 5
= (\ a -> (a+3) + (a*100)) $ 5
= 8 + 500 = 508
Run Code Online (Sandbox Code Playgroud)
记住在这种情况下我们有f <*> g = \x -> f x (g x)
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |