Elmer:

As you point out in your subject line,  _5  is NOT the same thing as  -5  .  
The former is a single, indivisible number; the latter is a function applied to 
a number.  

The results of each are identical and indeed if you assigned them both to names 
and used the names instead of the expressions, you could use them 
interchangeably everywhere.  As an alternative to assigning them to names, you 
could wrap them in parens, and also use them interchangeably.

Which brings me to my point:  why need you wrap them in parens?  What do the 
parens do?  Well, what do parens traditionally do in computer languages (and 
mathematical expressions)?  They change precedence.  And that's the heart of 
this issue.

Your surprising observation, that  -5+-4  is not the same as  _5+_4  , is a 
result of J's precedence rules.  Whereas in other languages, different 
functions have different precedences, in J they do not.  J is egalitarian.  All 
functions have the same precedence.  

So, for example, in most languages, this expression:

   4*5+3

has two functions:  *  and  +  .  Usually, the  *  has higher precedence than  
+  , and so gets executed first.  Therefore, the expression would be equivalent 
to  (4*5)+3  and result in  23  .  Not so in J.   

In J, __all expressions execute from the right to the left__.  So, the above 
expression is equivalent to   4 * (5+3)  and hence the result is  32 .

Which brings me back to your expression.  In other languages, the symbol "-" 
has two roles.  When there's numbers on its left and on its right (binary), 
it's mathematical minus, and has the same precedence as  +  .  When there's no 
number on its left, but one on its right (unary), it's mathematical negate, 
with a much higher precedence. 

The same is true of J, except for the precedence change.  Unary (monad) - has 
exactly the same precedence as binary (dyad)  -  .  So, in other languages, 

   -4+-5  

gets interpreted as (-  4)  +  (-  5)  which becomes  _4+_5  which results in 
_9  .  But not in J.  

The rule is simple.  All expressions get evaluated right to left; there is no 
precedence.  So J sees the above as  - (4 + (-  5) )  which becomes   - (4 + 
_5)  which becomes  - (_1)  which results in  1  .

Though J has deposed the tyrant PEMDAS, it does recognize the convenience of 
the high-precedence "-" sign for denoting negative numbers.  No one wants to go 
around typing  (-5) + (-4)  .  So, in order to remain consistent but 
convenient, J permits you to denote the negation of a number by prefixing it 
with an underbar.  

But the underbar is not an function.  Unlike  -  , it cannot be separated from 
the number.  Just like a decimal point (or, for that matter, a numeral),  it is 
part of the number's identity:  it's how you spell its name.

Therefore, in  _4 + _5  , there is only one function to consider, and its 
obvious result is  _9  .  

-Dan

PS:  If it helps, think of verbs in J as named functions in other languages.  
In this case, the named function would be  negate =: -  .  Now, what would you 
expect  negate(4 + negate(5))  to  produce?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to