2,962
edits
(→Trains) |
mNo edit summary |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Tacit functions apply to implicit arguments | Tacit functions apply to implicit arguments. This is in contrast to the explicit use of arguments in [[dfn]]s (<source inline lang=apl>⍺ ⍵</source>) and [[tradfn]]s (which have named arguments). Some APL dialects allow to combine functions into '''trains''' following a small set of rules. This allows creating complex derived functions without specifying any arguments explicitly. | ||
Known dialects which implement trains are [[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]] and [[NARS2000]]. | |||
== Primitives == | == Primitives == | ||
Line 34: | Line 36: | ||
A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named. Below, <source lang=apl inline>⍺</source> and <source lang=apl inline>⍵</source> refer to the arguments of the train. <source lang=apl inline>f</source>, <source lang=apl inline>g</source>, and <source lang=apl inline>h</source> are functions (which themselves can be tacit or not), and <source lang=apl inline>A</source> is an array. The arguments are processed by the following rules: | A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named. Below, <source lang=apl inline>⍺</source> and <source lang=apl inline>⍵</source> refer to the arguments of the train. <source lang=apl inline>f</source>, <source lang=apl inline>g</source>, and <source lang=apl inline>h</source> are functions (which themselves can be tacit or not), and <source lang=apl inline>A</source> is an array. The arguments are processed by the following rules: | ||
=== Forks === | |||
A 3-train is a ''fork'': | A 3-train is a ''fork'': | ||
{| | {| | ||
Line 51: | Line 48: | ||
|- | |- | ||
|<source lang=apl>⍺ (A g h) ⍵</source>|| {{←→}} ||<source lang=apl>A g (⍺ h ⍵)</source> | |<source lang=apl>⍺ (A g h) ⍵</source>|| {{←→}} ||<source lang=apl>A g (⍺ h ⍵)</source> | ||
|} | |||
=== Atops === | |||
A 2-train is an ''atop'': | |||
{| | |||
|<source lang=apl> (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g ( h ⍵)</source> | |||
|- | |||
|<source lang=apl>⍺ (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g (⍺ h ⍵)</source> | |||
|} | |} | ||
Only [[dzaima/APL]] allows <source lang=apl inline>(A h)</source>, which it treats as <source lang=apl inline>A∘h</source>.<ref>dzaima/APL: [https://github.com/dzaima/APL/blob/ceea05e25687988ed0980a4abf4b9249b736543f/docs/differences.txt#L19 Differences from Dyalog APL]. Retrieved 09 Jan 2020.</ref> See [[Bind]]. | Only [[dzaima/APL]] allows <source lang=apl inline>(A h)</source>, which it treats as <source lang=apl inline>A∘h</source>.<ref>dzaima/APL: [https://github.com/dzaima/APL/blob/ceea05e25687988ed0980a4abf4b9249b736543f/docs/differences.txt#L19 Differences from Dyalog APL]. Retrieved 09 Jan 2020.</ref> See [[Bind]]. | ||
== Debugging == | |||
In [[Dyalog APL]], analysis of trains is assisted by a [[user command]] <source lang=apl inline>]Boxing on</source>. This is achieved by executing the command <source lang=apl inline>]Boxing on</source> and then entering a train without any parameters. A structure of the train will be displayed. | |||
For example, the "accursed train" from the section below can be analysed like this: | |||
<source lang=apl> | |||
]Boxing on | |||
Was OFF | |||
((+.×⍨⊢~∘.×⍨)1↓⍳) ⍝ the train to be analysed | |||
┌───────────────────────────────┬───────┐ | |||
│┌───────────┬─────────────────┐│┌─┬─┬─┐│ | |||
││┌───────┬─┐│┌─┬─┬───────────┐│││1│↓│⍳││ | |||
│││┌─┬─┬─┐│⍨│││⊢│~│┌───────┬─┐│││└─┴─┴─┘│ | |||
││││+│.│×││ │││ │ ││┌─┬─┬─┐│⍨││││ │ | |||
│││└─┴─┴─┘│ │││ │ │││∘│.│×││ ││││ │ | |||
││└───────┴─┘││ │ ││└─┴─┴─┘│ ││││ │ | |||
││ ││ │ │└───────┴─┘│││ │ | |||
││ │└─┴─┴───────────┘││ │ | |||
│└───────────┴─────────────────┘│ │ | |||
└───────────────────────────────┴───────┘ | |||
</source> | |||
Alternatively, a train can be represented in form of a tree: | |||
<source lang=apl> | |||
]Boxing on -trains=tree | |||
Was ON -trains=box | |||
((+.×⍨⊢~∘.×⍨)1↓⍳) ⍝ the train to be analysed | |||
┌───┴───┐ | |||
┌─┴─┐ ┌─┼─┐ | |||
⍨ ┌─┼─┐ 1 ↓ ⍳ | |||
┌─┘ ⊢ ~ ⍨ | |||
. ┌─┘ | |||
┌┴┐ . | |||
+ × ┌┴┐ | |||
∘ × | |||
</source> | |||
Or fully parenthesised: | |||
<source lang=apl> | |||
]Boxing on -trains=parens | |||
Was OFF -trains=box | |||
((+.×⍨⊢~∘.×⍨)1↓⍳) ⍝ the train to be analysed | |||
(((+.×)⍨)(⊢~((∘.×)⍨)))(1↓⍳) | |||
</source> | |||
== Examples == | == Examples == | ||
Line 150: | Line 198: | ||
666 ⍝ ⍎ executes original Accursed train | 666 ⍝ ⍎ executes original Accursed train | ||
</source> | </source> | ||
== See also == | |||
* [[Function composition]] | |||
== External links == | == External links == | ||
=== Tutorials === | === Tutorials === | ||
* Dyalog: [ | * Dyalog: [https://help.dyalog.com/16.0/Content/RelNotes14.0/Function%20Trains.htm version 14.0 release notes] | ||
* [[APL Cultivation]]: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-23-transcribing-to-and-reading-trains Transcribing to and reading trains] | * [[APL Cultivation]]: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-23-transcribing-to-and-reading-trains Transcribing to and reading trains] | ||
* [[APLtrainer]]: [https://www.youtube.com/watch?v=kt4lMZbn-so How to read trains in Dyalog APL code] (video) | * [[APLtrainer]]: [https://www.youtube.com/watch?v=kt4lMZbn-so How to read trains in Dyalog APL code] (video) | ||
Line 161: | Line 213: | ||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/16.0/Content/RelNotes14.0/Function%20Trains.htm Announcement] | ||
* [ | * [https://help.dyalog.com/latest/Content/Language/Introduction/Trains.htm Dyalog] | ||
== References == | == References == |