[Haskell-beginners] Type unions
Daniel Fischer
daniel.is.fischer at googlemail.com
Wed Dec 15 15:37:39 CET 2010
On Wednesday 15 December 2010 15:15:16, Guofeng Zhang wrote:
> What does "$" mean in Left $ A x. Why does not write it as "Left A x"?
>
($) is used for fixity, it's a low-precedence identity for functions. You
could also use parentheses for that.
"Left $ A x" is equivalent to "Left (A x)" while "Left A x" would be parsed
as "(Left A) x" - and therefore give a type error, since "Left A" has the
type Either (Int -> A) b (assuming A is a value constructor for the type A
which takes an Int argument) and not a function type, hence you can't apply
it to the value x.
> For putStrLn $ "welcome", is the "$" has the same meaning as that in
> Left $ A x?
>
In `putStrLn $ "welcome"', the $ is completely superfluous because you
apply putStrLn to an atomic value (syntacically atomic, it's a single
token), so you can say it has no meaning at all there, or it has the same
meaning, implicitly adding parentheses, sort of.
If you had `putStrLn $ "Welcome, " ++ name', it would again serve the same
purpose, to group the tokens to yield a syntactically correct expression.
More information about the Beginners
mailing list