Sum and product of an array: Difference between revisions
Content added Content deleted
(add bqn) |
|||
Line 2,150: | Line 2,150: | ||
echo array_sum($array); |
echo array_sum($array); |
||
echo array_product($array);</lang> |
echo array_product($array);</lang> |
||
=={{header|Picat}}== |
|||
<lang Picat>go => |
|||
L = 1..10, |
|||
println(sum=sum(L)), |
|||
println(prod=prod(L)), |
|||
nl, |
|||
println(sum_reduce=reduce(+,L)), |
|||
println(prod_reduce=reduce(*,L)), |
|||
println(sum_reduce=reduce(+,L,0)), |
|||
println(prod_reduce=reduce(*,L,1)), |
|||
nl, |
|||
println(sum_fold=fold(+,0,L)), |
|||
println(prod_fold=fold(*,1,L)), |
|||
nl, |
|||
println(sum_rec=sum_rec(L)), |
|||
println(prod_rec=prod_rec(L)), |
|||
nl. |
|||
% recursive variants |
|||
sum_rec(List) = Sum => |
|||
sum_rec(List,0,Sum). |
|||
sum_rec([],Sum0,Sum) => |
|||
Sum=Sum0. |
|||
sum_rec([H|T], Sum0,Sum) => |
|||
sum_rec(T, H+Sum0,Sum). |
|||
prod_rec(List) = Prod => |
|||
prod_rec(List,1,Prod). |
|||
prod_rec([],Prod0,Prod) => |
|||
Prod=Prod0. |
|||
prod_rec([H|T], Prod0,Prod) => |
|||
prod_rec(T, H*Prod0,Prod).</lang> |
|||
{{out}} |
|||
<pre>sum = 55 |
|||
prod = 3628800 |
|||
sum_reduce = 55 |
|||
prod_reduce = 3628800 |
|||
sum_reduce = 55 |
|||
prod_reduce = 3628800 |
|||
sum_fold = 55 |
|||
prod_fold = 3628800 |
|||
sum_rec = 55 |
|||
prod_rec = 3628800</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |