Simple examples: Difference between revisions

Jump to navigation Jump to search
208 bytes added ,  05:49, 26 August 2020
m
(8 intermediate revisions by 3 users not shown)
Line 5: Line 5:
Here is an APL program to calculate the average (arithmetic mean) of a list of numbers, written as a [[dfn]]:
Here is an APL program to calculate the average (arithmetic mean) of a list of numbers, written as a [[dfn]]:
<source lang=apl>
<source lang=apl>
       {(+⌿ω)÷≢ω}  
       {(+⌿⍵)÷≢⍵}  
</source>
</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.
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 <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
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>
<source lang=apl>
       +⌿ 1 2 3 4 5 6
       +⌿ 1 2 3 4 5 6
Line 24: Line 24:
[[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
[[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 lang=apl>
       {α,', ',ω}⌿
       {,', ',}⌿
</source>
</source>
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:
Line 33: Line 33:
└────────────────────┘
└────────────────────┘
</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 elements in it.
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>
Line 41: Line 41:
=== Tacit programming ===
=== Tacit programming ===


{{Main|Tacit}}
{{Main|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:
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 49: 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.
This is a so called 3-train, also known as a ''fork''. It is evaluated like this:
<source lang=apl>
{|
      2 (+⌿÷≢)/ 3 4.5 7 21
|<source lang=apl>(+⌿ ÷ ≢) 3 4.5 7 21</source>|| {{←→}} ||<source lang=apl>(+3 4.5 7 21) ÷ (≢ 3 4.5 7 21)</source>
3.75 5.75 14
|}
</source>
 
or, more verbosely
Note that <source lang=apl inline>+⌿</source> is evaluated as a single derived function.
<source lang=apl>
The general scheme for monadic 3-trains is the following:
      ave ← +⌿÷≢
{|
      ave 3 4.5 7 21
|<source lang=apl>(f g h) ⍵</source>|| {{←→}} ||<source lang=apl>(f ⍵) g (h ⍵)</source>
8.875
|}
      mave ← ave⌿
 
      2 mave 3 4.5 7 21
But other types of [[Tacit programming#Trains|trains]] are also possible.
3.75 5.75 14
</source>


==Text processing==
==Text processing==
Line 94: Line 92:
</source>
</source>
{{Works in|[[Dyalog APL]]}}
{{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''.
Notice that 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 ===
<source lang=apl inline>∊</source> gives us a mask for elements (characters) in the left argument that are members of the right argument:
<source lang=apl inline>∊</source> gives us a mask for elements (characters) in the left argument that are members of the right argument:
Line 150: Line 149:
{{Works in|all APLs}}
{{Works in|all APLs}}
==== Method B ====
==== Method B ====
Alternatively, we can utilise that if the [Index Of]] function <source lang=apl inline>⍳</source> doesn't find what it is looking for, it returns the next index after the last element in the the lookup array:
Alternatively, we can utilise that if the [[Index Of]] function <source lang=apl inline>⍳</source> doesn't find what it is looking for, it returns the next index after the last element in the the lookup array:
<source lang=apl>
<source lang=apl>
       'ABBA'⍳'ABC'
       'ABBA'⍳'ABC'
Line 198: Line 197:
<references/>
<references/>
{{APL development}}
{{APL development}}
[[Category:Examples]]

Navigation menu