Essays/The Essentials of J

From J Wiki
Jump to: navigation, search

by Kenneth E. Iverson, presented at the J User Conference, 1996-06-24

We begin with an introduction to all aspects of the J language that are essential to an understanding of subsequent lectures. This is not an exhaustive treatment of J, but all speakers have been asked to assume no further knowledge, and to introduce in context any special uses of notation that might otherwise prove puzzling. Similar brief introductions will be provided for other aspects of the J system: the user interface, and interfaces to other languages and systems.

Moreover, computers, tutors, and tutorial material will be available in a separate room throughout the conference for any who wish to extend their mastery of the J system.

Lists, Tables, Lists of Tables etc. (Reports)

   b=: 0 1 2 3 4 5

   b*b
0 1 4 9 16 25

   b;b*b
┌───────────┬─────────────┐
│0 1 2 3 4 5│0 1 4 9 16 25│
└───────────┴─────────────┘

Operators: Apply to functions to produce related functions

   b
0 1 2 3 4 5

   b */ b
0 0  0  0  0  0
0 1  2  3  4  5
0 2  4  6  8 10
0 3  6  9 12 15
0 4  8 12 16 20
0 5 10 15 20 25

   b (*/ ; </ ; <./ ; =/) b
┌───────────────┬───────────┬───────────┬───────────┐
│0 0  0  0  0  0│0 1 1 1 1 1│0 0 0 0 0 0│1 0 0 0 0 0│
│0 1  2  3  4  5│0 0 1 1 1 1│0 1 1 1 1 1│0 1 0 0 0 0│
│0 2  4  6  8 10│0 0 0 1 1 1│0 1 2 2 2 2│0 0 1 0 0 0│
│0 3  6  9 12 15│0 0 0 0 1 1│0 1 2 3 3 3│0 0 0 1 0 0│
│0 4  8 12 16 20│0 0 0 0 0 1│0 1 2 3 4 4│0 0 0 0 1 0│
│0 5 10 15 20 25│0 0 0 0 0 0│0 1 2 3 4 5│0 0 0 0 0 1│
└───────────────┴───────────┴───────────┴───────────┘

This use of the symbol = as a relation shows why it was necessary to use a different symbol (=:) for name assignment in the expression b=: 0 1 2 3 4 5 .

Ambivalence

   a=: 5 4 3 2 1 0
   b
0 1 2 3 4 5

   a-b
5 3 1 _1 _3 _5

   -b
0 _1 _2 _3 _4 _5

   a%b
_ 4 1.5 0.666667 0.25 0

   %b
_ 1 0.5 0.333333 0.25 0.2

   a^b
1 4 9 8 1 0

   ^b
1 2.71828 7.38906 20.0855 54.5982 148.413

   b +/ b
0 1 2 3 4  5
1 2 3 4 5  6
2 3 4 5 6  7
3 4 5 6 7  8
4 5 6 7 8  9
5 6 7 8 9 10

   +/ b
15

   sum=: +/
   sum b
15

Compositions

   # c=: 2 3 5 7 1 6
6
   (sum % #) c
4
   mean=: (sum % #)
   mean c=: 2 3 5 7 1 6
4

   center=: (] - mean)
   center c
_2 _1 1 3 _3 2

   mean center c
0

   *: center c
4 1 1 9 9 4

   variance=: (sum@:*:@:center)
   variance c
28

   deviation=: %:@:variance
   deviation c
5.2915

   deviation
%:@:variance

   D=: deviation f.
   D
%:@:(+/@:*:@:(] - +/ % #))

   ^. 10 20 100
2.30259 2.99573 4.60517

   10 ^. 10 20 100
1 1.30103 2

   log=: 10&^.
   log 10 20 100
1 1.30103 2

Cell and Rank

We begin with an array rep of rank 3 that might represent a report for two years of four quarters of three months each:

   rep=: i. 2 4 3
   rep
 0  1  2
 3  4  5
 6  7  8
 9 10 11

12 13 14
15 16 17
18 19 20
21 22 23

Each of the two tables is a rank-2 cell of the report; each of the rows is a rank-1 cell; each number is a rank-0 cell; and the entire report itself is a rank-3 cell. The rank operator " may be used to apply any function to all cells of a specified rank. We will use the reversal function to illustrate:

   rev=: |.
   (] ; rev ; rev"2 ; rev"1) rep
┌────────┬────────┬────────┬────────┐
│ 0  1  2│12 13 14│ 9 10 11│ 2  1  0│
│ 3  4  5│15 16 17│ 6  7  8│ 5  4  3│
│ 6  7  8│18 19 20│ 3  4  5│ 8  7  6│
│ 9 10 11│21 22 23│ 0  1  2│11 10  9│
│        │        │        │        │
│12 13 14│ 0  1  2│21 22 23│14 13 12│
│15 16 17│ 3  4  5│18 19 20│17 16 15│
│18 19 20│ 6  7  8│15 16 17│20 19 18│
│21 22 23│ 9 10 11│12 13 14│23 22 21│
└────────┴────────┴────────┴────────┘

Vocabulary

There are many more facilities in the language, but don't try to memorize them any more than you would try to memorize a dictionary to learn a natural language. Do scan the one-page vocabulary so as to be aware of the existence of various facilities. For example, the entries for sort and grade might suggest experimentation:

   p=: 3 1 4 1 5 9

   /: p
1 3 0 2 4 5

   p /: p
1 1 3 4 5 9

   /:~ p
1 1 3 4 5 9

   sort=: /:~

   h=: 'do you love me'

   sort h
   deelmooouvy

   i=: 'or do you not'
   j=: 'you told me once'
   k=: 'but I forgot'
   poem=: h,i,j,:k

   (] ; sort ; sort"1) poem
┌────────────────┬────────────────┬────────────────┐
│do you love me  │but I forgot    │     deelmooouvy│
│or do you not   │do you love me  │      dnoooortuy│
│you told me once│or do you not   │   cdeelmnoootuy│
│but I forgot    │you told me once│      Ibfgoorttu│
└────────────────┴────────────────┴────────────────┘