A train is a compound function made up of a series of functions. It's written as an isolated expression (surrounded by parentheses or named) ending in a function. Defined by Ken Iverson and Eugene McDonnell in 1988 and added to Dyalog APL in 2014, trains are considered important for tacit programming and a characteristic of modern APL.
⍵ refer to the arguments of the train.
h are functions (which themselves can be tacit or not), and
A is an array. The arguments are processed by the following rules:
A 3-train is a fork, so denoted because its structure resembles a three-tines fork, or a three-pronged pitchfork. The two outer functions are applied first, and their results are used as arguments to the middle function:
(f g h) ⍵
( f ⍵) g ( h ⍵)
⍺ (f g h) ⍵
(⍺ f ⍵) g (⍺ h ⍵)
The left tine of a fork can be an array:
(A g h) ⍵
A g ( h ⍵)
⍺ (A g h) ⍵
A g (⍺ h ⍵)
(g h) ⍵
g ( h ⍵)
⍺ (g h) ⍵
g (⍺ h ⍵)
J instead defines the 2-train as a hook, equivalent to the function derived using the Withe operator. The left function is always applied dyadically, taking as right argument, the result of applying the right function on the right argument. If there is no left argument, the sole argument is used also as left argument:
(g h) ⍵
⍵ g (h ⍵)
⍺ (g h) ⍵
⍺ g (h ⍵)
Problems caused by function-operator overloading
Trains that use a hybrid function-operator in its function role can run into the problems with the hybrid being parsed as a monadic operator instead of as a function. This happens when a function appears to the immediate left of the hybrid, causing this function to be bound as the hybrid's operand — the hybrid taking on an operator role — rather than supplying a left argument or post-processing the result.
For example, the attempted fork
f/h is actually parsed as the atop
(f/)h and the attempted atop
f/ is actually parsed as a Windowed Reduction. There are multiple ways to mitigate this issue. For example, the fork can be enforced using the Atop operator by applying identity to the hybrid's result as
f⊢⍤/h and the atop can be enforced by using the explicit Atop operator instead of a 2-train;
No problem presents when left argument is supplied as an array (literal or by name reference) and when the hybrid is the leftmost token. For example,
1 0 1/⌽ and
/,⊃ are parsed as forks.
- APL Cultivation: Transcribing to and reading trains
- gitonthescene: Forks: Spoon fed
- gitonthescene: Training day
- APLtrainer: How to read trains in Dyalog APL code
- APLtrainer: Function trains in APL
- Dyalog webinar: Train Spotting in Dyalog APL
- Dyalog '13: Train Spotting in Version 14.0
- dzaima/APL: Differences from Dyalog APL. Retrieved 09 Jan 2020.