在Haskell中加入运算符优先级 [英] infix operator precedence in Haskell
问题描述
以下Haskell表达式
For the following Haskell expression
返回a >> = f
return a >>= f
它应该被读作
Should it be read as
(返回a)>> = f
(return a) >>= f
或
return(a >> = f)?
return (a >>= f)?
这里的相关规则是什么?
what are the related rules here?
推荐答案
规则总是那个函数应用比任何运算符都具有更高的优先级,所以
The rule is always that function application has higher precedence than any operator, so
return a >>= f
被解析为
(return a) >>= f
无论使用什么函数或运算符而不是 return
, f 和
>> =
。
no matter what functions or operators are being used instead of return
, f
, and >>=
.
这意味着像
divide :: Int -> Int -> Double
divide x y = (fromIntegral x) / (fromIntegral y)
相当于
divide :: Int -> Int -> Double
divide x y = fromIntegral x / fromIntegral y
另一个更有用的例子是在函数组成:
Another example where this is even more useful is in function composition:
something :: [Int] -> [Int]
something xs = filter even . map (+1) . zipWith (*) [1..] . take 200 . cycle $ xs
正如您在这里看到的,我们甚至有 zipWith
带两个由其他几个函数组成的参数。这相当于在构图的每个组成部分都放置了括号。
As you can see here, we even have zipWith
taking two arguments composed with several other functions. This is equivalent to having put parentheses around every component of the composition.
这篇关于在Haskell中加入运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!