Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Rich H.J for C programmers.2006.pdf
Скачиваний:
18
Добавлен:
23.08.2013
Размер:
1.79 Mб
Скачать

know when you need it, and you can sweat out the solution the first few times. Here are a few observations that may help when that time comes:

It is always entire cells of the operand with the shorter frame that are replicated. A cell is never tampered with; nothing inside a cell will be replicated. And, it is not the entire shorter-frame operand that is replicated, but cells singly, to match the surplus frame of the other operand.

This fact, that single operand cells are replicated, is implied by the decision that the shorter frame must be a prefix of the longer frame: the single cell is the only unit that can be replicated, since the surplus frame is at the end of the frame rather than the beginning. Take a moment to see that this was a good design decision. Why should the following fail?

1 2 3 + i. 2 3

 

|length

error

+i.2

3

| 1 2

3

The 'length error' means that the operands do not agree, because the frame-prefix rule is not met. Your first thought might be that adding a 3-item list to an array of 2 3-item lists should be something that a fancy language like J would do without complaining; if so, think more deeply. J does give you a way to add lists together—just tell J to apply the verb to lists:

1 2 3 +"1 i. 2 3

1 3 5

4 6 8

Operands in which one shape is a suffix of the other, as in this example, are handled by making the verb have the rank of the lower-rank operand; that single operand cell will then be paired with all the cells of the other operand. By requiring dissimilar frames to match at the beginning, J gives you more control over implicit looping, because each different verb-rank causes different operand cells to be paired. If dissimilar frames matched at the end, the pairing of operand cells would be the same regardless of verbrank.

Order of Execution in Implied Loops

Whenever a verb is applied to an operand whose rank is higher than the verb's rank, an implied loop is created, as we have discussed above. The order in which the verb is applied to the cells is undefined. The order used on one machine may not be that used on another one, and the ordering may not be predictable at all. If your verb has side effects, you must insure that they do not depend on the order of execution.

Current versions of the interpreter apply the verb to cells in order, but that may change in future releases.

A Mistake To Avoid

Do not fall into the error of thinking that v"r is 'v with the rank changed to r'. It is not. Nothing can ever change the rank of the verb vv"r is a new verb which has the rank r. This distinction will become important presently as we discuss nested loops.

49

Consider the verb v"1"2,which is parsed as (v"1)"2 . If v"r changed the rank of v, it would follow that v"1"2 would be 'v with the rank changed to 1 and then to 2', i. e. identical to v"2 . But it is not: actually, v"1"2 applies v"1 on the 2-cells of the operand, while v"2 applies v on those same cells—and we have seen that v and v"1 are very different verbs:

+/"1"2 i. 2 3 4 6 22 38 54 70 86

+/"2 i. 2 3 4 12 15 18 21 48 51 54 57

Summing the 2-cells (+/"2) is not the same as summing the 1-cells within each 2-cell (+/"1"2). Make sure you see why.

 

Ah, you may say, but +/"1"2 is equivalent to +/"1 . You are right for the

monadic case, but not for the dyadic:

4

0

(i.

3

4)

+"1"2 i. 2

3

2

4

6

 

 

 

 

8 10 12

14

 

 

 

 

16 18 20

22

 

 

 

 

12 14 16

18

 

 

 

 

20 22 24

26

 

 

 

 

28 30 32

34

+"1 i. 2 3

4

 

 

(i. 3

4)

 

|length error

i.2

3

4

|

(i.3

4)

+"1

Dyad +"1"2 is executed as (+"1)"2, i. e. it has rank 2. So, there is only one 2-cell of the left operand i. 3 4, and that cell is replicated to match the shape of the right operand. The operands then agree, and the 1-cells can be added. Trying to add the 1-cells directly with +"1 fails, because the frames of the operands with respect to 1-cells do not agree.

The situation becomes even more complicated if the assigned left and right ranks are not the same. My advice to you is simple: remember that u"r is a new verb that executes u on r-cells.

50