[Haskell-cafe] Those damned parentheses
Antoine Latter
aslatter at gmail.com
Sat May 7 21:20:15 CEST 2011
On Sat, May 7, 2011 at 2:10 PM, Eitan Goldshtrom <thesourceofx at gmail.com> wrote:
> Hi. I am kind of tired of all of the parentheses I have to put in places and
> I'm trying to figure out what is the correct way to write code such that I
> can leave out parentheses. For example, I have the following:
>
> data Message = ... --leaving this out because it's not important
> data Plane = Plane {
> id :: Int,
> position :: (Int,Int,Int),
> direction :: Int,
> path :: [Int],
> messagebuf :: Chan Message
> }
>
> main = do
> c <- newChan :: Chan Message
> p <- Plane 0 (0,0,0) 0 [] c
> f p
>
> f p = putStrLn $ (show Main.id p) ++ " - message received"
>
One thing to keep in mind is that function application binds tightest,
and function application goes from left to right, so:
> a b c d
is parsed as:
> ((a b) c) d
I'm not really sure what you're doing, but I would probably write it as:
> f p = putStrLn $ show (Main.id p) ++ " - message received"
Because function application binds tightest, I don't need parenthesis
around the (show ...) part.
The other rule to note is the the ($) function binds the weakest of
them all, so you can do a lot to the left of it and to the right of it
without parentheses.
> This causes an error "The function `show' is applied to two arguments". If I
> put instead:
> f p = putStrLn $ (show . Main.id p) ++ " - message received"
>
> I get the error "Couldn't match expected type `[Char]' with actual type `a0
> -> c0'". The only way it seems to work is
> f p = putStrLn $ (show (Main.id p)) ++ " - message received"
>
> This seems to be the same for many other situations where I try to use
> function composition of some sort. It's just getting kind of annoying.
>
> -Eitan
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
More information about the Haskell-Cafe
mailing list