Order of evaluation
<= =>
What is the answer if the parentheses are left out?
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
Remember: no verb precedence and right-to-left evaluation.
<= =>
Primer Index Hover to reveal titles - Click to access - Current page is highlighted | ||||||||||||||||||||||||
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | ✅ |
26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | ✅ | ||||||
45 | 46 | 47 | 48 | ✅ | ||||||||||||||||||||
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | ✅ | ||||||||||||||||
59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | ✅ | |||||||
77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | ✅ | |||||
97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | ✅ |