[Haskell-cafe] Those damned parentheses

wren ng thornton wren at freegeek.org
Sun May 8 03:45:25 CEST 2011


On 5/7/11 4:29 PM, Eitan Goldshtrom wrote:
> I know about the $ symbol, that's why it's in there in the respective
> places. I see that I can use it to fix my problem, but I was trying to
> figure out function composition really. I guess that's just not the
> place for it. I'll check out Control.Applicative. Also thanks for the
> clarification on function application. I know functions are by default
> infixl 9, but I hadn't really thought that through all the way.

The "secret" to parentheses in Haskell is that juxtaposition binds most 
strongly. That's it. Other languages (e.g., Perl, ML) have much more 
complicated rules which often lead new Haskellers astray; ignore them!

Thus,

     f p = putStrLn $ show (Main.id p) ++ " - message received"

Because you are applying Main.id to p, and you are applying show to 
(Main.id p). Of course, this is identical to:

     f p = putStrLn (show (Main.id p) ++ " - message received")

Since ($) is defined by:

     f $ x = f x

with very low precedence. Alternatively, these are also identical to:

     f p = putStrLn $ (show . Main.id) p ++ " - message received"

     f p = putStrLn $ ((show . Main.id) p) ++ " - message received"

     f p = putStrLn $ (show . Main.id $ p) ++ " - message received"

     f p = putStrLn ((show . Main.id $ p) ++ " - message received")

etc. Note that the definition of (.) is:

     f . g = \x -> f (g x)

or, if you prefer,

     (f . g) x = f (g x)

-- 
Live well,
~wren



More information about the Haskell-Cafe mailing list