Sum and product of an array: Difference between revisions
Content added Content deleted
(→{{header|APL}}: Added a solution with the pair primitive function in dzaima/APL and Extended Dyalog APL) |
|||
Line 260: | Line 260: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|APL2}} |
{{works with|APL2}} |
||
<lang apl> sum ← +/ |
<lang apl> sum ← +/ ⍝ sum (+) over (/) an array |
||
prod ← ×/ |
prod ← ×/ ⍝ product (×) over (/) an array |
||
a ← 1 2 3 4 5 ⍝ assign a literal array to variable 'a' |
|||
sum |
sum a |
||
15 |
15 |
||
prod |
prod a |
||
120</lang> |
120</lang> |
||
What follows ⍝ is a comment and / is usually known as ''reduce'' in APL. |
|||
{{works with|dzaima/APL}} |
|||
{{works with|Extended Dyalog APL}} |
|||
using the [https://aplwiki.com/wiki/Pair pair (⍮)] primitive function ([https://tio.run/##SyzI0U2pSszMTfz//1Hf1EdtEzS09R/1rjs8XV/TUMFIwVjBRMH0/38A try it online]) |
|||
<lang apl> ⎕ ← (+/ ⍮ ×/) 1 2 3 4 5 |
|||
15 120</lang> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |