I like to think of the $ sign as a replacement for brackets.
For example, the following expression:
take 1 $ filter even [1..10] -- = [2]
What happens if we do not put $? Then it would turn out
take 1 filter even [1..10]
and the compiler will now complain because it would think that we are trying to apply 4 arguments to the take function with arguments 1 :: Int , filter :: (a -> Bool) -> [a] -> [a] , even :: Integral a => a -> Bool , [1..10] :: [Int] .
This is obviously not true. So what can we do instead? Well, we could put parentheses around our expression:
(take 1) (filter even [1..10])
This will not decrease to:
(take 1) ([2,4,6,8,10])
which then becomes:
take 1 [2,4,6,8,10]
But we do not always want to write brackets, especially since we begin to enter each other. An alternative is to place the $ sign between where the pair of brackets will go, which in this case will be:
take 1 $ filter even [1..10]
stephen May 14 '16 at 3:25 pm 2016-05-14 15:25
source share