Order of evaluation
10 - 4 - 3 9
J evaluates the sentence as:
10 - (4 - 3) 9
Most other languages would evaluate it as:
(10 - 4) - 3 3
In the absence of parentheses, J implicitly provides them from the right towards the left. Other languages provide them from the left towards the right. A longer sentence will make this visually clearer.
10 - 4 - 3 - 1 8 10 - (4 - (3 - 1)) NB. J right-to-left 8 ((10 - 4) - 3) - 1 NB. others left-to-right 2
Now consider a sequence of monadic verbs.
- - - 4 _4
Everyone knows how to parenthesize this, and every language does it the same.
- (- (- 4)) _4
The grouping is done right-to-left and in this case the other languages agree with J. J always parenthesizes from right-to-left, whereas other languages have different rules for different situations.
J has a right-to-left order of evaluation. Most other languages have a left-to-right order of evaluation for dyads, right-to-left for monads; and this is modified by the relative precedence of the verbs involved.
With nouns and verbs the J evaluation rule from the J Dictionary section E is:
Execution proceeds from right to left, except that when a right parenthesis is encountered, the segment enclosed by it and its matching left parenthesis is executed, and its result replaces the entire segment and its enclosing parentheses.
We should note that although NuVoc is the authoritative source of J information, there is a lot of good information in the J dictionary that may have not made its way into the wiki.
There are things in J, other than nouns and verbs, that you have not yet met that complicate this rule by adding a few more. It is these additional classes that largely justify the J break with tradition and adoption of a right-to-left evaluation.To further quote from the J Dictionary section E:
One important consequence of these rules is that in an unparenthesized expression the right argument of any verb is the result of the entire phrase to its right.
This is due to the lack of verb precedence as well as right-to-left evaluation.
No verb precedence, right-to-left evaluation, and the rules for the other classes make the overall evaluation rules simple, reduce the need for parentheses, and make sentences easier for an experienced J user to read and write.
Read the following sentences, evaluate them in your head, and understand how the no precedence and right-to-left rules explain the answer.
2 * 4 + 5 18 2 + 4 * 5 22 2 - 4 - 5 3 8 % 2 + 2 2
|Primer Index Hover to reveal titles - Click to access -|