Studio/TasteofJPart2

From J Wiki
Jump to: navigation, search

A Taste of J - Part 2

First Steps

Look at the following J sentences; can you figure them out? The sentences are shown indented 3 spaces; the result, if any, is shown aligned to the left margin.

These simple sentences illustrate some of the core facilities of J, that will be described in this lab.

   tab=: /~

   (];*.tab;!tab) _2+i.7
+---------------+-------------------+---------------+
|_2 _1 0 1 2 3 4| 2  2 0 _2 _2 _6 _4| 1 _1 0 0 0 0 0|
|               | 2  1 0 _1 _2 _3 _4| 0  1 0 0 0 0 0|
|               | 0  0 0  0  0  0  0| 1  1 1 1 1 1 1|
|               |_2 _1 0  1  2  3  4|_2 _1 0 1 2 3 4|
|               |_2 _2 0  2  2  6  4| 3  1 0 0 1 3 6|
|               |_6 _3 0  3  6  3 12|_4 _1 0 0 0 1 4|
|               |_4 _4 0  4  4 12  4| 5  1 0 0 0 0 1|
+---------------+-------------------+---------------+

To start, note that "verbs" (i.e. functions) in J can apply to several data items at once. Indeed, the basic datatype is an array, and J verbs are designed to apply to arrays. For example, the following adds two lists of numbers:

   2 3 5 + 10 20 30
12 23 35

Adverbs

The "adverb" in J takes a verb argument and returns another verb, typically related. The behaviour is much like in English: "run quickly", is the verb "run", modified by the adverb "quickly".

The J adverb / results in a verb that applies to each pair of elements, compare with above:

   2 3 5 +/ 10 20 30
12 22 32
13 23 33
15 25 35

Thus, +/ forms an addition table, and similarly, */ forms a multiplication table:

   2 3 5 */ 10 20 30
20  40  60
30  60  90
50 100 150

Try entering:

   2 3 5 %/ 10 20 30

The adverb ~ applies a verb with the same argument on the left as on the right, thus the following two expressions are equivalent:

   0 1 2 3 + 0 1 2 3
0 2 4 6

   +~ 0 1 2 3
0 2 4 6

We can combine two adverbs. The following applies the +/ addition table with the same argument on left and right:

   +/~ 0 1 2 3
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6

Since /~ is to be used again, we give it a name "tab". This is not required, but is helpful in reading later expressions.

   tab=. /~

   + tab 0 1 2 3
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6

   * tab 0 1 2 3
0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9

Try entering:

   ^ tab 0 1 2 3

Integer

The verb i. (integer) generates the first n numbers.

Try entering:

   i. 3 4

and:

   i. 3 4 5


   i.7
0 1 2 3 4 5 6

Negative Numbers

Negative numbers are shown with a leading underscore character, which is not the same as "-", the minus verb.

   _2+ i.7
_2 _1 0 1 2 3 4

Combining Verbs

Now lets look at the expression in parentheses:

   ];*.tab;!tab

Ignoring the semicolons for the moment, the expression contains 3 verbs:

] is the identity verb:

   ] _2+i.7
_2 _1 0 1 2 3 4

*. is the least common multiple verb, so *.tab is the corresponding table. For example, the LCM of 3 and 4 is 12:

   3 *. 4
12

   *.tab _2+i.7
 2  2 0 _2 _2 _6 _4
 2  1 0 _1 _2 _3 _4
 0  0 0  0  0  0  0
_2 _1 0  1  2  3  4
_2 _2 0  2  2  6  4
_6 _3 0  3  6  3 12
_4 _4 0  4  4 12  4

! is the combinations verb. m!n is the number of ways of taking m combinations of n objects; so !tab is the corresponding table. For example, 2!4 is 6.

Note that if you look at the lower right part of the table, you can see a copy of the triangle of Pascal.

   2!4
6

   !tab _2+i.7
 1 _1 0 0 0 0 0
 0  1 0 0 0 0 0
 1  1 1 1 1 1 1
_2 _1 0 1 2 3 4
 3  1 0 0 1 3 6
_4 _1 0 0 0 1 4
 5  1 0 0 0 0 1

The expression in parentheses:

  ];*.tab;!tab

is therefore seen to be of the form:

  f;g;h

for verbs f g and h . It happens that ; is also a verb, so this expression is a list of 5 verbs!

To understand this, consider a simpler list of 3 verbs commonly seen in mathematics. Suppose f and g are functions, then:

   (f + g) x

is typically defined to be

   f(x) + g(x)

In J, this concept is extended to any type of function. Given verbs f g and h , then:

   (f g h) x

is defined as

   (f x) g (h x)

Example:

   (] + %) 1 2 3 4   NB. % is the reciprocal
2 2.5 3.33333 4.25

A list of 3 verbs together is called a fork, and defines a new verb.

Here is another example. The verb ; links its arguments together, putting each in a box. The following is the fork: identity linked with reciprocal.

   (] ; %) 1 2 3 4
+-------+-------------------+
|1 2 3 4|1 0.5 0.333333 0.25|
+-------+-------------------+

J interprets a list of 5 verbs by creating a fork from the rightmost 3 verbs, then another fork from the new verb and the two remaining verbs.

It may be seen that:

  (];*.tab;!tab)

is a verb that returns the identity; the LCM table; and the combinations table; all linked together.

Lets try it with a different argument:

   (];*.tab;!tab) 3+i.7
+-------------+--------------------+-------------------+
|3 4 5 6 7 8 9| 3 12 15  6 21 24  9|1 4 10 20 35 56  84|
|             |12  4 20 12 28  8 36|0 1  5 15 35 70 126|
|             |15 20  5 30 35 40 45|0 0  1  6 21 56 126|
|             | 6 12 30  6 42 24 18|0 0  0  1  7 28  84|
|             |21 28 35 42  7 56 63|0 0  0  0  1  8  36|
|             |24  8 40 24 56  8 72|0 0  0  0  0  1   9|
|             | 9 36 45 18 63 72  9|0 0  0  0  0  0   1|
+-------------+--------------------+-------------------+

Table Utility

Since verb tables are so useful for exploration, J has a standard utility adverb called "table" that creates a table bordered by its arguments. The verb used to combine the arguments is displayed in the top-left cell of the table.

For example:

   !table 3+i.7             NB. right argument only
+-+-------------------+
|!|3 4  5  6  7  8   9|
+-+-------------------+
|3|1 4 10 20 35 56  84|
|4|0 1  5 15 35 70 126|
|5|0 0  1  6 21 56 126|
|6|0 0  0  1  7 28  84|
|7|0 0  0  0  1  8  36|
|8|0 0  0  0  0  1   9|
|9|0 0  0  0  0  0   1|
+-+-------------------+

   0 1 2 3 !table 3+i.7     NB. left and right arguments
+-+------------------+
|!|3 4  5  6  7  8  9|
+-+------------------+
|0|1 1  1  1  1  1  1|
|1|3 4  5  6  7  8  9|
|2|3 6 10 15 21 28 36|
|3|1 4 10 20 35 56 84|
+-+------------------+

End of lab