假设我想用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 "
产生:
: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 "
的效果符合预期....为什么呢?
filter even . map (+5)
。部分应用函数的组合相当惯用。还要注意,map、filter
等的参数顺序是为了方便选择的。 - chi