为什么功能组合需要括号?

Abr*_*m P 8 syntax haskell operator-precedence function-composition

假设我想要Text.pack与之合作Text.strip.

: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     "
Run Code Online (Sandbox Code Playgroud)

(strip . pack) " example " 按预期工作....为什么?

che*_*ner 11

功能应用程序的优先级高于组合.

strip.pack " example "相当于strip.(pack " example ").这是人们$在编写完所有函数之前使用"压制"应用程序的一个原因:

strip . pack $ "    example     "
Run Code Online (Sandbox Code Playgroud)

  • @amalloy ...除了记录更新.= P (9认同)
  • 甚至更强大,功能应用程序的优先级高于*其他任何*. (8认同)