Simple examples: Difference between revisions

Jump to navigation Jump to search
2,866 bytes added ,  00:42, 4 November 2019
Miraheze>Adám Brudzewsky
No edit summary
Miraheze>Adám Brudzewsky
Line 6: Line 6:


* [[APL Wiki logo]]
* [[APL Wiki logo]]
== 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 of its elements.
<source lang=apl>
      {(+/ω)÷ρω} 3 4.5 7 21
8.875
</source>
=== Tacit programming ===
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 inline>
      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==
==Text processing==

Navigation menu