1A. Conventions

J is an executable mathematical language available on a wide range of computers.
This book presents a collection of J phrases, useful to beginners in learning the language,
and of continuing use to practical programmers.

Each phrase is executable and includes assignment to a name whose numeric part
is unique within the section.
The literal part of the name indicates the class of the phrase as follows:

a adverb

m monad

c conjunction

n noun

d dyad

v ambivalent verb

Some phrases include assignment of a mnemonic name that is not necessarily unique.
Each is accompanied by a brief statement of its purpose. Thus:

n0=: i. 6

List of first six non-negative integers

m1=: ^&3

Cube

m2=: mean=: +/ % #

Arithmetic mean

d3=: $,:

x copies of y

m4=: <.@(0.5&+)

Round

m5=: =+

Test for real number

m6=: (<0 _1)&C.

Swap leading and final items

m7=: +/\ -: +/\.&.|.

Prefix sum scan is suffix under reversal

A beginner may find it useful to simply browse through,
and enter, various portions of the phrase lists.
In so doing, she may best skip those whose purpose is described by unfamiliar terms,
and concentrate first on those phrases described by words that are familiar,
or at least to be found in an English dictionary.

Complete definitions of the primitives used (such as &
in m1) may be found in [5],
but it is probably more fruitful and more fun to first experiment
by entering a phrase together with a suitable argument to see it in action,
and then experiment further by entering what may appear to be meaningful variants.
You may also wish to annotate these experiments
(but do not enter the annotation on the computer unless you preface it
by NB.). For example:

Experimenting with an unfamiliar language is bound to lead to
some puzzling results:

^&3 y=: 0 1 2 3 4 NB. Power with 3 is the cube
0 1 8 27 64
^&3 0 1 2 3 4 NB. A puzzling result
^&3 0 1 2 3 4
^&3 (0 1 2 3 4) NB. The list must be isolated from the 3 of the phrase
0 1 8 27 64 NB. so that it does not attach to it to form a longer list

Spend a little time trying to understand such problems,
but do not let them deter you from further experiments or detain you very long;
it is better to forge ahead and return to them, or perhaps forget them as they
will probably clear up in the course of further work.

In experimenting with phrases,
begin with the simplest arguments that occur to you,
but progress to more complex ones to explore the potential of the phrase.
For example, m4 appears useless when applied to an integer,
useful when applied to a fraction,
and perhaps more interesting when applied to a negative fraction.
Similarly, m5 is uninteresting when applied to real numbers,
but it (as well as the primitive +) is useful
on complex numbers such as 2j3 and %:_1
(the square root of negative one).

Phrases such as m2 and m6 begin to show interesting
results only when applied to lists, such as in m6 'ABCDabcd'.
They should also be tried on tables (matrices) and reports (higher-rank arrays)
such as i. 3 4 and i.3 4 5
and 2 3 4$'ABCDEFG' .
Also try expressions such
as m6"2 i.3 4 5 .For example:

Parameters that occur in phrases (such as the 3 in m1)
invite further experiments. Substitute both positive and negative integers
for the parameter in m1; experiment to determine the range of
permissible values,
and try to state in English the general definition of the
primitive ^. Also see the relevant section on permutations.

Although the d in the name of the phrase d3 indicated that it was for dyadic use only, it will also be found to give a (perhaps unintelligible) result on a single argument as well. For example, try 3 d3 2 3 and d3 2 3. Thus the d in the name of the phrase merely suggests its intended use, and does nothing to prevent its use as a monad. However, a verb so restricted can be defined by f=: [: : d3.

The definition of a verb, adverb, or conjunction may be displayed in any of three forms:
box, tree, or linear (the form mainly used herein).
The form may be chosen from the view menu,
or by the foreign conjunction 9!:3 as illustrated below:

It is sometimes helpful to define and experiment with parts of a phrase.
For example, the tautology m7 asserts the equivalence of the
prefix sum scan +/\ and
the suffix sum scan +/\. under reversal.
Thus:

Certain utilities included in a J system are normally loaded automatically;
if not, they can be invoked by entering 0!:0 <'profile.ijs' .
For example:

names ''
d3 m1 m2 m4 m5 m6 m7 mean n0

The response should include all names defined by your experiments.

Presence of these utilities is assumed throughout.

Fixing Definitions

The use of one or more phrases as components in the definition
of another phrase may simplify and clarify the latter definition.
For example,
Parity & Symmetry includes:

n16=: m=: 3 1 4,2 0 5,:1 4 1

A 3-by-3 matrix

d17=: ip=: +/ . *

Inner (matrix) product

m18=: L=: m&ip

A linear function

However, re-definition of a component phrase may inadvertently
alter the definition of the phrase in which it is used.
This may be avoided by using the adverb f. to fix a definition,
perhaps assigning a new name to it. Thus
(assuming the foregoing three definitions to have been made):