home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / j / dos / boxed.txt < prev    next >
Text File  |  1992-12-07  |  30KB  |  911 lines

  1.  
  2.  
  3.  
  4.  
  5.                               Using J's Boxed Arrays
  6.  
  7.                                 Donald B. McIntyre
  8.                               Luachmhor, Church Road
  9.                              Kinfauns, Perth PH2  7LD
  10.                                  Scotland - U.K.
  11.                              Telephone:  0738-86-726
  12.  
  13.  
  14.         Introduction:
  15.  
  16.         APL, which has been an implemented computer language for more than
  17.         25 years, grew out of the concise mathematical notation devised by
  18.         Kenneth Iverson.   J is his powerful new dialect [1].   He has
  19.         defined it in a Dictionary that is an essential reference [2].
  20.         Iverson has extended the language in interesting ways.   He has
  21.         also rationalised features which, benefiting from so many years of
  22.         hindsight, he recognised as anomalies in earlier versions of APL.
  23.         Users accustomed to APL can therefore expect to be puzzled by
  24.         changes of behaviour.   For example, the sum over a table (+/
  25.         table) formerly gave row totals (summing over the last axis), but
  26.         the same expression in J gives column totals (summing over the
  27.         first axis).   The reason is that the table is considered an array
  28.         of items, each row being an item.   The number of items is given
  29.         by the tally (#) or by the first number or head ({.) of the shape
  30.         ($);  a 4 by 12 table (matrix) is looked on as a collection of 4
  31.         lists (vectors), each of length 12.   Consequently the mean (sum
  32.         divided by tally) is the sum down the columns (across the items)
  33.         divided by the number of rows.
  34.  
  35.         The use of square brackets and semicolon in indexing is anomalous;
  36.         e.g. expressions such as  V[I] or M[I;J] do not have the syntax of
  37.         other verbs (functions).   J uses normal syntax;  e.g.  i { v
  38.         (which can be read as "i from v").   In higher rank objects, which
  39.         need pairs (or triplets, etc) of indexes to identify one atom in
  40.         the array, the syntax is the same, but indexes identifying a
  41.         single cell are boxed together.   Examples follow.
  42.  
  43.         Thinking that my experience with J might help others, I have given
  44.         accounts of various applications [4-7].   This further example
  45.         describes one of my first attempts -- the use of boxes to create a
  46.         small database.   Eugene McDonnell responded to my questions in a
  47.         helpful letter, which I included in my paper for APL91 [4].
  48.  
  49.  
  50.         Basic Techniques Relating to Boxes::
  51.  
  52.         APL's index generator (iota) has been extended:
  53.            ]a=. i.2 3
  54.         0 1 2
  55.         3 4 5
  56.  
  57.         After being boxed, this table of 2 rows (each of length 3) behaves
  58.         as an atom (scalar):
  59.  
  60.  
  61.  
  62.  
  63.                                        -1-
  64.  
  65.  
  66.  
  67.  
  68.            $a
  69.         2 3
  70.            ]b=. <i.2 3
  71.         ┌─────┐
  72.         │0 1 2│
  73.         │3 4 5│
  74.         └─────┘
  75.            $b
  76.  
  77.  
  78.         Boxes also result from link (;)
  79.            ]c=. 'mary';'jones';a; 1 3 5
  80.         ┌────┬─────┬─────┬─────┐
  81.         │mary│jones│0 1 2│1 3 5│
  82.         │    │     │3 4 5│     │
  83.         └────┴─────┴─────┴─────┘
  84.         From this list of 4 items we can make a selection:
  85.            $c
  86.         4
  87.            3 1 2 0{c
  88.         ┌─────┬─────┬─────┬────┐
  89.         │1 3 5│jones│0 1 2│mary│
  90.         │     │     │3 4 5│    │
  91.         └─────┴─────┴─────┴────┘
  92.  
  93.         Individual words are boxed by word formation (;:)
  94.            ;:s=. 'here we go gathering nuts in may'
  95.         ┌────┬──┬──┬─────────┬────┬──┬───┐
  96.         │here│we│go│gathering│nuts│in│may│
  97.         └────┴──┴──┴─────────┴────┴──┴───┘
  98.         The length of each word is found by applying tally under open;
  99.         i.e. each box is opened, the tally is taken, and the box is closed
  100.         again.   under (&.) is a conjunction, in this case combining tally
  101.         (#) and open (>).
  102.             wl=. #&.>@;:
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.                                        -2-
  129.  
  130.  
  131.  
  132.  
  133.             wl s
  134.         ┌─┬─┬─┬─┬─┬─┬─┐
  135.         │4│2│2│9│4│2│3│
  136.         └─┴─┴─┴─┴─┴─┴─┘
  137.  
  138.         If f, g, h are verbs to be applied to a list of numbers, y, then
  139.            (f g h) y   is the same as    (f y) g (h y)
  140.  
  141.         This train of three verbs is called a fork, and the mean is an
  142.         example [4-7].   The average word-length is found by applying the
  143.         verb mean after opening the boxes containing word-lengths.
  144.            mean=. +/%#
  145.            mean > wl s
  146.         3.71429
  147.  
  148.         Or, defining the verb mwl we get the mean word-length of any
  149.         string.   This is a tacit definition, a pure functional form, with
  150.         no explicit reference to the arguments.
  151.            mwl=. mean@(>@wl)       NB.  Parentheses are required!
  152.            mwl s
  153.         3.71429
  154.  
  155.         When we open a list of boxes we get a table:
  156.            > ;:s
  157.         here
  158.         we
  159.         go
  160.         gathering
  161.         nuts
  162.         in
  163.         may
  164.  
  165.         The table is alphabetised by the dyadic verb sort (/:) using the
  166.         adverb both (~) so that the left and right arguments are the same.
  167.            sort=. /:~@(>@;:)     NB.  Parentheses are required!
  168.            sort s
  169.         gathering
  170.         go
  171.         here
  172.         in
  173.         may
  174.         nuts
  175.         we
  176.  
  177.         We must distinguish between boxing the list as a whole and boxing
  178.         the individual items.   The rank conjunction (") instructs the box
  179.         verb to enclose elements of specified rank (in this case  atoms):
  180.            v=. 2 3.4 5.67
  181.            <"0 v
  182.         ┌─┬───┬────┐
  183.         │2│3.4│5.67│
  184.         └─┴───┴────┘
  185.         The items (in this case individual numbers) are made into vectors
  186.         (raised from rank-0 to rank-1) by ravel item (,.)
  187.            ,. v
  188.            2
  189.  
  190.  
  191.  
  192.  
  193.                                        -3-
  194.  
  195.  
  196.  
  197.  
  198.          3.4
  199.         5.67
  200.  
  201.         A rank-3 array can be boxed with any rank from 0 to 3:
  202.            ]m=. i.2 3 4
  203.          0  1  2  3
  204.          4  5  6  7
  205.          8  9 10 11
  206.  
  207.         12 13 14 15
  208.         16 17 18 19
  209.         20 21 22 23
  210.            <"2 m
  211.         ┌─────────┬───────────┐
  212.         │0 1  2  3│12 13 14 15│
  213.         │4 5  6  7│16 17 18 19│
  214.         │8 9 10 11│20 21 22 23│
  215.         └─────────┴───────────┘
  216.            <"1 m
  217.         ┌───────────┬───────────┬───────────┐
  218.         │0 1 2 3    │4 5 6 7    │8 9 10 11  │
  219.         ├───────────┼───────────┼───────────┤
  220.         │12 13 14 15│16 17 18 19│20 21 22 23│
  221.         └───────────┴───────────┴───────────┘
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.                                        -4-
  259.  
  260.  
  261.  
  262.  
  263.         Construct a Simple Database:
  264.  
  265.         Use "link" (;) to create the database as an array of boxed
  266.         elements:
  267.            d=.,:'E.E.';'McDonnell';'Palo Alto';27;10000; 8 3 12 25 10
  268.            d=.d,'Ken';'Iverson';'Toronto';55;15000; 4 19 32 1 15 10
  269.            d=.d,'Donald';'McIntyre';'U.K.';61;12000;''
  270.            d=.d,'Roger';'Hui';'Toronto';49;20000; 32 4
  271.            d=.d,'Anthony';'Camacho';'U.K.';45;35000; 19 23 45 4 17 13 5
  272.            d
  273.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  274.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  275.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  276.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  277.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  278.         │Donald │McIntyre │U.K.     │61│12000│                  │
  279.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  280.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  281.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  282.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│
  283.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  284.  
  285.         The columns can be taken as First Name, Family Name, Location,
  286.         Age, and Salary.   The final column illustrates a ragged array
  287.         with one empty cell.   (The content of the database is
  288.         fictitious.)
  289.  
  290.         We can extract columns:
  291.            col=. >@{"1
  292.            locality=. 2&col
  293.            locality d
  294.         Palo Alto
  295.         Toronto
  296.         U.K.
  297.         Toronto
  298.         U.K.
  299.  
  300.         Pronouns might be convenient:
  301.            name=. 1 [ age=. 3 [ salary=. 4
  302.  
  303.         Because x is sorted into an order specified by y,  x /: y  is the
  304.         same as  (/:y) { x  See [2]:
  305.            sort=. ] /: col          NB.   Fork
  306.  
  307.         It is important to remember that right (]) and left ([) are verbs
  308.         and not merely placeholders;  they take arguments and produce
  309.         results.
  310.  
  311.            name sort d
  312.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  313.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│
  314.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  315.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  316.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  317.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  318.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  319.  
  320.  
  321.  
  322.  
  323.                                        -5-
  324.  
  325.  
  326.  
  327.  
  328.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  329.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  330.         │Donald │McIntyre │U.K.     │61│12000│                  │
  331.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  332.  
  333.            salary sort d
  334.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  335.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  336.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  337.         │Donald │McIntyre │U.K.     │61│12000│                  │
  338.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  339.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  340.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  341.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  342.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  343.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│
  344.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  345.  
  346.         To sort on the basis of the ragged column (5), we might use the
  347.         mean:
  348.            mean age col d
  349.         47.4
  350.            mean salary col d
  351.         18400
  352.  
  353.         Using under to define the adverb each
  354.            each=. &.>
  355.            meanr=. mean each@(5&{"1)     NB.  Means in ragged column 5
  356.            meanr d
  357.         ┌────┬────┬─┬──┬──┐
  358.         │11.6│13.5│0│18│18│
  359.         └────┴────┴─┴──┴──┘
  360.  
  361.         Sorting on the basis of the means in the ragged column, and
  362.         appending the means:
  363.            sortr=. ] /: >@meanr
  364.            sortr d,"1 0 meanr d
  365.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┬────┐
  366.         │Donald │McIntyre │U.K.     │61│12000│                  │0   │
  367.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┼────┤
  368.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │11.6│
  369.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┼────┤
  370.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │13.5│
  371.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┼────┤
  372.         │Roger  │Hui      │Toronto  │49│20000│32 4              │18  │
  373.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┼────┤
  374.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│18  │
  375.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┴────┘
  376.  
  377.         Append (,"1 0) is performed here so that rank-1 cells (rows) on
  378.         the left are joined to rank-0 cells (atoms) on the right.
  379.  
  380.         To display the data on people at a given locality, apply the
  381.         adverb each to the verb match (-:).   Because the data are boxed,
  382.         the search-key must be boxed too.   Copy (the dyadic #) does the
  383.         same as compress or replicate in older APL.   The adverb cross (~)
  384.         interchanges the arguments, thus cutting down on parentheses:
  385.  
  386.  
  387.  
  388.                                        -6-
  389.  
  390.  
  391.  
  392.  
  393.  
  394.            d #~ >(<'Toronto') -: each 2{"1  d
  395.         ┌─────┬───────┬───────┬──┬─────┬───────────────┐
  396.         │Ken  │Iverson│Toronto│55│15000│4 19 32 1 15 10│
  397.         ├─────┼───────┼───────┼──┼─────┼───────────────┤
  398.         │Roger│Hui    │Toronto│49│20000│32 4           │
  399.         └─────┴───────┴───────┴──┴─────┴───────────────┘
  400.  
  401.         The following verb makes selections easier:
  402.            place=. ] #~ >@(<@[ -: each 2&{"1 @ ])
  403.  
  404.            'U.K.' place d
  405.         ┌───────┬────────┬────┬──┬─────┬──────────────────┐
  406.         │Donald │McIntyre│U.K.│61│12000│                  │
  407.         ├───────┼────────┼────┼──┼─────┼──────────────────┤
  408.         │Anthony│Camacho │U.K.│45│35000│19 23 45 4 17 13 5│
  409.         └───────┴────────┴────┴──┴─────┴──────────────────┘
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.                                        -7-
  454.  
  455.  
  456.  
  457.  
  458.         Replacement and Insertion:
  459.  
  460.         We first assign a "linear index" to each cell:
  461.               i. $ d
  462.          0  1  2  3  4  5
  463.          6  7  8  9 10 11
  464.         12 13 14 15 16 17
  465.         18 19 20 21 22 23
  466.         24 25 26 27 28 29
  467.  
  468.               li=. [ { i.@$@]      NB.  Linear Index.   Fork
  469.  
  470.               1 4 li d             NB.  Rows 1 and 4
  471.          6  7  8  9 10 11
  472.         24 25 26 27 28 29
  473.  
  474.               (<2 5) li d    NB.  Linear index of cell at row 2 and column
  475.         5
  476.         17
  477.  
  478.               (2 5;4 2) li d     NB.  Scattered indexing
  479.         17 26
  480.  
  481.         The verb from ({) is used to select information.   Placing
  482.         information into an existing cell is a more complicated process.
  483.         We use the adverb amend (}), and we need three pieces of
  484.         information:  the array to be changed;  the indexes identifying
  485.         the affected cells;  and the new data to be inserted.
  486.  
  487.         The indexes will normally be computed from the data, but we can
  488.         arbitrarily select any cell;  e.g. to place an array into the cell
  489.         at row-2 column-5:
  490.  
  491.            x=. 9 8 7 6,:5 4 3 2
  492.            (<x) ((<2 5) li d)}d
  493.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  494.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  495.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  496.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  497.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  498.         │Donald │McIntyre │U.K.     │61│12000│9 8 7 6           │
  499.         │       │         │         │  │     │5 4 3 2           │
  500.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  501.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  502.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  503.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│
  504.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  505.  
  506.         Scattered indexing presents no problem:
  507.            (x;'London') ((2 5;4 2)li d)} d
  508.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  509.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  510.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  511.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  512.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  513.         │Donald │McIntyre │U.K.     │61│12000│9 8 7 6           │
  514.  
  515.  
  516.  
  517.  
  518.                                        -8-
  519.  
  520.  
  521.  
  522.  
  523.         │       │         │         │  │     │5 4 3 2           │
  524.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  525.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  526.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  527.         │Anthony│Camacho  │London   │45│35000│19 23 45 4 17 13 5│
  528.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  529.  
  530.  
  531.         Expansion in General, and a Digression on Reading J:
  532.  
  533.         To insert new items (new rows) the database must be expanded.   In
  534.         older APL the back-slash (\) was used along with a boolean string
  535.         (U), as in U\[0]D to expand along the first axis. J uses the back-
  536.         slash for other operations, but Iverson has defined a verb that
  537.         performs the operation of expansion [4]:
  538.  
  539.               exp=. /:@\:@[{#@[{.]
  540.               1 0 1 exp 7 8
  541.         7 0 8
  542.  
  543.         To someone unfamiliar with the notation, this expression is, of
  544.         course, incomprehensible.   This, however, is not a reason for
  545.         supposing it to be difficult.   Parse any sentence as a step
  546.         towards understanding it.   Characters immediately followed by a
  547.         period (.) or a colon (:) are taken as 2-character symbols.   Make
  548.         this clear by inserting spaces and note the sequence of 11
  549.         symbols:
  550.               exp=. /: @ \: @ [ { # @ [ {. ]
  551.  
  552.         Square brackets ([) and (]) are not punctuation symbols (as round
  553.         parentheses are), but verbs returning their left and right
  554.         arguments respectively.
  555.  
  556.         There being no adverbs (mondadic operators), next identify
  557.         conjunctions (dyadic operators) -- in this case only atop (@) --
  558.         and insert parentheses to show that a conjunction combines the
  559.         item (verb or noun) on either side of it to compose a new verb.
  560.         Put the right parentheses in first:
  561.               exp=. /: @\:) @ [) { # @[) {. ]
  562.  
  563.         Conjunctions have long left-scope and short right-scope;  for this
  564.         reason, put in the left parentheses while working from left to
  565.         right.   Confirm that this gives the same result as before.
  566.               exp=. ((/:@\:)@[) { (#@[) {. ]
  567.  
  568.         The expressions enclosed in parentheses are new verbs resulting
  569.         from the composition of simple verbs, consequently we can now view
  570.         the definition as a sequence of 5 verbs:
  571.  
  572.               p=. /:@\:@[
  573.               q=. {
  574.               r=. #@[
  575.               s=. {.
  576.               t=. ]
  577.  
  578.               1 0 1 (p q r s t) 7 8
  579.  
  580.  
  581.  
  582.  
  583.                                        -9-
  584.  
  585.  
  586.  
  587.  
  588.         7 0 8
  589.  
  590.         Such a sequence of verbs previously had no meaning.   Iverson
  591.         calls it a train:  "an isolated sequence of parts of speech that
  592.         does not resolve to a shorter sequence through the normal
  593.         application of verbs" [2, p.5].   He assigns meanings to trains of
  594.         two verbs (hooks) and three verbs (forks), and by repeated
  595.         resolution to any train of verbs [2, 8, 9].   Thus
  596.               (s t)            NB.   Hook
  597.         ┌─┬─┐
  598.         │s│t│
  599.         └─┴─┘
  600.               (r s t)          NB.   Fork
  601.         ┌─┬─┬─┐
  602.         │r│s│t│
  603.         └─┴─┴─┘
  604.               (q r s t)        NB.   Hook containing a Fork
  605.         ┌─┬───────┐
  606.         │q│┌─┬─┬─┐│
  607.         │ ││r│s│t││
  608.         │ │└─┴─┴─┘│
  609.         └─┴───────┘
  610.               (p q r s t)      NB.   Fork containing a Fork
  611.         ┌─┬─┬───────┐
  612.         │p│q│┌─┬─┬─┐│
  613.         │ │ ││r│s│t││
  614.         │ │ │└─┴─┴─┘│
  615.         └─┴─┴───────┘
  616.  
  617.  
  618.         J provides two tools for investigating parsing.   The directly
  619.         displayed form shows a fork within a fork:
  620.            exp
  621.         ┌───────────────┬─┬──────────────┐
  622.         │┌─────────┬─┬─┐│{│┌───────┬──┬─┐│
  623.         ││┌──┬─┬──┐│@│[││ ││┌─┬─┬─┐│{.│]││
  624.         │││/:│@│\:││ │ ││ │││#│@│[││  │ ││
  625.         ││└──┴─┴──┘│ │ ││ ││└─┴─┴─┘│  │ ││
  626.         │└─────────┴─┴─┘│ │└───────┴──┴─┘│
  627.         └───────────────┴─┴──────────────┘
  628.  
  629.         The conjunction (5!:4) displays a tree representation:
  630.               tree=. 5!:4 @ <
  631.               f=. p q r s t
  632.               tree 'f'
  633.               ┌─ p
  634.               ├─ q
  635.         ─ f ──┤    ┌─ r
  636.               └────┼─ s
  637.                    └─ t
  638.  
  639.            tree 'exp'
  640.                             ┌─ /:
  641.                       ┌─ @ ─┴─ \:
  642.                 ┌─ @ ─┴─ [
  643.                 ├─ {
  644.  
  645.  
  646.  
  647.  
  648.                                        -10-
  649.  
  650.  
  651.  
  652.  
  653.         ─ exp ──┤           ┌─ #
  654.                 │     ┌─ @ ─┴─ [
  655.                 └─────┼─ {.
  656.                       └─ ]
  657.  
  658.         In reading the tree, remember that the conjunction atop (@) makes
  659.         verbs.   Hooks and forks, on the other hand are trains of verbs.
  660.  
  661.         Returning to the original form without parentheses:
  662.               exp=. /: @ \: @ [ { # @ [ {. ]
  663.  
  664.         In Iverson's words:  "Notice how this tacit definition reads in
  665.         English.   The inverse of the downgrade of the left argument
  666.         permutes the (over)take of the right argument by the number of
  667.         items of the left argument" [4, p.270].
  668.  
  669.         Had an adverb (a monadic operator) been present, we would have
  670.         inserted parentheses to show that it modified the verb to its
  671.         left.   This is illustrated by the adverb each in the verb meanr,
  672.         which we used to compute the means of the ragged items in column
  673.         5:
  674.            meanr=. mean each @ (5&{"1)
  675.  
  676.         Note first that parentheses are required to prevent the atop (@)
  677.         from grabbing the 5 to its right.   Next, the conjunction with (&)
  678.         takes the 5 on its left along with the verb from ({) on its right
  679.         to compose a new verb.   The rank conjunction (") takes this
  680.         composed verb (including the 5 that is part of it) and combines it
  681.         with the noun (1) to its right.   The result (5&{"1) is a verb
  682.         that returns the fifth column of the array to which it applies.
  683.  
  684.         each is a defined adverb, which, like any other adverb, modifies
  685.         the verb to its left (mean) to compose a new verb (mean each).
  686.         This verb is the left argument of the conjunction atop.
  687.  
  688.         Had we defined meanr:
  689.            meanr=. +/%# each @ (5&{"1)
  690.  
  691.         then each would have modified tally (#) alone, and the definition
  692.         would have been parsed as a fork, which would have been quite
  693.         wrong:
  694.            (+/) % (# each @ (5&{"1))
  695.  
  696.  
  697.         Adding New Items to the Database:
  698.  
  699.         First expand the array:
  700.            ] e=. 1 0 1 1 0 1 1 exp d
  701.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  702.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  703.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  704.         │       │         │         │  │     │                  │
  705.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  706.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  707.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  708.         │Donald │McIntyre │U.K.     │61│12000│                  │
  709.  
  710.  
  711.  
  712.  
  713.                                        -11-
  714.  
  715.  
  716.  
  717.  
  718.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  719.         │       │         │         │  │     │                  │
  720.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  721.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  722.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  723.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│
  724.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  725.  
  726.         Then amend by inserting the new items one at a time:
  727.            t=. e (1 li d)}~  'Bob';'Bernecky';'Toronto';35;22000; 4 5
  728.            t (4 li d)}~  'Graham';'Woyka';'U.K.';62;35000; 14 31 5 7
  729.  
  730.         ┌───────┬─────────┬─────────┬──┬─────┬──────────────────┐
  731.         │E.E.   │McDonnell│Palo Alto│27│10000│8 3 12 25 10      │
  732.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  733.         │Bob    │Bernecky │Toronto  │35│22000│4 5               │
  734.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  735.         │Ken    │Iverson  │Toronto  │55│15000│4 19 32 1 15 10   │
  736.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  737.         │Donald │McIntyre │U.K.     │61│12000│                  │
  738.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  739.         │Graham │Woyka    │U.K.     │62│35000│14 31 5 7         │
  740.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  741.         │Roger  │Hui      │Toronto  │49│20000│32 4              │
  742.         ├───────┼─────────┼─────────┼──┼─────┼──────────────────┤
  743.         │Anthony│Camacho  │U.K.     │45│35000│19 23 45 4 17 13 5│
  744.         └───────┴─────────┴─────────┴──┴─────┴──────────────────┘
  745.  
  746.         Or define another array, from which to amend the original by
  747.         merging several items in one step:
  748.         x=.('Graham';'Woyka';'U.K.';1;2;3),:'Vin';'Grannell';'Los
  749.         Angeles';4;5;6 7 8
  750.  
  751.            x (1 4 li d)} e
  752.         ┌───────┬─────────┬───────────┬──┬─────┬──────────────────┐
  753.         │E.E.   │McDonnell│Palo Alto  │27│10000│8 3 12 25 10      │
  754.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  755.         │Graham │Woyka    │U.K.       │1 │2    │3                 │
  756.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  757.         │Ken    │Iverson  │Toronto    │55│15000│4 19 32 1 15 10   │
  758.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  759.         │Donald │McIntyre │U.K.       │61│12000│                  │
  760.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  761.         │Vin    │Grannell │Los Angeles│4 │5    │6 7 8             │
  762.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  763.         │Roger  │Hui      │Toronto    │49│20000│32 4              │
  764.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  765.         │Anthony│Camacho  │U.K.       │45│35000│19 23 45 4 17 13 5│
  766.         └───────┴─────────┴───────────┴──┴─────┴──────────────────┘
  767.  
  768.         Or, finally, amend the database by merging a selection from
  769.         another database with equivalent fields:
  770.            e (1 4 li e)}~ 1 0{ x
  771.         ┌───────┬─────────┬───────────┬──┬─────┬──────────────────┐
  772.         │E.E.   │McDonnell│Palo Alto  │27│10000│8 3 12 25 10      │
  773.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  774.  
  775.  
  776.  
  777.  
  778.                                        -12-
  779.  
  780.  
  781.  
  782.  
  783.         │Vin    │Grannell │Los Angeles│4 │5    │6 7 8             │
  784.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  785.         │Ken    │Iverson  │Toronto    │55│15000│4 19 32 1 15 10   │
  786.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  787.         │Donald │McIntyre │U.K.       │61│12000│                  │
  788.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  789.         │Graham │Woyka    │U.K.       │1 │2    │3                 │
  790.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  791.         │Roger  │Hui      │Toronto    │49│20000│32 4              │
  792.         ├───────┼─────────┼───────────┼──┼─────┼──────────────────┤
  793.         │Anthony│Camacho  │U.K.       │45│35000│19 23 45 4 17 13 5│
  794.         └───────┴─────────┴───────────┴──┴─────┴──────────────────┘
  795.  
  796.  
  797.         Acknowledgements:
  798.  
  799.         Kenneth Iverson and Roger Hui provided the language and the
  800.         system.   I am particularly indebted to Kenneth Iverson and Eugene
  801.         McDonnell for sharing their trenchant insight at crucial points in
  802.         my experience with J.
  803.  
  804.         References
  805.  
  806.             [1]    J is available from Iverson Software Inc., 33 Major
  807.                   Street, Toronto, Ontario, Canada M5S  2K9.   Phone (416)
  808.                   925-6096; Fax (416) 488-7559.   This paper was submitted
  809.                   in November 1991.   The examples, originally executed
  810.                   with J Version 3.3 have been revised for compatibility
  811.                   with Version 4.2.
  812.  
  813.             [2]    Kenneth E. Iverson, ISI Dictionary of J, Version 4 with
  814.                   Tutorials, Iverson Software Inc., Toronto (1991) 34pp.
  815.  
  816.             [3]    Kenneth E. Iverson, Arithmetic, Iverson Software Inc.,
  817.                   Toronto (1991) 119pp.
  818.  
  819.             [4]    Donald B. McIntyre, Mastering J, APL91 Conference
  820.                   Proceedings, Stanford, California, August 1991.   APL
  821.                   Quote Quad Vol. 21 Number 4 (August 1991), p.264-273.
  822.                   This paper includes valuable statements by K.E. Iverson
  823.                   and E.E. McDonnell, for which I am grateful.
  824.  
  825.             [5]    Donald B. McIntyre, Language as an Intellectual Tool:
  826.                   From Hieroglyphics to APL, IBM Systems Journal, Vol. 30,
  827.                   Number 4 (1991) 554-581.
  828.  
  829.             [6]    Donald B. McIntyre, Hooks and Forks and the Teaching of
  830.                   Elementary Arithmetic, Vector, Vol. 8, Number 3 (January
  831.                   1992) 101-123.
  832.  
  833.             [7]    Donald B. McIntyre, Using J with External Data:  two
  834.                   examples, Vector, Vol. 8, Number 4 (April 1992) 97-110.
  835.  
  836.             [8]    Kenneth E. Iverson, and E.E. McDonnell, Phrasal Forms,
  837.                   APL89 Conference Proceedings, New York City, August
  838.                   1989.   QuoteQuad, Volume 19, Number 4, (1989) p.197-
  839.                   199.
  840.  
  841.  
  842.  
  843.                                        -13-
  844.  
  845.  
  846.  
  847.  
  848.  
  849.             [9]    Roger K.W. Hui, Kenneth E. Iverson, Eugene E. McDonnell,
  850.                   Tacit Definition, APL91 Conference Proceedings,
  851.                   Stanford, California, August 1991.   APL Quote Quad Vol.
  852.                   21 Number 4 (August 1991), p.264-273.
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.                                        -14-
  909.  
  910.  
  911.