Trains: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
<table border="0" cellpadding="5" cellspacing="0" summary=""> | <table border="0" cellpadding="5" cellspacing="0" summary=""> | ||
<tr> | <tr> | ||
<td><apll>Z←(f g) R</apll></td> | <td><apll>Z← (f g) R</apll></td> | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
Line 16: | Line 16: | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td><apll>Z←(f g h) R</apll></td> | <td><apll>Z← (f g h) R</apll></td> | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
Line 28: | Line 28: | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td><apll>Z←(f g h ...) R</apll><br /><apll>Z←L (f g h ...) R</apll></td> | <td><apll>Z← (f g h ...) R</apll><br /><apll>Z←L (f g h ...) R</apll></td> | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
Line 41: | Line 41: | ||
</table> | </table> | ||
<br /> | <br /> | ||
<p>This clever idea from the designers of J is called [http://www.jsoftware.com/help/dictionary/dictf.htm 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.</p> | <p>This clever idea from the designers of J is called [http://www.jsoftware.com/help/dictionary/dictf.htm Trains] where a parenthesized sequence of functions (which normally would signal a SYNTAX ERROR) can be interpreted as per the above descriptions. Very nicely, they fit into and extend the spectrum of function definition syntax from user-defined to dynamic to trains to operator expressions. They are another and very interesting form of functional programming.</p> | ||
<p>Note that the spacing between functions is for visual purposes only — it has no effect on the interpretation.</p> | |||
<p>For example,</p> | <p>For example,</p> | ||
Line 59: | Line 61: | ||
<p>Longer Trains are defined as follows:</p> | <p>Longer Trains are defined as follows:</p> | ||
<apll>(e f g h) ←→ (e (f g h))</apll><br /> | <apll> (e f g h) ←→ (e (f g h))</apll><br /> | ||
<apll>(d e f g h) ←→ (d e (f g h))</apll><br /> | <apll>(d e f g h) ←→ (d e (f g h))</apll><br /> | ||
and in general | and in general | ||
Even length: <apll>(a b c ...) ←→ (a (b c ...))</apll> | {|border="0" | ||
Odd length: <apll>(a b c ...) ←→ (a b (c ...))</apll> | |Even length: || <apll>(a b c ...) ←→ (a (b c ...))</apll> | ||
|- | |||
|Odd length: || <apll>(a b c ...) ←→ (a b (c ...))</apll> | |||
|} | |||
<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 series of [[Train_Tables|tables]] of common function expressions and their corresponding Train.</p> | <p>There is also a series of [[Train_Tables|tables]] of common function expressions and their corresponding Train.</p> |
Revision as of 19:08, 7 March 2009
|
||||||||||||||||||||
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. Very nicely, they fit into and extend the spectrum of function definition syntax from user-defined to dynamic to trains to operator expressions. They are another and very interesting form of functional programming.
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 series of tables of common function expressions and their corresponding Train.