[Haskell-cafe] map (-2) [1..5]
jupdike at gmail.com
Fri Sep 8 15:15:32 EDT 2006
> In C, it wouldn't be, since there, unary ops always bind tighter than infix
> ops, and the precedences used in C are also used in C++, Java, C#,
> tighter so making unary minus have the same precedence as infix minus just
> makes Haskell syntax difficult to parse for anyone coming from one of these
> other very popular languages. Imho, for better or worse, C has established a
> kind of de-facto standard that unary ops always bind tighter than infix ops
> in programming languages ;-)
... in **programming languages that don't have infix exponeniation**.
You missed Ruby, Python, Matlab, Mathematica (not generally a good
inspiration for syntax but it has a rather large following and almost
20 years of usage), and TI calculators. These all have ^ or ** bind
tighter than unary - to go along with math, not C. Since it didn't
happen we will never know **if C had had infix exponentiation, what
would the precedence have been relative to unary ops** or indeed if
they dropped infix exponentiation, preferring pow(x,y), for exactly
that reason, or what.
All this talk is a design choice and folks will naturally disagree and
the "right" balance for *everything*, especially since all their
conflicting interactions yield so many mutually exclusive possible
outcomes. I suppose I just disagree that C has said the last word
about infix/unary ops because C never spoke up on the problem of unary
negation and exponentiation. (And if it had, would I agree anyway? :-)
I still get tripped up by unary negation in Haskell, especially with
sections, parenthese, etc. But that's what hugs or ghci are for.
> Also, it's a good example of why we should *not* have unary minus, since the
> above could be written with no ambiguity as:
> negate (4 ^ 2)
> or better still:
> negate (expNat 4 2)
> because this would free the ^ symbol for some more widely applicable use,
True. I would much rather be forced to type negate (4 ^ 2) than have
-4^2 return 16 instead of -16 because the - got lexed into -4.
Alternately, if you want unary ops to bind the tightest, then just
eliminating ^ and replacing it with expNat, etc. would solve any
precedence problems with unary minus.
More information about the Haskell-Cafe