为什么函数组合需要括号?
Why does function composition require parentheses?
假设我想用 Text.strip
组合 Text.pack
。
:t (.)
产生:(b -> c) -> (a -> b) -> a -> c
:t (Text.pack)
产生:String -> Text
:t (Text.strip)
产生:Text -> Text
所以用 strip
代替 (b -> c)
得到:
b = Text
c = Text
用 pack
代替 (a -> b)
得到:
a = String
b = Text
让我们验证::t strip . pack
产生:
strip . pack :: String -> Text
好的,太棒了,让我们试试吧:
strip.pack " example "
产生:
Couldn't match expected type ‘a -> Text’ with actual type ‘Text’
Relevant bindings include
it :: a -> Text (bound at <interactive>:31:1)
Possible cause: ‘pack’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘pack " example "’
In the expression: strip . pack " example "
(strip . pack) " example "
按预期工作....为什么?
函数应用的优先级高于组合。
strip.pack " example "
等同于strip.(pack " example ")
。这是人们使用 $
到 "suppress" 应用程序的原因之一,直到所有功能都已组合:
strip . pack $ " example "
函数应用程序的优先级高于函数组合运算符。
假设我想用 Text.strip
组合 Text.pack
。
:t (.)
产生:(b -> c) -> (a -> b) -> a -> c
:t (Text.pack)
产生:String -> Text
:t (Text.strip)
产生:Text -> Text
所以用 strip
代替 (b -> c)
得到:
b = Text
c = Text
用 pack
代替 (a -> b)
得到:
a = String
b = Text
让我们验证::t strip . pack
产生:
strip . pack :: String -> Text
好的,太棒了,让我们试试吧:
strip.pack " example "
产生:
Couldn't match expected type ‘a -> Text’ with actual type ‘Text’
Relevant bindings include
it :: a -> Text (bound at <interactive>:31:1)
Possible cause: ‘pack’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘pack " example "’
In the expression: strip . pack " example "
(strip . pack) " example "
按预期工作....为什么?
函数应用的优先级高于组合。
strip.pack " example "
等同于strip.(pack " example ")
。这是人们使用 $
到 "suppress" 应用程序的原因之一,直到所有功能都已组合:
strip . pack $ " example "
函数应用程序的优先级高于函数组合运算符。