As an example, take the following
type Row a = [a]
type Table a = [Row a]
mapTable :: (a -> b) -> Table a -> Table b
mapTable = map . map
notTable :: Table Bool -> Table Bool
notTable = map . map $ (not)
Why, if I remove the $ from notTable, does it stop working?
I have explained this to myself a few times, but it never sticks and it takes me awhile to reason through whats going on. I know the $ basically makes sure that each side of the $ gets evaluated separately because the $ has the lowest precedence but why does this break if I pull the $ out?
Thanks
You're right about precedence: .
is infixr 9 (9 is highest), while $
is infixr 0 (0 is lowest). See the Haskell Report for the operator fixity table.
However, function application has higher precedence than any operator, even .
. Hence:
map . map $ (not)
becomes:
(map . map) $ (not)
while
map . map (not)
becomes:
map . (map not)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With