In the J programming language
, one can concatenate verb
s directly, creating composite
verbs which, unsurprisingly, do everything their component
verbs do. Such a composite verb
is called a train
One might expect that concatenation would be equivalent to function composition, in the mathematical (or functional programming) sense. However, this is not the case; composition operators already exists in J: Compose (&) and Atop (@). If one wants the square of the conjugate of v, this can be obtained using (*: @ +) v, which is equivalent to *: (+ v).
Instead, trains are broken down into their composite forks and hooks. Specifically, the J interpreter counts the number of verbs in a train. If it is even, the left end of the train is a hook. For example, in the following, assume that k is an integer:
(a1 a2 ... a2k-1 a2k) v is equivalent to
(a1 (a2 ... a2k-1 a2k)) v is equivalent to
v a1 ((a2 ... a2k-1 a2k) v) (see the schema for monadic hook for explanation)
If the number of chained verbs is odd, then it is evaluated as a fork:
(a1 a2 a3 ... a2k a2k+1) v is equivalent to
(a1 a2 (a3 ... a2k a2k+1)) v is equivalent to
(a1 v) a2 ((a3 ... a2k a2k+1) v) (again, see the schema for monadic fork).
Extended trains are an elegant way to create verbs using tacit definition (as opposed to explicit). Avoiding the naming of arguments to the functions allows trains to be combined without the syntactic hassle of argument passage (contrast with C, Java, and nearly all other programming languages in use today). This advantage is similar to that obtained by using a stack-based language like FORTH, and adds to the mathematical clarity of the model.
For a simple example of a train with length 4, see fork; for a lot more examples of extended trains, see J Phrases, freely available from the J homepage in HTML and packaged with every distribution of J. For details on how to keep trains unbroken for long lengths without using parentheses, see cap ([:).