Simple examples: Difference between revisions

Jump to navigation Jump to search
1,266 bytes removed ,  22:44, 27 June 2020
Remove faulty part
(Remove faulty part)
(6 intermediate revisions by 2 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 47: Line 47:
       (+⌿÷≢) 3 4.5 7 21
       (+⌿÷≢) 3 4.5 7 21
8.875
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>
</source>


Line 129: Line 114:


=== Parenthesis nesting level ===
=== Parenthesis nesting level ===
{{quote | "I was at a meeting in Newcastle, England, where I’d been invited to give a talk, as had Don Knuth of Stanford, Ken Iverson from IBM, and a few others as well. I was sitting in the audience sandwiched between two very esteemed people in computer science and computing — Fritz Bauer, who runs computing in Bavaria from his headquarters in Munich, and Edsger Dijkstra, who runs computing all over the world from his headquarters in Holland.
{{quote | "Ken was showing some slides — and one of his slides had something on it that I was later to learn was an APL one-liner. And he tossed this off as an example of the expressiveness of the APL notation. I believe the one-liner was one of the standard ones for indicating the nesting level of the parentheses in an algebraic expression. But the one-liner was very short — ten characters, something like that — and having been involved with programming things like that for a long time and realizing that it took a reasonable amount of code to do, I looked at it and said, “My God, there must be something in this language.”"|[[Alan Perlis]]. ''[https://www.jsoftware.com/papers/perlis78.htm Almost Perfect Artifacts Improve only in Small Ways: APL is more French than English]'' at [[APL78]].}}
Ken was showing some slides — and one of his slides had something on it that I was later to learn was an APL one-liner. And he tossed this off as an example of the expressiveness of the APL notation. I believe the one-liner was one of the standard ones for indicating the nesting level of the parentheses in an algebraic expression. But the one-liner was very short — ten characters, something like that — and having been involved with programming things like that for a long time and realizing that it took a reasonable amount of code to do, I looked at it and said, “My God, there must be something in this language.” Bauer, on my left, didn’t see that. What he saw or heard was Ken’s remark that APL is an extremely appropriate language for teaching algebra, and he muttered under his breath to me, in words I will never forget, “As long as I am alive, APL will never be used in Munich.” And Dijkstra, who was sitting on my other side, leaned toward Bauer and said, “Nor in Holland.” The three of us were listening to the same lecture, but we obviously heard different things."|[[Alan Perlis]]. ''[https://www.jsoftware.com/papers/perlis78.htm Almost Perfect Artifacts Improve only in Small Ways: APL is more French than English]'' at [[APL78]].}}
What was the one-liner for the nesting level of parentheses? It would take a bit of work to figure out, because at the time of the meeting Perlis described, no APL implementation existed. Two possibilities are explained here.
What was the one-liner for the nesting level of parentheses? It would take a bit of work to figure out, because at the time of the meeting Perlis described, no APL implementation existed. Two possibilities are explained here.
=== Method A ===
==== Method A ====
For this more complex computation, we can expand on the previous example's use of <source lang=apl inline>∘.=</source>. First we compare all characters to the opening and closing characters;
For this more complex computation, we can expand on the previous example's use of <source lang=apl inline>∘.=</source>. First we compare all characters to the opening and closing characters;
<source lang=apl>
<source lang=apl>
Line 150: Line 134:
</source>
</source>
{{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 199: Line 183:
<references/>
<references/>
{{APL development}}
{{APL development}}
[[Category:Examples]]

Navigation menu