Reputation: 1836
I'm working my way through the first haskell book and struggle with the $
operator:
The following line works:
map (>= 16) . take 5 $ iterate (\x -> x^2) 2
However, the following doesn't:
map (>= 16) . take 5 (iterate (\x -> x^2) 2)
Possible cause: `take' is applied to too many arguments
I don't see the problem here. take
takes an int and a list. To my understanding, I provided both arguments.
What do I have to do if I want to avoid the $
operator?
Upvotes: 1
Views: 101
Reputation: 477607
The ($) :: (a -> b) -> a -> b
operator is a function that simply has a the lowest priority (infixr 0
, only ($!)
and seq
have the same priority). As a result:
map (>= 16) . take 5 $ iterate (\x -> x^2) 2
is equivalent to:
(map (>= 16) . take 5) (iterate (\x -> x^2) 2)
so also with brackets for the left operand as well.
It is actually a nice thing about Haskell that you can use operators as a grouping mechanism: ($)
is simply defined as ($) f x = f x
, but because of the fact that it is an operator, it can be used as a way to avoid brackets.
Upvotes: 7