Trains: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
Line 68: Line 68:


<p>For more applications of this concept, see the [http://www.jsoftware.com/help/learning/09.htm discussion] in the '''Learning J''' manual.</p>
<p>For more applications of this concept, see the [http://www.jsoftware.com/help/learning/09.htm discussion] in the '''Learning J''' manual.</p>
<p>There is also a [[TrainList|list]] of common function expressions and their corresponding Train.</p>

Revision as of 19:44, 4 March 2009

Z←(f g) R is called a Monadic HookZ ≡ R g h R
Z←L (f g) R is called a Dyadic HookZ ≡ L g h R
Z←(f g h) R is called a Monadic ForkZ ≡ (f R) g h R
Z←L (f g h) R is called a Dyadic ForkZ ≡ (L f R) g L h R
Z←(f g h ...) R
Z←L (f g h ...) R
is also defined for longer Trains
L and R are arbitrary arrays, f, g, h, etc, are arbitrary functions of any type: primitive, user-defined, system, and/or derived.


This clever idea from the designers of J is called Trains where a parenthesized sequence of functions (which normally would signal a SYNTAX ERROR) can be interpreted as per the above descriptions. Note that the spacing between functions is for visual purposes only — it has no effect on the interpretation.

For example,

      (,⍎)'2+3'
←→    '2+3',⍎'2+3'
←→    '2+3',5
2+3 5

      avg←(+/ ÷ ⍴) defines a function that computes the average of a numeric vector.
      avg 1 2 3 4
←→    (+/ ÷ ⍴) 1 2 3 4
←→    (+/1 2 3 4) ÷ ⍴1 2 3 4
←→    10 ÷ ,4
2.5

Longer Trains are defined as follows:

(e f g h) ←→ (e (f g h))
(d e f g h) ←→ (d e (f g h))

and in general

Even length: (a b c ...) ←→ (a (b c ...))
Odd length: (a b c ...) ←→ (a b (c ...))

For more applications of this concept, see the discussion in the Learning J manual.

There is also a list of common function expressions and their corresponding Train.