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

load 'system/packages/misc/fndisplay.ijs'

Then, select the type of display you want. We will be using setfnform 'J'

Then, give the names of the verbs you want to use. If you want to assign a rank, you may do so by appending "r to the name:

defverbs 'SumItems plus"0'

Optionally, define any names you want to use as nouns. The value assigned to the noun is the noun's name:

defnouns 'x y'

You are free to use other nouns in expressions, but they will be replaced by their values.

 

With these definitions, you can explore J's evaluations:

+

x (plus) y

--------+

|x plus y|

+--------

+

The result of the evaluation is a description of the evaluation that was performed. The result is displayed in a box so that a sentence with multiple evaluations shows each in its

proper place:

 

+

SumItems"1 i. 2 3

+

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

|SumItems 0 1 2|SumItems 3 4 5| +--------------+--------------+

Here we see that SumItems was applied twice, once on each 1-cell.

fndisplay cannot produce a valid result in cases where the rank of a verb is smaller than the rank of the result-cells of the preceding verb, because then the operation would be performed on part of a result cell, and the result cell is just a descriptive string which cannot be meaningfully subdivided.

In this book, if we give an example that starts with defverbs it is implied that load fndisplay and setfnform 'J' have been executed.

 

If you prefer to see the order of evaluation expressed in functional form like that used

in C, you may issue setfnform 'math' before you execute your sentences:

 

setfnform 'math'

+

1 plus 2 plus y

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

|plus(1,plus(2,y))|

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

+

Negative Verb Rank

Recall that we defined the _1-cell of a noun n to be the cells with rank one less than the rank of n, and similarly for other negative ranks. If a verb is defined with negative rank r, it means as usual that the verb will apply to r-cells if possible, but with r negative the rank of those r-cells will depend on the rank of the operand. After the interpreter decides what rank of cell the verb will be applied to, verbs with negative rank are processed just like verbs of positive rank.

41

+/ "_1 i. 3 0 1 2

Here the _1-cells are atoms, so applying monad SumItems on each one has no effect.

+/ "_1 i. 2 3 3 12

The operand has rank 2, so this expression totals the items in each 1-cell.

+/ "_2 i. 2 2 3 3 12 21 30

The operand has rank 3, so this totals the items in each 1-cell, leaving a 2×2 array of totals.

Verb Execution—How Rank Is Used (Dyads)

We are at last ready to understand the implicit looping that is performed when J processes a dyadic verb. Because a dyadic verb has two ranks (one for each operand), and these two ranks interact with each other as well as the ranks of the operands, you should not read further until you thoroughly understand what we have covered already.

We have learned that the rank conjunction u"n is used to specify the rank of a verb. Since each verb has the potential of being invoked monadically or dyadically, the rank conjunction must specify the ranks for both valences. This requires 3 ranks, since the monad has a single rank and the dyad a left and a right rank. The ranks n may comprise from 1 to 3 items: if 3 ranks are given they are, in order, the monad's rank, the dyad's left rank, and the dyad's right rank. If two ranks are given, the first is the dyad's left rank and the second is used for the dyad's right rank and the rank of the monad. If there is only one item in the list, it is used for all ranks. So, v"0 1 has monad rank 1, dyad left rank 0, and dyad right rank 1 . As usual, J primitives themselves have the ranks shown in the Dictionary.

Processing of a dyad follows the same overall plan as for monads, except that with two operands there are two cell-sizes (call them lr and rr) and two frames (call them lf and rf). If the left and right frames are identical, the operation can be simply described: the lr-cells of the left operand match one-to-one with the rr-cells of the right operand; the dyad is applied to those matched pairs of cells, producing a result for each pair; those results are collected as an array with frame lf. We illustrate this case with an example:

42

(i. 2 2) + i. 2 2

0 2

4 6

The verb has left rank 0, and the left operand has rank 2, 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 2, the right frame is 2 2 .

Think of the left operand

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

as a 2×2 array of 0-cells,

0

1

 

 

 

 

 

 

 

 

0

1

 

and the right operand as a

2

3

 

 

 

 

 

 

 

 

2

3

 

2×2 array of 0-cells:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The corresponding left

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0

 

0

1

1

 

 

 

 

and right operand cells are

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

paired:

 

2

 

2

 

 

3

3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The operation dyad + is

0

 

2

 

 

performed on each pair of

 

 

 

 

 

 

 

 

 

 

cells:

 

4

 

6

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Since each result is an

 

 

0 2

 

atom, and the frame is

 

 

 

2 2, the result is an array

 

 

4 6

 

with shape 2 2

 

 

 

 

 

 

 

Figure 7.

Execution of (i. 2 2) + i. 2 2

 

Using fndisplay, we have

 

 

 

load'system\packages\misc\fndisplay.ijs'

 

setfnform 'J'

 

 

 

 

 

 

defverbs 'plus"0'

 

 

 

 

 

(i. 2 2) plus i. 2 2

 

 

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

 

 

 

 

|0 plus 0|1 plus 1|

 

 

 

 

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

 

 

 

 

|2 plus 2|3 plus 3|

 

 

 

 

+

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

+

 

 

 

 

As you can see, we were correct when we asserted that the sum of two cells of the same shape is taken by adding their respective items.

43

Concatenating Lists: Dyad , (Append)

For a second example we will introduce a new verb, dyad , (the verb is the comma character). x , y creates an array whose leading items are the items of x and whose trailing items are the items of y; in other words, it concatenates x and y .

1 2 3 , 6

1 2 3 6

1 2 3 , 1 2 3

1 2 3 1 2 3

4 , 6

4 6

1 2 3 , 0$6

1 2 3

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

In the last example the items of x and y are 3-element lists, so x , y is a list of 3- element lists, containing the items of x followed by the items of y .

Dyad , has infinite rank, which means that it applies to its operands in their entirety and so its detailed operation is defined not by the implicit looping we have been learning, but instead by the definition of the verb in the Dictionary. The discussion given above describes the operation of dyad , when the operands have identically-shaped items (items, mind you—the shapes of x and y may differ, if one has more items than the other). In a later chapter we will learn about x , y when the items have different shapes; for now we will be dealing with operands that are scalars and lists, for both of which the items are scalars.

Now see if you can figure out what (i. 3 3) ,"1 0 i. 3 will do before reading the explanation that follows:

44