next up previous
Next: 2.8 Functional and Imperative Up: 2.7 Procedure Abstraction Previous: 2.7 Procedure Abstraction

2.7.1 Procedural Abstraction using J

In J, functions may be passed as arguments and returned as values. Adverbs are functions whose arguments are functions and results are functions. For example, insert (spelled "/") is an adverb which derives a verb result which is inserted between the items of its argument.

   +/ 10 20 50    NB. sum
80
   */ 10 20 50    NB. product 
10000
   -/ 10 20 50    NB. difference
40
Suppose a is defined by:
   a =: i. 2 3
   a
0 1 2
3 4 5

      +/ a
3 5 7
      */ a
0 4 10
Rank (spelled ", double quote) is a conjunction (a verb producing dyad) produces a result verb (derived from its left input) which is applied to its argument according to the right input of rank. For example, a (defined above) has two rows and 3 columns, and is said to be of rank 2 (2 dimensions).
      +/ " 1 a  NB. plus insert applied to the rank 1 items of a (rows)
3 12  NB. row sum
   */ " 1 a  NB. times insert applied to the rank 1 items of a (rows)
0 60  NB. row product
   +/ " 2 a  NB. plus insert applied to the rank 2 items of a (columns)
3 5 7  NB. column sum
   */ " 2 a  NB. times insert applied to the rank 2 items of a (columns)
0 4 10  NB. column product
J supports a number of other abstractions, too numerous to mention in this paper, such as hooks, forks, trains, function arrays, gerunds, agenda, power and inverse (where defined) for primitive functions as well as explicitly defined functions.


next up previous
Next: 2.8 Functional and Imperative Up: 2.7 Procedure Abstraction Previous: 2.7 Procedure Abstraction
2002-09-27