Reputation: 41909
I implemented an exercise from Learn You a Haskell.
isUpperCase' :: Char -> Bool
isUpperCase' = (`elem` ['A'..'Z'])
As I understand, (elem ['A'..'Z'])
is a partially applied function. Calling isUpperCase' 'B'
results in calling isUpperCase apply 'B'
, which results in True
.
Note that, without the parentheses, the following compile-time error occurs:
*Main> :l IsUpperCase.hs
[1 of 1] Compiling Main ( IsUpperCase.hs, interpreted )
IsUpperCase.hs:3:16: parse error on input ``'
Failed, modules loaded: none.
What's the reason for surrounding the partially applied function with parentheses?
Upvotes: 0
Views: 122
Reputation: 53881
It's the fact that the function is surrounded by ``'s.
In haskell foo `bar` baz
is exactly the same as bar foo baz
. It converts prefix function application into an infix operator.
Just like other operators we can use sectioning like (2 *)
. In this case
(`elem` ['A'..'Z'])
\a -> a `elem` ['A'..'Z']
\a -> elem a ['A' .. 'Z']
flip elem ['A' .. 'Z']
This is just a clever (hacky) way to avoid writing flip
. What you have is equivalent to
isUpperCase' = flip elem ['A' .. 'Z']
Upvotes: 3