Averages/Arithmetic mean: Difference between revisions

m
m (→‎{{header|Sidef}}: updated code)
(5 intermediate revisions by 3 users not shown)
Line 329:
=={{header|APL}}==
{{works with|APL2}}
<syntaxhighlight lang="apl"> X←3 1 4 1 5 9
X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</syntaxhighlight>
3.6</syntaxhighlight>
 
{{works with|langur|0.6.6Dyalog APL}}
A proper function definition:
<syntaxhighlight lang="rpl/2apl">1 2 3 5 7
Avg←{(+⌿⍵)÷≢⍵}
Avg 1 2 3 4 5 6
3.5
</syntaxhighlight>
 
Using [[tacit programming]]:
<syntaxhighlight lang="apl">
Avg← +⌿÷≢
Avg 1 2 3 4 5 6
3.5
</syntaxhighlight>
'''N.B.:''' the symbol for [https://aplwiki.com/wiki/Tally Tally (≢)] doesn't display correctly on Chrome-based browsers at the moment.
 
=={{header|AppleScript}}==
Line 1,963 ⟶ 1,981:
We could use fold() to write a function that takes an array and calculates the mean.
 
<syntaxhighlight lang="langur">val .mean = ffn(.x) { fold(ffn{+}, .x) / len(.x) }
{{works with|langur|0.6.6}}
<syntaxhighlight lang="langur">val .mean = f(.x) fold(f{+}, .x) / len(.x)
 
writeln " custom: ", .mean([7, 3, 12])
Line 3,375 ⟶ 3,392:
 
=={{header|RPL}}==
This is based on the dc version above.
This is a simple rewrite of the dc version above. This works on an HP 48. "→" is a single right arrow character on the 48. Feel free to alter this code as necessary to work on RPL/2.
{{works with|HP|48G}}
≪ DUP 'N' STO →LIST ΣLIST N / 'N' PURGE ≫ '<span style="color:blue">AMEAN</span>' STO
or,by using the stack instead of a temporary variable:
≪ →LIST ΣLIST LASTARG SIZE / ≫ '<span style="color:blue">AMEAN</span>' STO
 
CLEAR 1 2 3 5 7 DEPTH <span style="color:blue">AMEAN</span>
<syntaxhighlight lang="rpl/2">1 2 3 5 7
AMEAN
<< CLEAR DEPTH DUP 'N' STO →LIST ΣLIST N / >>
3.6</syntaxhighlight>
 
===Hard-working approach===
885

edits