Trains: Difference between revisions
No edit summary |
Sudleyplace (talk | contribs) No edit summary |
||
(3 intermediate revisions by one other user not shown) | |||
Line 7: | Line 7: | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
<td>is called a '''Monadic Hook''' — <apll>Z ≡ R g | <td>is called a '''Monadic Hook''' — <apll>Z ≡ R f g R</apll></td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
Line 13: | Line 13: | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
<td>is called a '''Dyadic Hook''' — <apll>Z ≡ L g | <td>is called a '''Dyadic Hook''' — <apll>Z ≡ L f g R</apll></td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
Line 37: | Line 37: | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td><apll>L</apll> and <apll>R</apll> are arbitrary arrays, <apll>f</apll>, <apll>g</apll>, <apll>h</apll>, etc, are arbitrary functions of any type: primitive, user-defined, system, and/or derived.</td> | <td><apll>L</apll> and <apll>R</apll> are arbitrary arrays, <apll>f</apll>, <apll>g</apll>, <apll>h</apll>, etc, are arbitrary functions of any type: primitive, user-defined, system, anonymous, and/or derived.</td> | ||
</tr> | </tr> | ||
</table> | </table> | ||
Line 47: | Line 47: | ||
<p>For example,</p> | <p>For example,</p> | ||
<apll> | <apll> (,⍎)'2+3'</apll><br /> | ||
<apll>←→ | <apll>←→ '2+3',⍎'2+3'</apll><br /> | ||
<apll>←→ | <apll>←→ '2+3',5</apll><br /> | ||
<apll>2+3 5</apll><br /> | <apll>2+3 5</apll><br /> | ||
<apll> | <apll> avg{is}(+⌿ ÷ ≢)</apll> defines a function that computes the average of a numeric vector.<br /> | ||
<apll> | <apll> avg 1 2 3 4</apll><br /> | ||
<apll>←→ | <apll>←→ (+⌿ ÷ ≢) 1 2 3 4</apll><br /> | ||
<apll>←→ | <apll>←→ (+⌿1 2 3 4) ÷ ≢1 2 3 4</apll><br /> | ||
<apll>←→ | <apll>←→ 10 ÷ 4</apll><br /> | ||
<apll>2.5</apll> | <apll>2.5</apll> | ||
<p>Longer Trains are defined as follows:</p> | <p>Longer Trains are defined as follows:</p> | ||
<apll> | <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 /> | ||
Line 67: | Line 67: | ||
{|border="0" | {|border="0" | ||
|Even length: || <apll>(a b c ...) ←→ (a (b | |Even length: || <apll>(a b c ...) ←→ (a (b c ...))</apll> | ||
|- | |- | ||
|Odd length: || <apll>(a b c ...) ←→ (a | |Odd length: || <apll>(a b c ...) ←→ (a b (c ...))</apll> | ||
|} | |} | ||
Latest revision as of 14:32, 15 April 2018
|
||||||||||||||||||||
L and R are arbitrary arrays, f, g, h, etc, are arbitrary functions of any type: primitive, user-defined, system, anonymous, 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.