Simple examples: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
Miraheze>Adám Brudzewsky
No edit summary
(34 intermediate revisions by 6 users not shown)
Line 1: Line 1:
This page will contain examples that serve well to show APL's strength. They require minimal background and have no special dependencies.
This page contains examples that show APL's strengths. The examples require minimal background and have no special dependencies. If these examples are too simple for you, have a look at our [[advanced examples]].
 
== Arithmetic mean ==
 
Here is an APL program to calculate the average (arithmetic mean) of a list of numbers, written as a [[dfn]]:
<source lang=apl>
      {(+⌿ω)÷≢ω}
</source>
It is unnamed: the enclosing braces mark it as a function definition. It can be assigned a name for use later, or used anonymously in a more complex expression.
 
The <source lang=apl inline>ω</source> refers to the argument of the function, a list (or 1-dimensional array) of numbers. The <source lang=apl inline>≢</source> denotes the [[tally]] function, which returns here the length of (number of elements in) the argument <source lang=apl inline>ω</source>. The divide symbol <source lang=apl inline>÷</source> has its usual meaning.
 
The parenthesised <source lang=apl inline>+⌿ω</source> denotes the sum of all the elements of <source lang=apl inline>ω</source>. The <source lang=apl inline>⌿</source> operator combines with the <source lang=apl inline>+</source> function: the <source lang=apl inline>⌿</source> fixes the <source lang=apl inline>+</source> function between each element of <source lang=apl inline>ω</source>, so that
<source lang=apl>
      +⌿ 1 2 3 4 5 6
21
</source>
is the same as
<source lang=apl>
      1+2+3+4+5+6
21
</source>
=== Operators ===
[[Operator]]s like <source lang=apl inline>⌿</source> can be used to derive new functions not only from [[primitive function]]s like <source lang=apl inline>+</source>, but also from defined functions. For example
<source lang=apl>
      {α,', ',ω}⌿
</source>
will transform a list of strings representing words into a comma-separated list:
<source lang=apl>
      {⍺,', ',⍵}⌿'cow' 'sheep' 'cat' 'dog'
┌────────────────────┐
│cow, sheep, cat, dog│
└────────────────────┘
</source>
So back to our mean example. <source lang=apl inline>(+⌿ω)</source> gives the sum of the list, which is then divided by <source lang=apl inline>≢ω</source>, the number elements in it.
<source lang=apl>
      {(+⌿)÷≢ω} 3 4.5 7 21
8.875
</source>
 
=== Tacit programming ===
 
{{Main|Tacit}}
 
In APL’s tacit definition, no braces are needed to mark the definition of a function: primitive functions just combine in a way that enables us to omit any reference to the function arguments — hence ''tacit''. Here is the same calculation written tacitly:
<source lang=apl>
      (+⌿÷≢) 3 4.5 7 21
8.875
</source>
 
The operator <source lang=apl inline>⌿</source> can also be used to modify the <source lang=apl inline>(+⌿÷≢)</source> function to produce a moving average.
<source lang=apl>
      2 (+⌿÷≢)/ 3 4.5 7 21
3.75 5.75 14
</source>
or, more verbosely
<source lang=apl>
      ave ← +⌿÷≢
      ave 3 4.5 7 21
8.875
      mave ← ave⌿
      2 mave 3 4.5 7 21
3.75 5.75 14
</source>
 
==Text processing==
APL represents text as character lists (vectors), making many text operations trivial.
=== Split text by delimiter ===
<source lang=apl inline>≠</source> gives 1 for true and 0 for false. It [[scalar function|pairs up]] a single element argument with all the elements of the other arguments:
<source lang=apl>
      ','≠'comma,delimited,text'
1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1
</source>
<source lang=apl inline>⊢</source> returns its right argument:
<source lang=apl>
          ','⊢'comma,delimited,text'
comma,delimited,text
</source>
<source lang=apl inline>⊆</source> returns a list of runs as indicated by runs of 1s, leaving out elements indicated by 0s:
<source lang=apl>
      1 1 0 1 1 1⊆'Hello!'
┌──┬───┐
│He│lo!│
└──┴───┘
</source>
We use the comparison [[vector]] to [[partition]] the right argument:
 
[https://tryapl.org/?a=%27%2C%27%28%u2260%u2286%u22A2%29%27comma%2Cdelimited%2Ctext%27&run Try it now!]
<source lang=apl>
      ','(≠⊆⊢)'comma,delimited,text'
┌─────┬─────────┬────┐
│comma│delimited│text│
└─────┴─────────┴────┘
</source>
{{Works in|[[Dyalog APL]]}}
Notice of you can read the [[tacit]] function <source lang=apl inline>≠⊆⊢</source> like an English sentence: ''The inequality partitions the right argument''.
=== Indices of multiple elements ===
=== Indices of multiple elements ===
APL represents text as character lists (vectors), making many text operations trivial:
<source lang=apl inline>∊</source> gives us a mask for elements (characters) in the left argument that are members of the right argument:
<code class="apl">∊</code> gives us a mask for elements (characters) in the left argument that are members of the right argument:
<source lang=apl>
<pre class=apl>
       'mississippi'∊'sp'
       'mississippi'∊'sp'
0 0 1 1 0 1 1 0 1 1 0
0 0 1 1 0 1 1 0 1 1 0
</pre>
</source>
<code class="apl">⍸</code> gives us the indices where true (1):
<source lang=apl inline>⍸</source> gives us the indices where true (1):
<pre class=apl>
<source lang=apl>
       ⍸'mississippi'∊'sp'
       ⍸'mississippi'∊'sp'
3 4 6 7 9 10
3 4 6 7 9 10
</pre>
</source>
We can combine this into an anonymous infix (dyadic) function:
We can combine this into an anonymous infix (dyadic) function:
<pre class=apl>
<source lang=apl>
       'mississippi' (⍸∊) 'sp'
       'mississippi' (⍸∊) 'sp'
3 4 6 7 9 10
3 4 6 7 9 10
</pre>
</source>
{{Works in|[[Dyalog APL]], [[NARS2000]], [[dzaima/APL]]}}
 
=== Frequency of characters in a string ===
The [[Outer Product]] allows for an intuitive way to compute the occurrence of characters at a given location in a string:
<source lang=apl>
      'abcd' ∘.= 'cabbage'
0 1 0 0 1 0 0
0 0 1 1 0 0 0
1 0 0 0 0 0 0
0 0 0 0 0 0 0
</source>
Then it is simply a matter of performing a sum-reduce <source lang=apl inline>+/</source> to calculate the total frequency of each character:<ref name="Marshall LambaConf 2019">[[Marshall Lochbaum]] used this example as part of his talk on [[Outer Product]] at LambdaConf 2019.</ref>
<source lang=apl>
      +/ 'abcd' ∘.= 'cabbage'
2 2 1 0
</source>
 
=== Parenthesis nesting level ===
=== Parenthesis nesting level ===
<pre class=apl>
We can expand on the use of <source lang=apl inline>∘.</source> in the above example to perform more complex calculations. First we compare all characters to the opening and closing characters;
<source lang=apl>
      '()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
</source>
An opening increases the current level, while a closing decreases, so we convert this to ''changes'' (or ''deltas'') by subtracting the bottom row from the top row:
<source lang=apl>
      -⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 ¯1 ¯1 ¯1
</source>
The running sum is what we're looking for:
<source lang=apl>
       +\-⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
       +\-⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 3 2 1
0 0 0 0 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 3 2 1
</pre>
</source>
{{APL programming language}}
{{Works in|all APLs}}
 
=== Grille cypher ===
A [[wikipedia:grille (cryptography)|grille]] is a 500 year old method for encrypting messages.
[[File:Grille.png|none|500px|frameless|The application of a grille cypher]]
<p>
Represent both the grid of letters and the grille as character matrices.
<source lang=apl>
      ⎕←(grid grille)←5 5∘⍴¨'VRYIALCLQIFKNEVPLARKMPLFF' '⌺⌺⌺ ⌺ ⌺⌺⌺ ⌺ ⌺ ⌺⌺⌺ ⌺⌺⌺  ⌺⌺'
┌─────┬─────┐
│VRYIA│⌺⌺⌺ ⌺│
│LCLQI│ ⌺⌺⌺ │
│FKNEV│⌺ ⌺ ⌺│
│PLARK│⌺⌺ ⌺⌺│
│MPLFF│⌺  ⌺⌺│
└─────┴─────┘
</source>
</p>
Retrieve elements of the grid where there are spaces in the grille.
<source lang=apl>
      grid[⍸grille=' ']
ILIKEAPL
</source>
An alternative method using [[ravel]].
<source lang=apl>
      (' '=,grille)/,grid
ILIKEAPL
</source>
===References===
<references/>
{{APL development}}

Revision as of 10:44, 28 January 2020

This page contains examples that show APL's strengths. The examples require minimal background and have no special dependencies. If these examples are too simple for you, have a look at our advanced examples.

Arithmetic mean

Here is an APL program to calculate the average (arithmetic mean) of a list of numbers, written as a dfn:

      {(+⌿ω)÷≢ω}

It is unnamed: the enclosing braces mark it as a function definition. It can be assigned a name for use later, or used anonymously in a more complex expression.

The ω refers to the argument of the function, a list (or 1-dimensional array) of numbers. The denotes the tally function, which returns here the length of (number of elements in) the argument ω. The divide symbol ÷ has its usual meaning.

The parenthesised +⌿ω denotes the sum of all the elements of ω. The operator combines with the + function: the fixes the + function between each element of ω, so that

      +⌿ 1 2 3 4 5 6
21

is the same as

      1+2+3+4+5+6
21

Operators

Operators like can be used to derive new functions not only from primitive functions like +, but also from defined functions. For example

      {α,', ',ω}⌿

will transform a list of strings representing words into a comma-separated list:

      {⍺,', ',⍵}⌿'cow' 'sheep' 'cat' 'dog'
┌────────────────────┐
│cow, sheep, cat, dog│
└────────────────────┘

So back to our mean example. (+⌿ω) gives the sum of the list, which is then divided by ≢ω, the number elements in it.

      {(+⌿)÷≢ω} 3 4.5 7 21
8.875

Tacit programming

Main article: Tacit

In APL’s tacit definition, no braces are needed to mark the definition of a function: primitive functions just combine in a way that enables us to omit any reference to the function arguments — hence tacit. Here is the same calculation written tacitly:

      (+⌿÷≢) 3 4.5 7 21
8.875

The operator can also be used to modify the (+⌿÷≢) function to produce a moving average.

      2 (+⌿÷≢)/ 3 4.5 7 21
3.75 5.75 14

or, more verbosely

      ave ← +⌿÷≢
      ave 3 4.5 7 21
8.875
      mave ← ave⌿
      2 mave 3 4.5 7 21
3.75 5.75 14

Text processing

APL represents text as character lists (vectors), making many text operations trivial.

Split text by delimiter

gives 1 for true and 0 for false. It pairs up a single element argument with all the elements of the other arguments:

      ','≠'comma,delimited,text'
1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1

returns its right argument:

          ','⊢'comma,delimited,text'
comma,delimited,text

returns a list of runs as indicated by runs of 1s, leaving out elements indicated by 0s:

      1 1 0 1 1 1⊆'Hello!'
┌──┬───┐
│He│lo!│
└──┴───┘

We use the comparison vector to partition the right argument:

Try it now!

      ','(≠⊆⊢)'comma,delimited,text'
┌─────┬─────────┬────┐
│comma│delimited│text│
└─────┴─────────┴────┘
Works in: Dyalog APL

Notice of you can read the tacit function ≠⊆⊢ like an English sentence: The inequality partitions the right argument.

Indices of multiple elements

gives us a mask for elements (characters) in the left argument that are members of the right argument:

      'mississippi'∊'sp'
0 0 1 1 0 1 1 0 1 1 0

gives us the indices where true (1):

      ⍸'mississippi'∊'sp'
3 4 6 7 9 10

We can combine this into an anonymous infix (dyadic) function:

      'mississippi' (⍸∊) 'sp'
3 4 6 7 9 10

Frequency of characters in a string

The Outer Product allows for an intuitive way to compute the occurrence of characters at a given location in a string:

      'abcd' ∘.= 'cabbage'
 0 1 0 0 1 0 0
 0 0 1 1 0 0 0
 1 0 0 0 0 0 0
 0 0 0 0 0 0 0

Then it is simply a matter of performing a sum-reduce +/ to calculate the total frequency of each character:[1]

      +/ 'abcd' ∘.= 'cabbage'
 2 2 1 0

Parenthesis nesting level

We can expand on the use of ∘. in the above example to perform more complex calculations. First we compare all characters to the opening and closing characters;

      '()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1

An opening increases the current level, while a closing decreases, so we convert this to changes (or deltas) by subtracting the bottom row from the top row:

      -⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 ¯1 ¯1 ¯1

The running sum is what we're looking for:

      +\-⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 3 2 1
Works in: all APLs

Grille cypher

A grille is a 500 year old method for encrypting messages.

The application of a grille cypher

Represent both the grid of letters and the grille as character matrices.

      ⎕←(grid grille)←5 5∘⍴¨'VRYIALCLQIFKNEVPLARKMPLFF' '⌺⌺⌺ ⌺ ⌺⌺⌺ ⌺ ⌺ ⌺⌺⌺ ⌺⌺⌺  ⌺⌺'
┌─────┬─────┐
│VRYIA│⌺⌺⌺ ⌺│
│LCLQI│ ⌺⌺⌺ │
│FKNEV│⌺ ⌺ ⌺│
│PLARK│⌺⌺ ⌺⌺│
│MPLFF│⌺  ⌺⌺│
└─────┴─────┘

Retrieve elements of the grid where there are spaces in the grille.

      grid[⍸grille=' ']
ILIKEAPL

An alternative method using ravel.

      (' '=,grille)/,grid
ILIKEAPL

References

  1. Marshall Lochbaum used this example as part of his talk on Outer Product at LambdaConf 2019.
APL development [edit]
Interface SessionTyping glyphs (on Linux) ∙ FontsText editors
Publications IntroductionsLearning resourcesSimple examplesAdvanced examplesMnemonicsISO 8485:1989ISO/IEC 13751:2001A Dictionary of APLCase studiesDocumentation suitesBooksPapersVideosAPL Quote QuadVector journalTerminology (Chinese, German) ∙ Neural networksError trapping with Dyalog APL (in forms)
Sharing code Backwards compatibilityAPLcartAPLTreeAPL-CationDfns workspaceTatinCider
Implementation ResourcesOpen-sourceMagic functionPerformanceAPL hardware
Developers Timeline of corporationsAPL2000DyalogIBMIPSASTSC