Averages/Arithmetic mean: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 266:
RealF(s,mean([1.0, 2.0, 3.0, 4.0, 5.0]), 2))
ENDPROC</lang>
 
=={{header|AntLang}}==
AntLang has a built-in avg function.
<lang AntLang>avg[list]</lang>
 
=={{header|APL}}==
{{works with|APL2}}
<lang apl> X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</lang>
 
=={{header|Applesoft BASIC}}==
Line 286 ⟶ 296:
N = A(0) : IF N THEN S = 0 : FOR I = 1 TO N : S = S + A(I) : NEXT : ? S / N
</lang>
=={{header|AntLang}}==
AntLang has a built-in avg function.
<lang AntLang>avg[list]</lang>
 
=={{header|Arturo}}==
Line 348 ⟶ 355:
zero-length input !
</pre>
 
=={{header|APL}}==
{{works with|APL2}}
<lang apl> X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</lang>
 
=={{header|Babel}}==
Line 952 ⟶ 953:
AveVal(SetVals) //returns 30.0 ;
</lang>
 
=={{header|Elena}}==
ELENA 5.0:
Line 1,049 ⟶ 1,051:
10
</pre>
 
=={{header|F_Sharp|F#}}==
The following computes the running mean using a tail-recursive approach. If we just sum all the values then divide by the number of values then we will suffer from overflow problems for large lists. See [[wp:Moving_average|wikipedia]] about the moving average computation.
<lang fsharp>let avg (a:float) (v:float) n =
a + (1. / ((float n) + 1.)) * (v - a)
 
let mean_series list =
let a, _ = List.fold_left (fun (a, n) h -> avg a (float h) n, n + 1) (0., 0) list in
a</lang>
 
Checking this:
<lang fsharp> > mean_series [1; 8; 2; 8; 1; 7; 1; 8; 2; 7; 3; 6; 1; 8; 100] ;;
val it : float = 10.86666667
> mean_series [] ;;
val it : float = 0.0</lang>
 
We can also make do with the built-in ''List.average'' function:
<lang fsharp>List.average [4;1;7;5;8;4;5;2;1;5;2;5]</lang>
 
=={{header|Factor}}==
Line 1,197 ⟶ 1,217:
mean[x] := length[x] > 0 ? sum[x] / length[x] : undef
</lang>
 
=={{header|F_Sharp|F#}}==
The following computes the running mean using a tail-recursive approach. If we just sum all the values then divide by the number of values then we will suffer from overflow problems for large lists. See [[wp:Moving_average|wikipedia]] about the moving average computation.
<lang fsharp>let avg (a:float) (v:float) n =
a + (1. / ((float n) + 1.)) * (v - a)
 
let mean_series list =
let a, _ = List.fold_left (fun (a, n) h -> avg a (float h) n, n + 1) (0., 0) list in
a</lang>
 
Checking this:
<lang fsharp> > mean_series [1; 8; 2; 8; 1; 7; 1; 8; 2; 7; 3; 6; 1; 8; 100] ;;
val it : float = 10.86666667
> mean_series [] ;;
val it : float = 0.0</lang>
 
We can also make do with the built-in ''List.average'' function:
<lang fsharp>List.average [4;1;7;5;8;4;5;2;1;5;2;5]</lang>
 
=={{header|GAP}}==
Line 1,708 ⟶ 1,710:
print "Arithmetic mean: ";mean
</lang>
 
=={{header|Limbo}}==
<lang Limbo>implement Command;
Line 2,109 ⟶ 2,112:
RETURN sum / FLOAT(Samples)
END Average;</lang>
 
=={{header|MUMPS}}==
<lang MUMPS>MEAN(X)
Line 2,289 ⟶ 2,293:
=> 1.9619999999999997
</lang>
 
=={{header|Oberon-2}}==
Oxford Oberon-2
Line 2,326 ⟶ 2,331:
14.55
</pre>
 
=={{header|Objeck}}==
<lang objeck>
Line 2,538 ⟶ 2,544:
print avg(qw(3 1 4 1 5 9)), "\n";</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2015.10-11}}
 
<lang perl6>multi mean([]){ Failure.new('mean on empty list is not defined') }; # Failure-objects are lazy exceptions
multi mean (@a) { ([+] @a) / @a }</lang>
 
=={{header|Phix}}==
Line 2,563:
else
echo "0\n";</lang>
 
=={{header|PL/I}}==
<lang pli>arithmetic_mean = sum(A)/dimension(A,1);</lang>
 
=={{header|PicoLisp}}==
Line 2,575 ⟶ 2,572:
<pre>: (mean (range 1 1000))
-> 500</pre>
 
=={{header|PL/I}}==
<lang pli>arithmetic_mean = sum(A)/dimension(A,1);</lang>
 
=={{header|Pop11}}==
Line 2,740:
(mean #(3 4 5 8)) ; -> 5
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.10-11}}
 
<lang perl6>multi mean([]){ Failure.new('mean on empty list is not defined') }; # Failure-objects are lazy exceptions
multi mean (@a) { ([+] @a) / @a }</lang>
 
=={{header|REBOL}}==
Line 3,163 ⟶ 3,170:
mean(a)
16715.75</lang>
 
=={{header|Swift}}==
<lang swift>func meanDoubles(s: [Double]) -> Double {
Line 3,209 ⟶ 3,217:
alert( mean( [] ) );
</lang>
 
=={{header|UnixPipes}}==
{{incorrect|UnixPipes|There is a race between parallel commands. <code>cat count</code> might try to read the file before <code>wc -l >count</code> writes it. This may cause an error like ''cat: count: No such file or directory'', then ''bc: stdin:1: syntax error: ) unexpected''.}}
 
Uses [[ksh93]]-style process substitution. Also overwrites the file named <tt>count</tt> in the current directory.
{{works with|bash}}
<lang bash>term() {
b=$1;res=$2
echo "scale=5;$res+$b" | bc
}
 
sum() {
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
}
 
fold() {
func=$1
(while read a ; do
fold $func | $func $a
done)
}
 
mean() {
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
}
 
(echo 3; echo 1; echo 4) | mean</lang>
 
=={{header|UNIX Shell}}==
Line 3,265 ⟶ 3,245:
printf "test 5: "; mean - # expr: syntax error
printf "test 6: "; mean 1 2 A 3 # expr: non-numeric argument</lang>
 
=={{header|UnixPipes}}==
{{incorrect|UnixPipes|There is a race between parallel commands. <code>cat count</code> might try to read the file before <code>wc -l >count</code> writes it. This may cause an error like ''cat: count: No such file or directory'', then ''bc: stdin:1: syntax error: ) unexpected''.}}
 
Uses [[ksh93]]-style process substitution. Also overwrites the file named <tt>count</tt> in the current directory.
{{works with|bash}}
<lang bash>term() {
b=$1;res=$2
echo "scale=5;$res+$b" | bc
}
 
sum() {
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
}
 
fold() {
func=$1
(while read a ; do
fold $func | $func $a
done)
}
 
mean() {
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
}
 
(echo 3; echo 1; echo 4) | mean</lang>
 
=={{header|Ursa}}==
Line 3,292 ⟶ 3,300:
output:
<pre>1.600000e+00</pre>
 
 
=={{header|V}}==
Line 3,372 ⟶ 3,379:
mean[1] = 0
mean[] = Fout 2007</pre>
 
=={{header|VBScript}}==
<lang vb>
Line 3,440 ⟶ 3,448:
Output:
<pre>2</pre>
 
=={{header|Wren}}==
<lang wren>class Arithmetic {
static mean(arr) {
if (arr.count == 0) Fiber.abort("Length must be greater than zero")
return arr.reduce(Fn.new{ |x,y| x+y }) / arr.count
}
}
Arithmetic.mean([1,2,3,4,5]) // 3
</lang>
 
=={{header|Wortel}}==
Line 3,466 ⟶ 3,464:
Returns:
<pre>[3.5714285714285716 3.5714285714285716]</pre>
 
=={{header|Wren}}==
<lang wren>class Arithmetic {
static mean(arr) {
if (arr.count == 0) Fiber.abort("Length must be greater than zero")
return arr.reduce(Fn.new{ |x,y| x+y }) / arr.count
}
}
Arithmetic.mean([1,2,3,4,5]) // 3
</lang>
 
=={{header|XLISP}}==
10,339

edits