Sujet : Re: Operator precedence
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.awkDate : 23. May 2024, 17:49:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240523092856.646@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-05-23, Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> wrote:
After reading an old article in the Algol bulletin about questions
and issues with operator precedences I tried this with (Gnu) Awk
>
$ awk 'BEGIN { a=5; print a^2, -a^2, 0-a^2 }'
25 -25 -25
>
and shell (Ksh)
>
$ a=5 ; print $(( a**2 )) $(( -a**2 )) $(( 0-a**2 ))
25 25 -25
>
and Algol 68
>
$ a68g -p 'INT a=5; (a^2, -a^2, 0-a^2)'
+25 +25 -25
>
I don't want to value the different results (based on precedence),
but I'm interested in your comments/thoughts about the differences.
There is no question that we want the exponentiation operator
to have a higher precedence than plus or minus, even including
the unary forms. It's the "E" in the BEDMAS acronym that
English-speaking children learn in some places in the world: brackets,
exponentiation, division, multiplication, addition, subtracation.
If -a**2 is not parsed as -(a**2), you're messing with the BEDMAS.
Furthermore exponentation between on an intermediate precedence
level between unary minus and regular minus is simply insane.
You might think you're out of the woods with Lisp, where you don't
have precedence. But some math operations have variadic arguments,
so associativity comes into play.
Common Lisp restricts expt to exactly two arguments:
[1]> (expt 5 2)
25
[2]> (expt 5 2 3)
*** - EVAL: too many arguments given to EXPT: (EXPT 2 5 3)
I made it n-ary in TXR Lisp:
1> (expt 5 2)
25
2> (expt 5 2 3)
390625
Look, it's a right-to-left reduction, unlike addition, or multiplication:
1> (expt (expt 5 2) 3)
15625
2> (expt 5 (expt 2 3))
390625
In other words (expt a b c ...) denotes
...
c
b
a
rather than
b c ...
(.. ((a) ) )
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca