Train: Difference between revisions
(Separate from tacit programming page) Tag: Removed redirect |
(→References: External links) |
||
Line 42: | Line 42: | ||
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, <source lang=apl inline>1 0 1/⌽</source> and <source lang=apl inline>/,⊃</source> are parsed as forks. | 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, <source lang=apl inline>1 0 1/⌽</source> and <source lang=apl inline>/,⊃</source> are parsed as forks. | ||
== External links == | |||
=== Documentation === | |||
* [https://help.dyalog.com/latest/Content/Language/Introduction/Trains.htm Dyalog] | |||
* [http://wiki.nars2000.org/index.php?title=Trains NARS2000] | |||
* [https://mlochbaum.github.io/BQN/doc/train.html BQN] | |||
* J: [https://code.jsoftware.com/wiki/Vocabulary/hook Hook], [https://code.jsoftware.com/wiki/Vocabulary/fork Fork] | |||
=== Tutorials === | |||
==== Text ==== | |||
* [[APL Cultivation]]: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-23-transcribing-to-and-reading-trains Transcribing to and reading trains] | |||
* gitonthescene: [https://gist.github.com/gitonthescene/666c77ee3ed0ae0a79cf8e057584b7fd Forks: Spoon fed] | |||
* gitonthescene: [https://gist.github.com/gitonthescene/5e9c25ab9edd2f2ce0d5ad38d8a8b2b4 Training day] | |||
==== Videos ==== | |||
* [[APLtrainer]]: [https://www.youtube.com/watch?v=kt4lMZbn-so How to read trains in Dyalog APL code] | |||
* [[APLtrainer]]: [https://www.youtube.com/watch?v=A2LqqBosvY0 Function trains in APL] | |||
* [[Dyalog webinar]]: [https://www.youtube.com/watch?v=Enlh5qwwDuY?t=440 Train Spotting in Dyalog APL] | |||
* [[Dyalog '13]]: [https://www.youtube.com/watch?v=7-93GzDqC08 Train Spotting in Version 14.0] | |||
</div> | |||
== References == | == References == | ||
<references /> | <references /> | ||
{{APL syntax}}[[Category:Tacit programming]][[Category:Kinds of functions]][[Category:Defining functions]] | |||
{{APL syntax}}[[Category:Kinds of functions]][[Category:Defining functions]] |
Revision as of 15:36, 8 August 2022
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.
Definition
Below, ⍺
and ⍵
refer to the arguments of the train. f
, g
, and h
are functions (which themselves can be tacit or not), and A
is an array. The arguments are processed by the following rules:
3-trains
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 ⍵) |
2-trains
Most dialects define a 2-train is an atop, equivalent to the function derived using the Atop operator. The left function is applied monadically on the result of the right function:
(g h) ⍵ |
g ( h ⍵) | |
⍺ (g h) ⍵ |
g (⍺ h ⍵) |
Only dzaima/APL allows (A h)
, which it treats as A∘h
.[1] See Bind.
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; f⍤/
.
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.
External links
Documentation
Tutorials
Text
- APL Cultivation: Transcribing to and reading trains
- gitonthescene: Forks: Spoon fed
- gitonthescene: Training day
Videos
- 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
References
- ↑ dzaima/APL: Differences from Dyalog APL. Retrieved 09 Jan 2020.