Tacit programming: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
m (Fix code)
 
(69 intermediate revisions by 11 users not shown)
Line 1: Line 1:
Tacit functions apply to implicit arguments following a small set of rules. This is in contrast to the explicit use of arguments in [[dfns]] (<source inline lang=apl>⍺ ⍵</source>) and [[tradfns]] (which have named arguments). Known dialects which implement trains are [[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]] and [[NARS2000]].
[[File:Function compositions.png|thumb|right|Diagrams of [[function composition]]s, an important part of tacit programming.]]
'''Tacit programming''', also called '''[[wikipedia:Tacit_programming|point-free style]]''', refers to usage of tacit [[function]]s that are defined in terms of implicit [[argument]]s. This is in contrast to the explicit use of arguments in [[dfn]]s (<syntaxhighlight inline lang=apl>⍺ ⍵</syntaxhighlight>) and [[tradfn]]s (which have named arguments). Some APL dialects allow to combine functions into [[train]]s following a small set of rules. This allows creating complex [[derived function]]s without specifying any arguments explicitly.
 
Dialects which implement trains include [[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]] and [[NARS2000]].


== Primitives ==
== Primitives ==
All [[primitive functions]] are tacit. Some APLs allow primitive functions to be named.
All [[primitive functions]] are tacit. Some APLs allow primitive functions to be named.
<source lang=apl>
<syntaxhighlight lang=apl>
       plus ← +
       plus ← +
       times ← ×
       times ← ×
       6 times 3 plus 5
       6 times 3 plus 5
48
48
</source>
</syntaxhighlight>


== 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>
<syntaxhighlight lang=apl>
       sum ← +/
       Sum ← +/
       sum ⍳10
       Sum ⍳10
55
55
</source>
 
      Dot ← +.×
      3 1 4 Dot 2 7 1
17
</syntaxhighlight>
 
== Derived operators ==
A dyadic operator with its right operand forms a tacit monadic operator:
<syntaxhighlight lang=apl>
      1(+⍣2)10
12
      Twice ← ⍣2
      1 +Twice 10
12
</syntaxhighlight>


== 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.
 
These rules are used for 3-trains, called [[fork]]s:
{|
|<syntaxhighlight lang=apl>  (f g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>(  f ⍵) g (  h ⍵)</syntaxhighlight>
|-
|<syntaxhighlight lang=apl>⍺ (f g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>(⍺ f ⍵) g (⍺ h ⍵)</syntaxhighlight>
|}
The ''left tine'' of a fork can be an array:
{|
|<syntaxhighlight lang=apl>  (A g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>A g (  h ⍵)</syntaxhighlight>
|-
|<syntaxhighlight lang=apl>⍺ (A g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>A g (⍺ h ⍵)</syntaxhighlight>
|}
 
In APL (but not [[J]]), these rules are used for 2-trains, called [[atop]]s:
{|
|<syntaxhighlight lang=apl>  (g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>g (  h ⍵)</syntaxhighlight>
|-
|<syntaxhighlight lang=apl>⍺ (g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>g (⍺ h ⍵)</syntaxhighlight>
|}
 
Any train can be expressed in terms of [[function composition]] &mdash; except dyadic forks. Some common patterns are:
 
{|
|<syntaxhighlight lang=apl>(f g h) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>g⍨∘f⍨∘h⍨ ⍵</syntaxhighlight>
|-
|<syntaxhighlight lang=apl>(f g f) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>g⍥f⍨ ⍵</syntaxhighlight>
|-
|<syntaxhighlight lang=apl>(⊢ g f) ⍵</syntaxhighlight>|| {{←→}} ||<syntaxhighlight lang=apl>g∘f⍨ ⍵</syntaxhighlight>
|}


A 2-train is an ''atop'':
== Debugging ==
<source lang=apl>
In [[Dyalog APL]], analysis of trains is assisted by a [[user command]] <syntaxhighlight lang=apl inline>]Boxing on</syntaxhighlight>. This is achieved by executing the command <syntaxhighlight lang=apl inline>]Boxing on</syntaxhighlight> and then entering a train without any parameters. A structure of the train will be displayed.
  (g h) ⍵ ⬄ g (  h ⍵)
⍺ (g h) ⍵ ⬄ g (⍺ h ⍵)
</source>


A 3-train is a ''fork'':
For example, the "accursed train" from the section below can be analysed like this:
<source lang=apl>
<syntaxhighlight lang=apl>
  (f g h) ⍵ ⬄ ( f ⍵) g (  h ⍵)
      ]Boxing on
⍺ (f g h) ⍵ ⬄ (⍺ f ⍵) g (⍺ h ⍵)
Was OFF
</source>
      ((+.×⍨⊢~∘.×⍨)1↓⍳)     ⍝ the train to be analysed
┌───────────────────────────────┬───────┐
│┌───────────┬─────────────────┐│┌─┬─┬─┐│
││┌───────┬─┐│┌─┬─┬───────────┐│││1│↓│⍳││
│││┌─┬─┬─┐│⍨│││⊢│~│┌───────┬─┐│││└─┴─┴─┘│
││││+│.│×││ │││ │ ││┌─┬─┬─┐│⍨││││      │
│││└─┴─┴─┘│ │││ │ │││∘│.│×││ ││││      │
││└───────┴─┘││ │ ││└─┴─┴─┘│ ││││      │
││          ││ │ │└───────┴─┘│││      │
││          │└─┴─┴───────────┘││      │
│└───────────┴─────────────────┘│      │
└───────────────────────────────┴───────┘
</syntaxhighlight>


The ''left tine'' of a fork (but not an atop) can be an array:
Alternatively, a train can be represented in form of a tree:
<source lang=apl>
<syntaxhighlight lang=apl>
  (A g h) ⍵ ⬄ A g ( h ⍵)
      ]Boxing on -trains=tree
(A g h) ⍵ ⬄ A g (⍺ h ⍵)
Was ON -trains=box
</source>
      ((+.×⍨⊢~∘.×⍨)1↓⍳)     ⍝ the train to be analysed
    ┌───┴───┐ 
  ┌─┴─┐  ┌─┼─┐
  ⍨ ┌─┼─┐ 1 ↓ ⍳
┌─┘ ⊢ ~ ⍨     
.    ┌─┘     
┌┴┐    .       
+ ×  ┌┴┐     
      ∘ ×     
</syntaxhighlight>
Or fully parenthesised:
<syntaxhighlight lang=apl>
      ]Boxing on -trains=parens
Was OFF -trains=box
      ((+.×⍨⊢~∘.×⍨)1↓⍳)    ⍝ the train to be analysed
(((+.×)⍨)(⊢~((∘.×)⍨)))(1↓⍳)
</syntaxhighlight>


=== Conversion to dfns ===
It can help understanding to convert a tacit function to a dfn. For many tacit functions, it is not immediately clear if the intention of the function is to be used monadically or dyadically, or even both. Such knowledge can be conveyed by comments, but sometimes it is possible to spot patterns that are exclusively monadic or dyadic: A function with a bound argument (for example <syntaxhighlight lang=apl inline>+∘1</syntaxhighlight>) can indicate a monadic function, and in some contexts, <syntaxhighlight lang=apl inline>=</syntaxhighlight>, which can only be used dyadically, would indicate a dyadic function. The website [https://tacit.help tacit.help] provides automated translation of most tacit functions, into both monadic and dyadic, fully parenthesised dfns.
== Examples ==
== Examples ==
One of the major benefits of tacit programming is the ability to convey a short, well-defined idea as an isolated expression. This aids both human readability ([[semantic density]]) and the computer's ability to interpret code, potentially executing special code for particular [[idioms]].
One of the major benefits of tacit programming is the ability to convey a short, well-defined idea as an isolated expression. This aids both human readability ([[semantic density]]) and the computer's ability to interpret code, potentially executing special code for particular [[idiom]]s.


=== Plus and minus ===
=== Plus and minus ===
<source lang=apl>
<syntaxhighlight 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
</syntaxhighlight>
1 1 1 ¯1 ¯1 ¯1
1 1 1 ¯1 ¯1 ¯1     
</source>


=== Arithmetic mean ===
=== Arithmetic mean ===
<source lang=apl>
<syntaxhighlight lang=apl>
       (+⌿÷≢) ⍳10      ⍝ Mean of the first ten integers
       (+⌿÷≢) ⍳10      ⍝ Mean of the first ten integers
5.5
5.5
       (+⌿÷≢) 5 4⍴⍳4    ⍝ Mean of columns in a matrix
       (+⌿÷≢) 5 4⍴⍳4    ⍝ Mean of columns in a matrix
1 2 3 4
1 2 3 4
</source>
</syntaxhighlight>


=== Top-heavy fraction as decimal ===
=== Fractions ===
<source lang=apl>
We can convert decimal numbers to fractions. For example, we can convert <math>2.625</math> to the improper fraction <math>\tfrac{21}{8}</math> with
       (1∧⊢,÷) 1.125
<syntaxhighlight lang=apl>
9 8
       (1∧⊢,÷)2.625
</source>
21 8
</syntaxhighlight>
Alternatively, we can convert it to the mixed fraction <math>2\tfrac{5}{8}</math> with a mixed fraction:
<syntaxhighlight lang=apl>
      (1∧0 1∘⊤,÷)2.625
2 5 8
</syntaxhighlight>


=== Is it a palindrome? ===
=== Is it a palindrome? ===
<source lang=apl>
<syntaxhighlight lang=apl>
       (⌽≡⊢)'racecar'
       (⌽≡⊢)'racecar'
1
1
       (⌽≡⊢)'racecat'
       (⌽≡⊢)'racecat'
0
0
</source>
</syntaxhighlight>


=== Split delimited text ===
=== Split delimited text ===
<source lang=apl>
<syntaxhighlight lang=apl>
       ','(≠⊆⊢)'comma,delimited,text'
       ','(≠⊆⊢)'comma,delimited,text'
┌─────┬─────────┬────┐
┌─────┬─────────┬────┐
Line 85: Line 162:
│space│delimited│text│
│space│delimited│text│
└─────┴─────────┴────┘
└─────┴─────────┴────┘
</source>
</syntaxhighlight>


=== 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">
<syntaxhighlight lang=apl>
<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
       Root ← *∘÷⍨             ⍝ Nth root
</div>
       Norm ← 2 Root +.×⍨       ⍝ Magnitude (norm) of numeric vector in Euclidean space
 
       Unit ← ⊢÷Norm            ⍝ Unit vector in direction of vector ⍵
<source lang=apl>
       Sqrt ← *∘.5             ⍝ Square root  
       Norm ← Sqrt+.×⍨         ⍝ Magnitude (norm) of numeric vector in Euclidean space
       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>
</syntaxhighlight>
For a more parallel comparison of the notations, see the [[Comparison_with_traditional_mathematics#Practical_example|comparison with traditional mathematics]].
 
===The Number of the Beast===
The following expression for computing the [[wikipedia:666 (number)|number of the Beast]] (and of [[I.P. Sharp]]'s APL-based email system, [[666 BOX]]) nicely illustrates how to read a train.
<syntaxhighlight lang=apl>
      ((+.×⍨⊢~∘.×⍨)1↓⍳)17 ⍝ Accursed train
666
</syntaxhighlight>
First, <syntaxhighlight lang=apl inline>((+.×⍨⊢~∘.×)1↓⍳)</syntaxhighlight> is supplied with only one argument <syntaxhighlight lang=apl inline>17</syntaxhighlight> and is thus interpreted monadically.
 
Second, <syntaxhighlight lang=apl inline>(+.×⍨⊢~∘.×⍨)1↓⍳</syntaxhighlight> is a 4-train: reading right-to-left, the last 3 components are interpreted as the fork <syntaxhighlight lang=apl inline>1↓⍳</syntaxhighlight> and the 4-train is interpreted as the atop <syntaxhighlight lang=apl inline>(+.×⍨⊢~∘.×⍨)(1↓⍳)</syntaxhighlight>.
Similarly, <syntaxhighlight lang=apl inline>(+.×⍨⊢~∘.×⍨)</syntaxhighlight> is also a 4-train and interpreted as the atop <syntaxhighlight lang=apl inline>+.×⍨(⊢~∘.×⍨)</syntaxhighlight>.
 
Thus the accursed train is interpreted as <syntaxhighlight lang=apl inline>((+.×⍨(⊢~∘.×⍨))(1↓⍳))17</syntaxhighlight>. Having read the train, we now evaluate it monadically.
<syntaxhighlight lang=apl>
      ((+.×⍨(⊢~∘.×⍨))(1↓⍳))17 ⍝ Accursed train as an atop over a fork atop a fork
      +.×⍨(⊢~∘.×⍨)1↓⍳17      ⍝ Atop evalution
      +.×⍨(⊢1↓⍳17)~∘.×⍨1↓⍳17  ⍝ Fork evalution
      +.×⍨(1↓⍳17)~∘.×⍨1↓⍳17  ⍝ ⊢ evaluation
      +.×⍨2 3 5 7 11 13 17    ⍝ numbers 2 through 17 without those appearing in their multiplication table are primes
666                          ⍝ the sum of the squares of the primes up to 17
</syntaxhighlight>
Note that <syntaxhighlight lang=apl inline>((⊢~∘.×⍨)1↓⍳)</syntaxhighlight> is a train computing primes up to the given input.
 
A more satisfying variation of the accursed train is the following.
<syntaxhighlight lang=apl>
      (⍎⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ Accursed train 2.0
      ⍎(⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ 4-train intepreted as an atop
      ⍎(⊢'((+.×⍨⊢~∘.×⍨)1↓⍳)'),⍕∘≢'((+.×⍨⊢~∘.×⍨)1↓⍳)' ⍝ fork evaluation
      ⍎'((+.×⍨⊢~∘.×⍨)1↓⍳)','17'                      ⍝ ⊢ evaluation and ⍕∘≢ evaluation
      ⍎'((+.×⍨⊢~∘.×⍨)1↓⍳)17'                        ⍝ , evaluation
666                                                  ⍝ ⍎ executes original Accursed train
</syntaxhighlight>


In particular, the definition of <source inline lang=apl>InDirOf</source> resembles the definition in traditional mathematical notation:
== External links ==
<div style="text-align:center">
=== Tutorials ===
{| class="wikitable c" style="margin: 1em auto 1em auto"
<div style="column-width:30em">
! style="width:33%" | Traditional notation !! style="width:33%" | APL
==== In text form ====
|-
* [[Learning APL]]: [https://xpqz.github.io/learnapl/tacit.html Trainspotting]
| <math>|\textbf{b}|</math> || <source lang=apl>(Sqrt+.×⍨) b</source>
* [[Documentation_suites#Dyalog_APL|Dyalog documentation]]: [https://help.dyalog.com/16.0/Content/RelNotes14.0/Function%20Trains.htm version 14.0 release notes]
|-
* [[Dfns workspace]]: [https://dfns.dyalog.com/n_tacit.htm Translation of <nowiki>[dfns]</nowiki> into tacit form]
| <math>\hat{\textbf{b}} = \frac{\textbf{b}}{|\textbf{b}|}</math>  ||  <source lang=apl>(÷∘Norm⍨) b</source>
* [[Dfns workspace]]: [https://dfns.dyalog.com/n_dft.htm Display of function tree]
|-
* [[Dfns workspace]]: [https://dfns.dyalog.com/n_fork.htm Simulation of fork syntax]
| <math>\textbf{a}\cdot\textbf{b}</math>  ||  <source lang=apl>a +.× b</source>
* [[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]
| <math>(\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>  ||  <source lang=apl>a (⊢×+.×)∘Unit b</source>
* 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>
</div>


{{APL features}}
== References ==
{{APL syntax}}
<references/>
 
{{APL syntax}}[[Category:Tacit programming| ]]

Latest revision as of 21:28, 6 March 2024

Diagrams of function compositions, an important part of tacit programming.

Tacit programming, also called point-free style, refers to usage of tacit functions that are defined in terms of implicit arguments. This is in contrast to the explicit use of arguments in dfns (⍺ ⍵) and tradfns (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.

Dialects which implement trains include Dyalog APL, dzaima/APL, ngn/apl and NARS2000.

Primitives

All primitive functions are tacit. Some APLs allow primitive functions to be named.

      plus ← +
      times ← ×
      6 times 3 plus 5
48

Derived functions

Functions derived from a monadic operator and an operand, or from a dyadic operator and two operands are tacit functions:

      Sum ← +/
      Sum ⍳10
55

      Dot ← +.×
      3 1 4 Dot 2 7 1
17

Derived operators

A dyadic operator with its right operand forms a tacit monadic operator:

      1(+⍣2)10
12
      Twice ← ⍣2
      1 +Twice 10
12

Trains

A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named.

These rules are used for 3-trains, called forks:

  (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 ⍵)

In APL (but not J), these rules are used for 2-trains, called atops:

  (g h) ⍵
g (  h ⍵)
⍺ (g h) ⍵
g (⍺ h ⍵)

Any train can be expressed in terms of function composition — except dyadic forks. Some common patterns are:

(f g h) ⍵
g⍨∘f⍨∘h⍨ ⍵
(f g f) ⍵
g⍥f⍨ ⍵
(⊢ g f) ⍵
g∘f⍨ ⍵

Debugging

In Dyalog APL, analysis of trains is assisted by a user command ]Boxing on. This is achieved by executing the command ]Boxing on 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:

      ]Boxing on
Was OFF
      ((+.×⍨⊢~∘.×⍨)1↓⍳)     ⍝ the train to be analysed
┌───────────────────────────────┬───────┐
│┌───────────┬─────────────────┐│┌─┬─┬─┐│
││┌───────┬─┐│┌─┬─┬───────────┐│││1│↓│⍳││
│││┌─┬─┬─┐│⍨│││⊢│~│┌───────┬─┐│││└─┴─┴─┘│
││││+│.│×││ │││ │ ││┌─┬─┬─┐│⍨││││       │
│││└─┴─┴─┘│ │││ │ │││∘│.│×││ ││││       │
││└───────┴─┘││ │ ││└─┴─┴─┘│ ││││       │
││           ││ │ │└───────┴─┘│││       │
││           │└─┴─┴───────────┘││       │
│└───────────┴─────────────────┘│       │
└───────────────────────────────┴───────┘

Alternatively, a train can be represented in form of a tree:

      ]Boxing on -trains=tree
Was ON -trains=box
      ((+.×⍨⊢~∘.×⍨)1↓⍳)     ⍝ the train to be analysed
     ┌───┴───┐  
   ┌─┴─┐   ┌─┼─┐
   ⍨ ┌─┼─┐ 1 ↓ ⍳
 ┌─┘ ⊢ ~ ⍨      
 .     ┌─┘      
┌┴┐    .        
+ ×   ┌┴┐       
      ∘ ×

Or fully parenthesised:

      ]Boxing on -trains=parens
Was OFF -trains=box
      ((+.×⍨⊢~∘.×⍨)1↓⍳)     ⍝ the train to be analysed
(((+.×)⍨)(⊢~((∘.×)⍨)))(1↓⍳)

Conversion to dfns

It can help understanding to convert a tacit function to a dfn. For many tacit functions, it is not immediately clear if the intention of the function is to be used monadically or dyadically, or even both. Such knowledge can be conveyed by comments, but sometimes it is possible to spot patterns that are exclusively monadic or dyadic: A function with a bound argument (for example +∘1) can indicate a monadic function, and in some contexts, =, which can only be used dyadically, would indicate a dyadic function. The website tacit.help provides automated translation of most tacit functions, into both monadic and dyadic, fully parenthesised dfns.

Examples

One of the major benefits of tacit programming is the ability to convey a short, well-defined idea as an isolated expression. This aids both human readability (semantic density) and the computer's ability to interpret code, potentially executing special code for particular idioms.

Plus and minus

      (+,-) 2     ⍝ ±2
2 ¯2
      5 (+,-) 2   ⍝ 5±2
7 3

Arithmetic mean

      (+⌿÷≢) ⍳10       ⍝ Mean of the first ten integers
5.5
      (+⌿÷≢) 5 4⍴⍳4    ⍝ Mean of columns in a matrix
1 2 3 4

Fractions

We can convert decimal numbers to fractions. For example, we can convert to the improper fraction with

      (1∧⊢,÷)2.625
21 8

Alternatively, we can convert it to the mixed fraction with a mixed fraction:

      (1∧0 1∘⊤,÷)2.625
2 5 8

Is it a palindrome?

      (⌽≡⊢)'racecar'
1
      (⌽≡⊢)'racecat'
0

Split delimited text

      ','(≠⊆⊢)'comma,delimited,text'
┌─────┬─────────┬────┐
│comma│delimited│text│
└─────┴─────────┴────┘
      ' '(≠⊆⊢)'space delimited text'
┌─────┬─────────┬────┐
│space│delimited│text│
└─────┴─────────┴────┘

Component of a vector in the direction of another vector

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 in the direction of another vector :

      Root ← *∘÷⍨              ⍝ Nth root
      Norm ← 2 Root +.×⍨       ⍝ Magnitude (norm) of numeric vector in Euclidean space
      Unit ← ⊢÷Norm            ⍝ Unit vector in direction of vector ⍵
      InDirOf ← (⊢×+.×)∘Unit   ⍝ Component of vector ⍺ in direction of vector ⍵
      3 5 2 InDirOf 0 0 1      ⍝ Trivial example
0 0 2

For a more parallel comparison of the notations, see the comparison with traditional mathematics.

The Number of the Beast

The following expression for computing the number of the Beast (and of I.P. Sharp's APL-based email system, 666 BOX) nicely illustrates how to read a train.

      ((+.×⍨⊢~∘.×⍨)1↓⍳)17 ⍝ Accursed train
666

First, ((+.×⍨⊢~∘.×)1↓⍳) is supplied with only one argument 17 and is thus interpreted monadically.

Second, (+.×⍨⊢~∘.×⍨)1↓⍳ is a 4-train: reading right-to-left, the last 3 components are interpreted as the fork 1↓⍳ and the 4-train is interpreted as the atop (+.×⍨⊢~∘.×⍨)(1↓⍳). Similarly, (+.×⍨⊢~∘.×⍨) is also a 4-train and interpreted as the atop +.×⍨(⊢~∘.×⍨).

Thus the accursed train is interpreted as ((+.×⍨(⊢~∘.×⍨))(1↓⍳))17. Having read the train, we now evaluate it monadically.

      ((+.×⍨(⊢~∘.×⍨))(1↓⍳))17 ⍝ Accursed train as an atop over a fork atop a fork
      +.×⍨(⊢~∘.×⍨)1↓⍳17       ⍝ Atop evalution
      +.×⍨(⊢1↓⍳17)~∘.×⍨1↓⍳17  ⍝ Fork evalution
      +.×⍨(1↓⍳17)~∘.×⍨1↓⍳17   ⍝ ⊢ evaluation
      +.×⍨2 3 5 7 11 13 17    ⍝ numbers 2 through 17 without those appearing in their multiplication table are primes
666                           ⍝ the sum of the squares of the primes up to 17

Note that ((⊢~∘.×⍨)1↓⍳) is a train computing primes up to the given input.

A more satisfying variation of the accursed train is the following.

      (⍎⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ Accursed train 2.0
      ⍎(⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ 4-train intepreted as an atop
      ⍎(⊢'((+.×⍨⊢~∘.×⍨)1↓⍳)'),⍕∘≢'((+.×⍨⊢~∘.×⍨)1↓⍳)' ⍝ fork evaluation
      ⍎'((+.×⍨⊢~∘.×⍨)1↓⍳)','17'                      ⍝ ⊢ evaluation and ⍕∘≢ evaluation
      ⍎'((+.×⍨⊢~∘.×⍨)1↓⍳)17'                         ⍝ , evaluation
666                                                  ⍝ ⍎ executes original Accursed train

External links

Tutorials

In text form


Videos

References


APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment