Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell help with . and $

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

like image 707
Justin Avatar asked Apr 03 '11 06:04

Justin


1 Answers

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)
like image 187
Joey Adams Avatar answered Sep 23 '22 17:09

Joey Adams