Skip to content

Instantly share code, notes, and snippets.

@myw
Created August 19, 2014 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myw/f17f72418b137ade7aa8 to your computer and use it in GitHub Desktop.
Save myw/f17f72418b137ade7aa8 to your computer and use it in GitHub Desktop.
Prelude Data.String Data.List> (delete 'h' . delete 'h') "hi there"
"i tere"
Prelude Data.String Data.List> delete 'h' . delete 'h' $ "hi there"
"i tere"
Prelude Data.String Data.List> delete 'h' . delete 'h' ( "hi there" )
<interactive>:21:14:
Couldn't match expected type `a0 -> [Char]'
with actual type `[Char]'
In the return type of a call of `delete'
Probable cause: `delete' is applied to too many arguments
In the second argument of `(.)', namely `delete 'h' ("hi there")'
In the expression: delete 'h' . delete 'h' ("hi there")
Prelude Data.String Data.List>
@mmachenry
Copy link

Function application has very high precedence. In the first example, you force the two deletes to be correctly applied to each other with the use of parenthesis. You've completely spelled out that (delete 'h' . delete 'h') is one expression and therefore of course that's evaluated as one function and then applied to "hi there".

In the second example the precedence of ($) and (.) are are at play. Function application still has the highest precedence so (delete 'h') and (delete 'h') become expressions and you have (delete 'h') . (delete 'h') $ "hi there". Since ($) is specifically intended to have a very low precedence, which gives it it's behavior of grouping everything to its left and right separately, (.) binds first and you get the same thing as the first example.

In the third example you don't have the ($) so function applications binds tightly, giving you first (delete 'h' ( "hi there" )) which is a [Char] and that can't be composed with a function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment