比方說,我想編寫Text.pack
與Text.strip
。爲什麼函數組合需要括號?
:t (.)
生產:(b -> c) -> (a -> b) -> a -> c
:t (Text.pack)
生產:String -> Text
:t (Text.strip)
生產:Text -> Text
所以對於(b -> c)
代strip
給出: b = Text
c = Text
代pack
爲(a -> b)
給出: a = String
b = Text
讓我們驗證::t strip . pack
生產: strip . pack :: String -> Text
OK,讓偉大的嘗試:
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 "
按預期工作....爲什麼?
更強大的功能應用比*其他任何*都具有更高的優先級。 – amalloy
@amalloy ...除了記錄更新。 = P –
該優先權允許例如'甚至過濾。地圖(+5)'。組成部分應用的功能是相當習慣的。還要注意,爲了方便起見,選擇了「map,filter,...」參數的順序。 – chi