Simple examples: Difference between revisions

Jump to navigation Jump to search
1,655 bytes added ,  10:44, 28 January 2020
m
Miraheze>Adám Brudzewsky
No edit summary
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
This page contains examples that show APL's strengths. The examples 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]].


More involved examples include:
* [[Ranking poker hands]]
* [[APL Wiki logo]]
* [[Error trapping with Dyalog APL]]
== Arithmetic mean ==
== Arithmetic mean ==


Line 35: Line 28:
will transform a list of strings representing words into a comma-separated list:
will transform a list of strings representing words into a comma-separated list:
<source lang=apl>
<source lang=apl>
    {⍺,', ',⍵}⌿'cow' 'sheep' 'cat' 'dog'
      {⍺,', ',⍵}⌿'cow' 'sheep' 'cat' 'dog'
┌────────────────────┐
┌────────────────────┐
│cow, sheep, cat, dog│
│cow, sheep, cat, dog│
└────────────────────┘
└────────────────────┘
</source>
</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 of its elements.
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>
<source lang=apl>
       {(+)÷ρω} 3 4.5 7 21
       {(+)÷≢ω} 3 4.5 7 21
8.875
8.875
</source>
</source>


=== Tacit programming ===
=== 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:
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:
Line 54: Line 49:
</source>
</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.
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>
<source lang=apl>
       2 (+⌿÷≢)/ 3 4.5 7 21
       2 (+⌿÷≢)/ 3 4.5 7 21
Line 64: Line 59:
       ave 3 4.5 7 21
       ave 3 4.5 7 21
8.875
8.875
       mave ← ave/
       mave ← ave⌿
       2 mave 3 4.5 7 21
       2 mave 3 4.5 7 21
3.75 5.75 14
3.75 5.75 14
Line 117: Line 112:
</source>
</source>
{{Works in|[[Dyalog APL]], [[NARS2000]], [[dzaima/APL]]}}
{{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 ===
First we compare all characters to the opening and closing characters;
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>
<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)))'
Line 136: Line 146:
</source>
</source>
{{Works in|all APLs}}
{{Works in|all APLs}}
{{APL programming language}}
 
=== 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}}

Navigation menu