Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
APL (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Examples === ==== Hello, world ==== This displays "[[Hello, world]]": <syntaxhighlight lang="apl"> 'Hello, world' </syntaxhighlight> A design theme in APL is to define default actions in some cases that would produce syntax errors in most other programming languages. The 'Hello, world' string constant above displays, because display is the default action on any expression for which no action is specified explicitly (e.g. assignment, function parameter). ==== Exponentiation ==== Another example of this theme is that exponentiation in APL is written as {{code|2*3}}, which indicates raising 2 to the power 3 (this would be written as {{code|2^3}} or {{code|2**3}} in some languages, or relegated to a function call such as {{code|pow(2, 3);}} in others). Many languages use {{code|*}} to signify multiplication, as in {{code|2*3}}, but APL chooses to use {{code|2×3}}. However, if no base is specified (as with the statement {{code|*3}} in APL, or {{code|^3}} in other languages), most programming languages one would see this as a syntax error. APL, however, assumes the missing base to be the natural logarithm constant [[e (mathematical constant)|e]], and interprets {{code|*3}} as {{code|2.71828*3}}. ==== Simple statistics ==== Suppose that {{code|X}} is an array of numbers. Then {{code|(+/X)÷⍴X}} gives its average. Reading ''right-to-left'', {{code|⍴X}} gives the number of elements in X, and since {{code|÷}} is a dyadic operator, the term to its left is required as well. It is surrounded by parentheses since otherwise X would be taken (so that the summation would be of {{code|X÷⍴X}}—each element of X divided by the number of elements in X), and {{code|+/X}} gives the sum of the elements of X. Building on this, the following expression computes [[standard deviation]]: {{sxhl|2=apl|((+/((X - (+/X)÷⍴X)*2))÷⍴X)*0.5}} Naturally, one would define this expression as a function for repeated use rather than rewriting it each time. Further, since assignment is an operator, it can appear within an expression, so the following would place suitable values into T, AV and SD: {{sxhl|2=apl|SD←((+/((X - AV←(T←+/X)÷⍴X)*2))÷⍴X)*0.5}} ==== ''Pick 6'' lottery numbers ==== This following immediate-mode expression generates a typical set of ''Pick 6'' [[lottery]] numbers: six [[pseudo-random]] [[integer]]s ranging from 1 to 40, ''guaranteed non-repeating'', and displays them sorted in ascending order: <syntaxhighlight lang="apl"> x[⍋x←6?40] </syntaxhighlight> The above does a lot, concisely, although it may seem complex to a new [[wiktionary:APLer|APLer]]. It combines the following APL ''functions'' (also called ''primitives''<ref>{{cite web|last1=MicroAPL|title=APL Primitives |url=http://www.microapl.co.uk/apl_help/ch_020_020.htm |website=www.microapl.co.uk |publisher=MicroAPL |access-date=January 11, 2015}}</ref> and ''glyphs''<ref>{{cite web |title=APL Font – Extra APL Glyphs |website=wiki.nars2000.org |url=http://wiki.nars2000.org/index.php/APL_Font |publisher=NARS2000 |access-date=January 11, 2015}}</ref>): * The first to be executed (APL executes from rightmost to leftmost) is dyadic function <code>?</code> (named <code>deal</code> when dyadic) that returns a [[array data structure|vector]] consisting of a select number (left argument: 6 in this case) of random integers ranging from 1 to a specified maximum (right argument: 40 in this case), which, if said maximum ≥ vector length, is guaranteed to be non-repeating; thus, generate/create 6 random integers ranging from 1 to 40.<ref>{{cite web |last1=Fox |first1=Ralph L. |title=Systematically Random Numbers |publisher=SIGAPL |url=http://www.sigapl.org/article1.php |website=www.sigapl.org |access-date=January 11, 2015}}</ref> * This vector is then ''assigned'' (<code>←</code>) to the variable <code>x</code>, because it is needed later. * This vector is then ''sorted'' in ascending order by a monadic <code>⍋</code> function, which has as its right argument everything to the right of it up to the next unbalanced ''close-bracket'' or close-parenthesis. The result of <code>⍋</code> is the indices that will put its argument into ascending order. * Then the output of <code>⍋</code> is used to index the variable <code>x</code>, which we saved earlier for this purpose, thereby selecting its items in ''ascending'' sequence. Since there is no function to the left of the left-most x to tell APL what to do with the result, it simply outputs it to the display (on a single line, separated by spaces) without needing any explicit instruction to do that. <code>?</code> also has a monadic equivalent called <code>roll</code>, which simply returns one random integer between 1 and its sole operand [to the right of it], inclusive. Thus, a [[role-playing game]] program might use the expression <code>?20</code> to roll a twenty-sided die. ==== Prime numbers ==== The following expression finds all [[prime number]]s from 1 to R. In both time and space, the calculation complexity is <math>O(R^2)\,\!</math> (in [[Big O notation]]). <syntaxhighlight lang="apl"> (~R∊R∘.×R)/R←1↓⍳R </syntaxhighlight> Executed from right to left, this means: * ''[[Iota]]'' <code>⍳</code> creates a vector containing [[integer]]s from <code>1</code> to <code>R</code> (if <code>R= 6</code> at the start of the program, <code>⍳R</code> is <code>1 2 3 4 5 6</code>) * ''Drop'' first element of this vector (<code>↓</code> function), i.e., <code>1</code>. So <code>1↓⍳R</code> is <code>2 3 4 5 6</code> * ''Set'' <code>R</code> to the new vector (<code>←</code>, ''assignment'' primitive), i.e., <code>2 3 4 5 6</code> * The <code>/</code> ''replicate'' operator is dyadic (binary) and the interpreter first evaluates its left argument (fully in parentheses): * Generate ''[[outer product]]'' of <code>R</code> multiplied by <code>R</code>, i.e., a matrix that is the ''[[multiplication table]]'' of R by R (<code>°.×</code> operator), i.e., {| class="wikitable" style="text-align:right;" |- | 4 | 6 | 8 | 10 | 12 |- | 6 | 9 | 12 | 15 | 18 |- | 8 | 12 | 16 | 20 | 24 |- | 10 | 15 | 20 | 25 | 30 |- | 12 | 18 | 24 | 30 | 36 |} * Build a vector the same length as <code>R</code> with <code>1</code> in each place where the corresponding number in <code>R</code> is in the outer product matrix (<code>∈</code>, ''set inclusion'' or ''element of'' or ''[[Epsilon]]'' operator), i.e., <code>0 0 1 0 1</code> * Logically negate (''not'') values in the vector (change zeros to ones and ones to zeros) (<code>∼</code>, logical ''not'' or ''[[Tilde]]'' operator), i.e., <code>1 1 0 1 0</code> * Select the items in <code>R</code> for which the corresponding element is <code>1</code> (<code>/</code> ''replicate'' operator), i.e., <code>2 3 5</code> (This assumes the APL origin is 1, i.e., indices start with 1. APL can be set to use 0 as the origin, so that <code>ι6</code> is <code>0 1 2 3 4 5</code>, which is convenient for some calculations.) ==== Sorting ==== The following expression [[sorting|sorts]] a word list stored in matrix X according to word length: <syntaxhighlight lang="apl"> X[⍋X+.≠' ';] </syntaxhighlight> ==== Game of Life ==== The following function "life", written in Dyalog APL,<ref>{{cite AV media |authorlink=John M. Scholes |last1=Scholes |first1=John |date=January 26, 2009 |title=Conway's Game of Life in APL |medium=video |url=http://www.youtube.com/watch?v=a9xAKttWgP4 |location= |publisher=[[YouTube]]|access-date=November 20, 2021}}</ref><ref>Further technical details in [https://aplwiki.com/wiki/Conway%27s_Game_of_Life APL Wiki's article "Conway's Game of Life"]. Retrieved November 20, 2021.</ref> takes a Boolean matrix and calculates the new generation according to [[Conway's Game of Life]]. It demonstrates the power of APL to implement a complex algorithm in very little code, but understanding it requires some advanced knowledge of APL (as the same program would in many languages). <syntaxhighlight lang="apl"> life ← {⊃1 ⍵ ∨.∧ 3 4 = +/ +⌿ ¯1 0 1 ∘.⊖ ¯1 0 1 ⌽¨ ⊂⍵} </syntaxhighlight> ==== HTML tags removal ==== In the following example, also Dyalog, the first line assigns some HTML code to a variable <code>txt</code> and then uses an APL expression to remove all the HTML tags: <syntaxhighlight lang="apl"> txt←'<html><body><p>This is <em>emphasized</em> text.</p></body></html>' {⍵ /⍨ ~{⍵∨≠\⍵}⍵∊'<>'} txt This is emphasized text. </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)