对于以下 Haskell 表达式
返回 a >>= f
应该读作
(返回a)>>= f
or
返回 (a >>= f)?
这里的相关规则是什么?
规则始终是函数应用程序的优先级高于任何运算符,因此
return a >>= f
被解析为
(return a) >>= f
无论使用什么函数或运算符来代替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
另一个更有用的例子是在函数组合中:
something :: [Int] -> [Int]
something xs = filter even . map (+1) . zipWith (*) [1..] . take 200 . cycle $ xs
正如你在这里看到的,我们甚至有zipWith
采用由几个其他函数组成的两个参数。这相当于在组合的每个组成部分都加上括号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)