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

The verb (dyad ,"1 0) has left rank 1, and the left operand has rank 2, so the operation will be applied to 1-cells of the left operand. The verb has right rank 0, and the right operand has rank 1, so the operation will be applied to 0-cells of the right operand. The left frame is 3, the right frame is 3 .

Think of the left operand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

as a list of 3 1-cells, and

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 1 2

3 4 5

 

6 7 8

 

 

 

 

0

1

 

2

 

 

the right operand as a list

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

of 3 0-cells:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The corresponding left

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

and right operand cells are

 

0 1 2

 

0

 

 

3 4 5

1

 

 

 

6 7 8

 

2

 

 

paired:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The operation dyad , is

performed on each pair of 0 1 2 0 3 4 5 1 6 7 8 2 cells:

 

Since each result is a

0 1

2 0

 

1-cell of shape 4, and the

 

3 4

5 1

 

frame is 3, the result is an

 

6 7

8 2

 

array with shape 3 4

 

 

 

 

Figure 8.

Execution of (i. 3 3) ,"1 0 i. 3

 

defverbs 'comma'

 

 

+

(i. 3 3) comma"1 0 i. 3

+

-------------+-------------

+-------------

|0 1 2 comma 0|3 4 5 comma 1|6 7 8 comma 2| +-------------+-------------+-------------+

(i. 3 3) ,"1 0 i. 3 0 1 2 0 3 4 5 1 6 7 8 2

When Dyad Frames Differ: Operand Agreement

The processing of dyads has an extra step not present for monads, namely the pairing of corresponding cells of the left and right operands. As long as the frames lf and rf are the same, as in the examples so far, this is straightforward. If the frames are different, J may still be able to pair left and right cells, using another level of implicit looping, one that provides considerable additional programming power. The formal description that

45

follows is not easy to follow—you might want to skim over it and read it in detail after you have studied the examples that follow.

J requires that one of the frames be a prefix of the other (if the frames are identical, each is a prefix of the other and all the following reduces to the simple case we have studied). The common frame cf is the part of the frames that is identical, namely the shorter of the two frames; its length is designated rcf. If we look at the cells of the operands relative to this common frame (i. e. the (-rcf)-cells), we see that for the operand with the shorter frame, these cells are exactly the rank that will be operated on, while for the operand with the longer frame, each (-rcf)-cell contains multiple operand cells.

First, the (-rcf)-cells of the two operands are paired one-to-one (because they have the same frame), leaving each shorter-frame operand cell paired with a longer-frame (-rcf)-cell. Then, the longer-frame (-rcf)-cells are broken up into operand cells, with each operand cell being paired with a copy of the shorter-frame operand cell that was paired with the (-rcf)-cell (an equivalent statement is that the cells of the shorter-frame operand are replicated to match the surplus frame of the longer-frame operand). This completes the pairing of operand cells, and the operation is then performed on the paired operand cells, and collected using the longer frame. Maybe some examples will help.

46

100 200 + i. 2 3

100 101 102

203 204 205

 

The verb (dyad +) has left rank 0, and the left operand has rank 1, so the operation

 

will be applied to 0-cells of the left operand. The verb has right rank 0, and the

 

right operand has rank 2, so the operation will be applied to 0-cells of the right

 

operand. The left frame is 2, the right frame is 2 3 .

 

 

 

 

 

The common frame is 2, with

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

length 1, so think of each operand

 

100

200

 

 

 

 

 

 

 

0 1 2

 

3 4 5

 

 

 

 

as a list of 2

_1-cells

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The _1-cells of the operands are

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

100

 

0

1 2

 

 

 

200

3 4 5

 

 

 

 

 

 

paired:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The longer-frame operand (the right

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

one) is broken up into operand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

100

 

0

 

 

 

100

 

1

 

 

100

 

2

 

 

 

 

0-cells, each being paired with a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

copy of the shorter-frame operand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

200

 

3

 

 

 

200

 

4

 

 

200

 

5

 

 

 

 

cell. Each paired _1-cell becomes a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

row of paired operand cells:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The operation dyad

+ is performed

 

100

101

 

102

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

on each pair of cells:

 

203

204

 

205

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Since each result is an atom, and

 

 

 

 

 

 

 

 

100

101

102

 

 

 

 

 

the longer frame is 2 3, the result

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

203

204

205

 

 

 

 

 

is an array with shape 2 3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 9. Execution of 100 200 + i. 2 3

 

 

 

 

 

 

 

 

 

 

defverbs 'plus"0'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+

100 200 plus i. 2 3

 

 

+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+----------

+----------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

|100 plus 0|100 plus 1|100 plus 2|

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+----------

+----------

+----------

 

 

 

+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

|200 plus 3|200 plus 4|200 plus 5|

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+----------

+----------

+----------

 

 

 

+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The simplest and most common case of different-length frames is when the shorter frame is of zero length; in other words, when one of the operands has only one cell. In that case, the single cell is replicated to match every cell of the longer operand. An easy way to force an operand to be viewed as a single cell is to make the verb have infinite rank for that operand. This is not a special case—the behavior follows from the rules already given—but it's worth an example:

47

'abc' ,"_ 0 'defg' abcd

abce abcf abcg

The verb (dyad ,"_ 0) has left rank _, and the left operand has rank 1, so the operation will be applied to 1-cells of the left operand. The verb has right rank 0, and the right operand has rank 1, so the operation will be applied to 0-cells of the right operand. The left frame is (empty), the right frame is 4 .

The common frame is

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(empty), with length 0, so

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

abc

 

 

 

 

 

 

 

 

 

 

 

 

 

 

defg

 

 

 

 

 

 

take each operand in its

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

entirety:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The cells of the operands

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

abc

defg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

are paired:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The longer-frame operand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(the right one) is broken up

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

into operand 0-cells, each

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

abc

d

 

 

 

abc

e

 

 

 

 

abc

f

 

 

abc

g

 

 

being paired with a copy of

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

the shorter-frame operand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

cell:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The operation dyad , is

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

abcd

abce

 

abcf

abcg

 

 

 

 

 

 

 

 

performed on each pair of

 

 

 

 

 

 

 

 

 

 

cells:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Since each result is a 1-cell

 

 

 

 

 

 

 

 

 

 

 

 

 

abcd

 

 

 

 

 

 

with length 4, and the

 

 

 

 

 

 

 

 

 

 

 

 

 

abce

 

 

 

 

 

 

longer frame is 4, the result

 

 

 

 

 

 

 

 

 

 

 

 

 

abcf

 

 

 

 

 

 

is an array with shape 4 4

 

 

 

 

 

 

 

 

 

 

 

 

 

abcg

 

 

 

 

 

 

Figure 10. Execution of 'abc' ,"_ 0 'defg'

 

 

 

 

 

 

defverbs 'comma'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'abc' comma"_ 0 'defg'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+-----------+-----------+-----------+-----------+ |abc comma d|abc comma e|abc comma f|abc comma g| +-----------+-----------+-----------+-----------+

You must thoroughly understand this example, where one operand has only one cell, because it occurs frequently. The handling of the general case of dissimilar frames is uncommon enough that you do not need to understand it perfectly right now—you'll

48