Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does function composition require parentheses?

Let's say I want to compose Text.pack with Text.strip.

:t (.) produces: (b -> c) -> (a -> b) -> a -> c

:t (Text.pack) produces: String -> Text

:t (Text.strip) produces: Text -> Text

So substituting strip for (b -> c) gives: b = Text c = Text

Substituting pack for (a -> b) gives: a = String b = Text

Lets verify: :t strip . pack produces: strip . pack :: String -> Text

ok, great lets try it:

strip.pack " example "

Produces:

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 " works as expected.... why?

like image 506
Abraham P Avatar asked Aug 11 '16 20:08

Abraham P


2 Answers

Function application has a higher priority than composition.

strip.pack " example " is equivalent to strip.(pack " example "). This is one reason why people use $ to "suppress" application until after all the functions have been composed:

strip . pack $ "    example     "
like image 71
chepner Avatar answered Nov 14 '22 02:11

chepner


Function application has an higher precedence over the function composition operator.

like image 25
Shoe Avatar answered Nov 14 '22 00:11

Shoe