Tacit programming: Difference between revisions

Jump to navigation Jump to search
1,508 bytes added ,  17:10, 7 April 2020
(15 intermediate revisions by 2 users not shown)
Line 11: Line 11:


== Derived functions ==
== Derived functions ==
Functions derived from an operator and operand are tacit.
Functions derived from a monadic operator and an operand, or from a dyadic operator and two operands are tacit functions:
<source lang=apl>
<source lang=apl>
       sum ← +/
       Sum ← +/
       sum ⍳10
       Sum ⍳10
55
55
      Dot ← +.×
      3 1 4 dot 2 7 1
17
</source>
== Derived operators ==
A dyadic operator with its right operand forms a tacit monadic operator:
<source lang=apl>
      1(+⍣2)10
12
      Twice ← ⍣2
      1 +Twice 10
12
</source>
</source>


== Trains ==
== Trains ==
A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named. 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:


A 2-train is an ''atop'':
A 2-train is an ''atop'':
<source lang=apl>
{|
  (g h) ⍵ g (  h ⍵)
|<source lang=apl> (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g (  h ⍵)</source>
⍺ (g h) ⍵ g (⍺ h ⍵)
|-
</source>
|<source lang=apl>⍺ (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g (⍺ h ⍵)</source>
 
|}
A 3-train is a ''fork'':
A 3-train is a ''fork'':
<source lang=apl>
{|
  (f g h) ⍵ (  f ⍵) g (  h ⍵)
|<source lang=apl> (f g h) ⍵</source>|| {{←→}} ||<source lang=apl>(  f ⍵) g (  h ⍵)</source>
⍺ (f g h) ⍵ (⍺ f ⍵) g (⍺ h ⍵)
|-
</source>
|<source lang=apl>⍺ (f g h) ⍵</source>|| {{←→}} ||<source lang=apl>(⍺ f ⍵) g (⍺ h ⍵)</source>
|}
The ''left tine'' of a fork can be an array:
{|
|<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>
|}


The ''left tine'' of a fork (but not an atop) can be an array:
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>
<source lang=apl>
  (A g h) ⍵ ⬄ A g (  h ⍵)
⍺ (A g h) ⍵ ⬄ A g (⍺ h ⍵)
</source>


== Examples ==
== Examples ==
Line 44: Line 60:
=== Plus and minus ===
=== Plus and minus ===
<source lang=apl>
<source lang=apl>
       (+,-)2
       (+,-) 2     ⍝ ±2
2 ¯2
2 ¯2
       1 2 3 (+,-) 4
       5 (+,-) 2  ⍝ 5±2
5 6 7 ¯3 ¯2 ¯1
7 3
      (2 3⍴0) (+,-) 1
1 1 1 ¯1 ¯1 ¯1
1 1 1 ¯1 ¯1 ¯1     
</source>
</source>


Line 95: Line 108:


=== Component of a vector in the direction of another vector ===
=== Component of a vector in the direction of another vector ===
Sometimes a train can make an expression nicely resemble its equivalent definition in [[Comparison_with_traditional_mathematics|traditional mathematical notation]]. As an example, here is a program to compute the component of a vector '''a''' in the direction of another vector '''b'''.
Sometimes a train can make an expression nicely resemble its equivalent definition in traditional mathematical notation. As an example, here is a program to compute the component of a vector <math>\textbf{a}</math> in the direction of another vector <math>\textbf{b}</math>:
 
:::<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
<div style="text-align:center">
<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
</div>
 
<source lang=apl>
<source lang=apl>
       Sqrt ← *∘0.5            Square root  
       Root ← *∘÷⍨              Nth root
       Norm ← Sqrt+.×⍨         ⍝ Magnitude (norm) of numeric vector in Euclidean space
       Norm ← 2 Root +.×⍨       ⍝ Magnitude (norm) of numeric vector in Euclidean space
       Unit ← ÷∘Norm⍨          ⍝ Unit vector in direction of vector ⍵
       Unit ← ⊢÷Norm            ⍝ Unit vector in direction of vector ⍵
       InDirOf ← (⊢×+.×)∘Unit  ⍝ Component of vector ⍺ in direction of vector ⍵
       InDirOf ← (⊢×+.×)∘Unit  ⍝ Component of vector ⍺ in direction of vector ⍵
       3 5 2 InDirOf 0 0 1      ⍝ Trivial example
       3 5 2 InDirOf 0 0 1      ⍝ Trivial example
0 0 2
0 0 2
</source>
</source>
For a more parallel comparison of the notations, see the [[Comparison_with_traditional_mathematics#Practical_example|comparison with traditional mathematics]].
== External links ==
== Tutorials ==
* Dyalog: [http://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]
* 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=A2LqqBosvY0 Function trains in APL] (video)
* Dyalog Webinar: [https://www.youtube.com/watch?v=Enlh5qwwDuY?t=440 Train Spotting in Dyalog APL] (video)
* Dyalog '13: [https://www.youtube.com/watch?v=7-93GzDqC08 Train Spotting in Version 14.0] (video)


In particular, the definition of <source inline lang=apl>InDirOf</source> resembles the definition in traditional mathematical notation:
== Documentation ==
<div style="text-align:center">
* [http://help.dyalog.com/16.0/Content/RelNotes14.0/Function%20Trains.htm Announcement]
{| class="wikitable c" style="margin: 1em auto 1em auto"
* [http://help.dyalog.com/latest/Content/Language/Introduction/Trains.htm Dyalog]
! style="width:33%" | Traditional notation !! style="width:33%" | APL
|-
| <math>|\textbf{b}|</math> || <source lang=apl>(Sqrt+.×⍨) b</source>
|-
| <math>\hat{\textbf{b}} = \frac{\textbf{b}}{|\textbf{b}|}</math>  ||  <source lang=apl>(÷∘Norm⍨) b</source>
|-
| <math>\textbf{a}\cdot\textbf{b}</math>  ||  <source lang=apl>a +.× b</source>
|-
| <math>(\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>  ||  <source lang=apl>a (⊢×+.×)∘Unit b</source>
|}
</div>


==References==
<references/>
{{APL syntax}}
{{APL syntax}}

Navigation menu