Why does replacing the dollar sign ($) with parentheses in this expression leads to an error?
本问题已经有最佳答案,请猛点这里访问。
我有两个表达:
(1)工作打印出结果,但(2)有错误,表示:
1 2 3 4 5 6 7 8 9 | <interactive>:50:15: Couldn't match expected type ‘a -> t0 c’ with actual type ‘[Integer]’ Relevant bindings include it :: a -> c (bound at <interactive>:50:1) Possible cause: ‘map’ is applied to too many arguments In the second argument of ‘(.)’, namely ‘map (uncurry (*)) (coords 5 7)’ In the expression: foldr (-) 0 . map (uncurry (*)) (coords 5 7) |
有人能告诉我这两者有什么区别吗?谢谢。
有一个更简单的例子:
1 2 3 4 5 6 7 8 9 | Prelude> id . id $"Example" "Example" Prelude> id . id ("Example") <interactive>:2:10: Couldn't match expected type ‘a -> c’ with actual type ‘[Char]’ Relevant bindings include it :: a -> c (bound at <interactive>:2:1) In the first argument of ‘id’, namely ‘("Example")’ In the second argument of ‘(.)’, namely ‘id ("Example")’ In the expression: id . id ("Example") |
问题是函数应用程序的绑定比
但是,使用
1 2 3 4 |
1 2 3 4 5 6 7 8 9 10 11 12 | foldr (-) 0 . map (uncurry (*)) $ coords 5 7 -- is equivalent to ( foldr (-) 0 . map (uncurry (*)) ) (coords 5 7) -- and to foldr (-) 0 ( map (uncurry (*)) (coords 5 7) ) foldr (-) 0 . map (uncurry (*)) (coords 5 7) -- is equivalent to foldr (-) 0 . ( map (uncurry (*)) (coords 5 7) ) -- and to \x -> foldr (-) 0 ( map (uncurry (*)) (coords 5 7) x) |
在后者中,
注意这也可以:
1 2 3 | GHCi> :i $ ($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ infixr 0 $ |
它出现在其中的任何表达式都将被解析为具有括号。在你的例子中,
被解析为
因为
当计算
尽管您的尝试被不同地解析:函数应用程序比任何中缀绑定得更紧密,甚至是
这不合理。