J Labs

A Taste of J (2)

(1 of 18) 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.


In [ ]:
tab=: /~

In [ ]:
(];*.tab;!tab) _2+i.7

(2 of 18) First Steps (ctd)

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:


In [ ]:
2 3 5 + 10 20 30

(3 of 18) 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:


In [ ]:
2 3 5 +/ 10 20 30

(4 of 18) Adverbs (ctd)

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


In [ ]:
2 3 5 */ 10 20 30

(5 of 18) Adverbs (ctd)

Try entering:

```2 3 5 %/ 10 20 30```

(6 of 18) Adverbs (ctd)

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


In [ ]:
0 1 2 3 + 0 1 2 3

In [ ]:
+~ 0 1 2 3

(7 of 18) Adverbs (ctd)

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


In [ ]:
+/~ 0 1 2 3

(8 of 18) Adverbs (ctd)

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


In [ ]:
tab=. /~

In [ ]:
+ tab 0 1 2 3

In [ ]:
* tab 0 1 2 3

(9 of 18) Adverbs (ctd)

Try entering:

```^ tab 0 1 2 3```

(10 of 18) Integer

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

Try entering:

```i. 3 4```

```i. 3 4 5```


In [ ]:
i.7

(11 of 18) Negative Numbers

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


In [ ]:
_2+ i.7

(12 of 18) 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:


In [ ]:
] _2+i.7

(13 of 18) Combining Verbs (ctd)

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


In [ ]:
3 *. 4

In [ ]:
*.tab _2+i.7

(14 of 18) Combining Verbs (ctd)

! 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.


In [ ]:
2!4

In [ ]:
!tab _2+i.7

(15 of 18) Combining Verbs (ctd)

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:


In [ ]:
(] + %) 1 2 3 4   NB. % is the reciprocal

(16 of 18) Combining Verbs (ctd)

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.


In [ ]:
(] ; %) 1 2 3 4

(17 of 18) Combining Verbs (ctd)

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:


In [ ]:
(];*.tab;!tab) 3+i.7

(18 of 18) 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.

For example:


In [ ]:
!table 3+i.7             NB. right argument only

In [ ]:
0 1 2 3 !table 3+i.7     NB. left and right arguments

End of Lab


In [ ]: