>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Phrases

# 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:

       n0=: i. 6
       n0	      NB. Enter name of noun to display it
    0 1 2 3 4 5

       m1=:    n0=: i. 6
       n0	      NB. Enter name of noun to display it
    0 1 2 3 4 5

       m1=: ^&3
       m1 n0
    0 1 8 27 64 125

       m2=: mean=: +/ % #
       m2 n0
    2.5

       mean m1 n0
    37.53
       m1 n0
    0 1 8 27 64 125

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:

       report=: ? 2 4 3 $ 10	NB. Report for two years of four
     			        NB. quarters of three months
       report
    1 7 4
    5 2 0
    6 6 9
    3 5 8

    0 0 5
    6 0 3
    0 4 6
    5 9 8

       mean report
    0.5 3.5 4.5
    5.5   1 1.5
      3   5 7.5
    4	7   8

       (mean ; mean"2 ; mean"1) report
    +-----------+--------------+-------------------------------+
    |0.5 3.5 4.5|3.75    5 5.25|      4 2.33333       7 5.33333|
    |5.5   1 1.5|2.75 3.25  5.5|1.66667       3 3.33333 7.33333|
    |  3   5 7.5|              |                               |
    |  4   7   8|              |                               |
    +-----------+--------------+-------------------------------+

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:

       9!:3 (5)	  NB. Linear display
       m7
    +/\ -: +/\.&.|.

       9!:3 (2 4 5)	  NB. Box, tree, and linear display
       m7
    +---------+--+------------------+
    |+-----+-+|-:|+----------+--+--+|
    ||+-+-+|\||  ||+-----+--+|&.||.||
    |||+|/|| ||  |||+-+-+|\.||  |  ||
    ||+-+-+| ||  ||||+|/||  ||  |  ||
    |+-----+-+|  |||+-+-+|  ||  |  ||
    |         |  ||+-----+--+|  |  ||
    |         |  |+----------+--+--+|
    +---------+--+------------------+
      +- \ ---- / ---- +
      +- -:
    --|      +- \. --- / --- +
      +- &. --- |.
    +/\ -: +/\.&.|.

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:

       pss=: +/\
       x=: 1 2 3 4 5 6
       pss x	  NB. Subtotals, or partial sums
    1 3 6 10 15 21

       sss=: +/\.
       sss x
    21 20 18 15 11 6

       sss |. x
    21 15 10 6 3 1

       |. sss |. x
    1 3 6 10 15 21

       sss&.|. x
    1 3 6 10 15 21

       <\x	  NB. Box prefix scan
    +-----+-----+-------+---------+-----------+
    |1|1 2|1 2 3|1 2 3 4|1 2 3 4 5|1 2 3 4 5 6|
    +-----+-----+-------+---------+-----------+

       <\.x	  NB. Box suffix scan
    +-----------+---------+-------+-----+---+-+
    |1 2 3 4 5 6|2 3 4 5 6|3 4 5 6|4 5 6|5 6|6|
    +-----------+---------+-------+-----+---+-+

Although an unfamiliar function should first be applied in simple cases, complex cases should be tried as well. For example:

       s=: sort=: /:~
       numb=: 3 1 4 1 [ char=: 'cage'
       poem=: 'i sing','of olaf',:'glad and big'
       (,.s numb);(,.s char);poem;(s poem);s"1 poem
    +------------------------------------------+
    |1|a|i sing      |glad and big|       giins|
    |1|c|of olaf     |i sing      |      affloo|
    |3|e|glad and big|of olaf     |  aabddggiln|
    |4|g|            |            |            |
    +------------------------------------------+

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):

       L
    (3 3$3 1 4 2 0 5 1 4 1)&ip

       L x=: 1 2 5
    25 27 14

       h=: 'L' f.
       h x
    25 27 14

       ip=: -/ . *
       L x
    21 27 _2

       h x
    25 27 14

If, as in the present example, the entity being fixed is a verb, the enclosing quotes may be omitted, as in`L f.` .

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Phrases