Sum of Array: Difference between revisions

From Rosetta Code
Content deleted Content added
mNo edit summary
Line 1: Line 1:
[http://kitmun.cn/zika-cvetkovic.html zika cvetkovic] [http://wonted.cn/www-win-it-ricerca/index.html www win it ricerca v videos de hector y] [http://bordeisbordei.clung.cn/index.html bordeis bordei] [http://knock-knock.cn/www-webmail/index.html www webmail uiowa edu] [http://bersuitvergarabatcomar.clung.cn/index.html bersuitvergarabat com ar] [http://bigfun.clung.cn/index.html bigfun] [http://quoits.cn/angi-vera.html angi vera] [http://snailtail.cn/www-piss/index.html www piss] [http://wonted.cn/www-ultra/index.html www ultra blue tv com] [http://bandida.romanikki.cn/index.html bandida] [http://biografiamercedesambrus.kittiss.cn/index.html biografia mercedes ambrus] [http://kitmun.cn/you-re-crazy.html you re crazy] [http://wonted.cn/wilehalm-eschenbach/index.html wilehalm eschenbach] [http://kitmun.cn/you-and.html you and me] [http://kitmun.cn/zita.html zita] [http://kitmun.cn/yutaka-ozaki.html yutaka ozaki] [http://brel.romanikki.cn/index.html brel] [http://knock-knock.cn/wyhylylybymy/index.html wyhylylybymy] [http://bandalimon.clung.cn/index.html banda limon] [http://knock-knock.cn/www-diddle/index.html www diddle] [http://barzelletteufficio.fast-road.cn/index.html barzellette ufficio] [http://snailtail.cn/yulia-mayarchuck/index.html yulia mayarchuck nuda] [http://quoits.cn/zoccolone.html zoccolone] [http://wonted.cn/www-ausoniatools/index.html www ausoniatools com] [http://berlinoesto.fast-road.cn/index.html berlino esto] [http://bumerklingelton.clung.cn/index.html bumer klingelton] [http://quoits.cn/al-piccolo.html al piccolo margherita] [http://wonted.cn/www-basketball/index.html www basketball it] [http://barbieredellasera.clung.cn/index.html barbiere della sera] [http://kitmun.cn/yates.html yates] [http://britneyfoto.fast-road.cn/index.html britney foto] [http://wonted.cn/www-lunapop/index.html www lunapop com] [http://basicinstict.romanikki.cn/index.html basic instict] [http://buscofamilia.midways.cn/index.html busco familia] [http://brigantesimore.clung.cn/index.html brigante si more] [http://knock-knock.cn/weisse/index.html weisse] [http://knock-knock.cn/w-w-w-melamara/index.html w w w melamara rocks it] [http://wonted.cn/www-lisola/index.html www lisola it] [http://bailalanegra.romanikki.cn/index.html baila la negra] [http://wonted.cn/www-sunia-it/index.html www sunia it] [http://blekandpics.midways.cn/index.html blek and pics] [http://snailtail.cn/xing/index.html xing] [http://bandierrossa.clung.cn/index.html bandier rossa] [http://benedettoseitu.romanikki.cn/index.html benedetto sei tu] [http://snailtail.cn/www-infocamere/index.html www infocamere it] [http://quoits.cn/zstar.html zstar] [http://britanyspearsnuda.romanikki.cn/index.html britany spears nuda] [http://knock-knock.cn/www-unt-se/index.html www unt se] [http://quoits.cn/aspetta-primavera.html aspetta primavera bandini] [http://bronomartino.midways.cn/index.html brono martino] [http://snailtail.cn/www-sussidiario/index.html www sussidiario it] [http://snailtail.cn/yo-yo/index.html yo yo] [http://bellajovanotti.clung.cn/index.html bella jovanotti] [http://kitmun.cn/youtouchmytralala.html youtouchmytralala] [http://kitmun.cn/yammat.html yammat] [http://braccialetti.kittiss.cn/index.html braccialetti] [http://quoits.cn/agente-federale.html agente federale x 3] [http://barbaradurso.romanikki.cn/index.html barbara durso] [http://wonted.cn/wverytime/index.html wverytime] [http://wonted.cn/www-zzchat/index.html www zzchat com] [http://snailtail.cn/www-fotos/index.html www fotos de culos orq] [http://knock-knock.cn/www-chistes/index.html www chistes] [http://balaomagico.romanikki.cn/index.html balao magico] [http://britneyspearstutto.midways.cn/index.html britney spears tutto] [http://quoits.cn/amore-gitano.html amore gitano] [http://beetlejuice.fast-road.cn/index.html beetlejuice] [http://briankrausnudo.clung.cn/index.html brian kraus nudo] [http://bobbisueluther.kittiss.cn/index.html bobbi sue luther] [http://knock-knock.cn/www-itullians/index.html www itullians com] [http://bibliaassociazionediculturabiblica.clung.cn/index.html biblia associazione di cultura biblica] [http://quoits.cn/zavidovici.html zavidovici] [http://bonjovi.clung.cn/index.html bon jovi] [http://quoits.cn/zozzone.html zozzone] [http://bigliettisupercoppa.clung.cn/index.html biglietti supercoppa] [http://basicinstinct2.kittiss.cn/index.html basic instinct 2] [http://knock-knock.cn/wolsvagen/index.html wolsvagen] [http://knock-knock.cn/www-taxus/index.html www taxus it] [http://snailtail.cn/xcam-dvb/index.html xcam dvb] [http://snailtail.cn/www-gesu/index.html www gesu liberatore it] [http://wonted.cn/www-soluzioniecostruzioni/index.html www soluzioniecostruzioni com] [http://knock-knock.cn/wwwloredanabontempi/index.html wwwloredanabontempi it] [http://snailtail.cn/ylli-trokites/index.html ylli trokites] [http://quoits.cn/zeze-di-camargo.html zeze di camargo midi] [http://belleinperizoma.clung.cn/index.html belle in perizoma] [http://snailtail.cn/www-ss-ortana/index.html www ss ortana calcio it] [http://brigittakerkova.midways.cn/index.html brigitta kerkova] [http://bacididonna.clung.cn/index.html bacididonna] [http://bouledifrutta.kittiss.cn/index.html boule di frutta] [http://snailtail.cn/xvid/index.html xvid] [http://quoits.cn/zostawcie.html zostawcie] [http://wonted.cn/ww-alitalia/index.html ww alitalia it] [http://burregionecalabria.clung.cn/index.html bur regione calabria] [http://kitmun.cn/zanussiprofessional.html zanussiprofessional it] [http://kitmun.cn/yimou.html yimou] [http://knock-knock.cn/www-scozia-com/index.html www scozia com] [http://benedetticom.romanikki.cn/index.html benedetti com] [http://snailtail.cn/yaga-mackie/index.html yaga mackie ranks clase aparte] [http://quoits.cn/zigano.html zigano] [http://ballaballaballerino.clung.cn/index.html balla balla ballerino] [http://knock-knock.cn/www-ostaggi/index.html www ostaggi usa com] [http://kitmun.cn/zain.html zain] [http://wonted.cn/www-thefamilyitalia/index.html www thefamilyitalia it] [http://bandodiconcorsocorpoforestale.clung.cn/index.html bando di concorso corpo forestale] [http://bluegulity.kittiss.cn/index.html blue gulity] [http://kitmun.cn/zuccherina.html zuccherina] [http://wonted.cn/www-happidea/index.html www happidea it] [http://knock-knock.cn/www-travis-it/index.html www travis it] [http://boysdziewczonka.fast-road.cn/index.html boys dziewczonka] [http://knock-knock.cn/www-tittypalace/index.html www tittypalace it] [http://snailtail.cn/www-sexape-com/index.html www sexape com] [http://barrywhitetestigratis.kittiss.cn/index.html barry white testi gratis] [http://blackeiedpeace.romanikki.cn/index.html black eied peace] [http://snailtail.cn/www-sulpm/index.html www sulpm it] [http://knock-knock.cn/www-radio101/index.html www radio101 it] [http://brikbraknapoli.midways.cn/index.html brik brak napoli] [http://benedettapassione.midways.cn/index.html benedetta passione] [http://snailtail.cn/www-infostrada/index.html www infostrada spazio zero it] [http://kitmun.cn/yeha.html yeha] [http://bacherboy2.kittiss.cn/index.html bacher boy 2] [http://kitmun.cn/yooom-wara.html yooom wara yooom] [http://snailtail.cn/xin-tai-ruan/index.html xin tai ruan] [http://kitmun.cn/yannick.html yannick] [http://snailtail.cn/xpie/index.html xpie] [http://boxautolamiera.clung.cn/index.html box auto lamiera] [http://borsalavoro.midways.cn/index.html borsalavoro] [http://bilderirak.romanikki.cn/index.html bilder irak] [http://snailtail.cn/www-torremolinos/index.html www torremolinos com] [http://brunodelturco.midways.cn/index.html bruno del turco] [http://kitmun.cn/zaza.html zaza] [http://briggettabui.fast-road.cn/index.html briggetta bui] [http://bonnietayler.clung.cn/index.html bonnie tayler] [http://boiardovitaeopere.kittiss.cn/index.html boiardo vita e opere] [http://quoits.cn/allarme-a.html allarme a gibilterra] [http://wonted.cn/www-fantasmas/index.html www fantasmas co] [http://bacisaffici.fast-road.cn/index.html bacisaffici] [http://quoits.cn/a-pugni-nudi.html a pugni nudi] [http://kitmun.cn/yoli-babasonicos.html yoli babasonicos] [http://knock-knock.cn/www-vicenza/index.html www vicenza com] [http://snailtail.cn/www-raitre/index.html www raitre rai it] [http://snailtail.cn/y-ahoo-com/index.html y ahoo com] [http://wonted.cn/www-uff-com-mx/index.html www uff com mx] [http://wonted.cn/www-mans/index.html www mans com] [http://quoits.cn/attenzione-alla.html attenzione alla puttana santa] [http://knock-knock.cn/www-3sudest/index.html www 3sudest ro] [http://knock-knock.cn/www-evatremila/index.html www evatremila] [http://bullismo.midways.cn/index.html bullismo] [http://quoits.cn/angeli-del.html angeli del peccato] [http://bobdylanknockonheavensdoor.fast-road.cn/index.html bob dylan knock on heavens door] [http://bailedelpescao.clung.cn/index.html baile del pescao] [http://quoits.cn/zoo-105.html zoo 105] {{task}}
{{task}}
Compute the sum of the elements of an Array
Compute the sum of the elements of an Array


Line 11: Line 11:
$var:=0
$var:=0
For ($i;1;Size of array($list))
For ($i;1;Size of array($list))
$var:=$var+$list{$i}
$var:=$var $list{$i}
End for
End for


Line 20: Line 20:
Sum : Integer := 0;
Sum : Integer := 0;
for I in Int_Array'range loop
for I in Int_Array'range loop
Sum := Sum + Int_Array(I);
Sum := Sum Int_Array(I);
end loop;
end loop;


Line 30: Line 30:
repeat with i in array
repeat with i in array
-- very important -- list index starts at 1 not 0
-- very important -- list index starts at 1 not 0
set product to product + i
set product to product i
end repeat
end repeat


Line 42: Line 42:
50 REM Find the sum of elements in the array
50 REM Find the sum of elements in the array
60 SUM = 0
60 SUM = 0
70 FOR I = 1 TO 5: SUM = SUM + ARRAY(I): NEXT I
70 FOR I = 1 TO 5: SUM = SUM ARRAY(I): NEXT I
80 PRINT "The sum is ";SUM
80 PRINT "The sum is ";SUM


==[[C plus plus|C++]]==
==[[C plus plus|C ]]==
[[Category:C plus plus]]
[[Category:C plus plus]]


Line 55: Line 55:
int list[] = { 1, 2, 3 } ;
int list[] = { 1, 2, 3 } ;
int sum = 0 ;
int sum = 0 ;
for( int i = 0 ; i < 3 ; i++ )
for( int i = 0 ; i < 3 ; i )
{
{
sum += list[i];
sum = list[i];
}
}
}
}
Line 69: Line 69:
{
{
int list[] = { 1, 2, 3 } ;
int list[] = { 1, 2, 3 } ;
std::accumulate(list, list + 3, 0);
std::accumulate(list, list 3, 0);
return 0;
return 0;
}
}
Line 78: Line 78:
{
{
T accum = 0;
T accum = 0;
for (unsigned i=0; i<n; i++)
for (unsigned i=0; i<n; i )
accum += array[i];
accum = array[i];
return accum;
return accum;
}
}
Line 103: Line 103:
int arg_length = arg.Length;
int arg_length = arg.Length;


for( int i = 0; i < arg_length; i++ )
for( int i = 0; i < arg_length; i )
value += arg[i];
value = arg[i];




Line 111: Line 111:
int sum = 0;
int sum = 0;
int[] arg = { 1, 2, 3, 4, 5 };
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) sum += value;
foreach (int value in arg) sum = value;


==[[Clean]]==
==[[Clean]]==
Line 128: Line 128:


(defparameter *data* #(1 2 3 4 5))
(defparameter *data* #(1 2 3 4 5))
(reduce #'+ *data*)
(reduce #' *data*)


==[[Delphi]]==
==[[Delphi]]==
Line 146: Line 146:


pragma.enable("accumulator")
pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _ + x }
accum 0 for x in [1,2,3,4,5] { _ x }


==[[Erlang]]==
==[[Erlang]]==
Line 157: Line 157:


% and compute its sum:
% and compute its sum:

2000
S = lists:sum(L).
S = lists:sum(L).


Line 168: Line 170:
0;
0;
sum_rec([Head|Tail]) ->
sum_rec([Head|Tail]) ->
Head + sum_rec(Tail).
Head sum_rec(Tail).


% tail-recursive definition:
% tail-recursive definition:
Line 176: Line 178:
Acc;
Acc;
sum_tail([Head|Tail], Acc) ->
sum_tail([Head|Tail], Acc) ->
sum_tail(Tail, Head + Acc).
sum_tail(Tail, Head Acc).


==[[Forth]]==
==[[Forth]]==
Line 183: Line 185:
: sum ( addr cnt -- n )
: sum ( addr cnt -- n )
0 -rot
0 -rot
cells bounds do i @ + cell +loop ;
cells bounds do i @ cell loop ;




Line 193: Line 195:
dim sum as integer = 0
dim sum as integer = 0
for index as integer = lbound(array) to ubound(array)
for index as integer = lbound(array) to ubound(array)
sum += array(index)
sum = array(index)
next
next


Line 201: Line 203:
let values = [1..10]
let values = [1..10]
sum values -- the easy way
sum values -- the easy way
foldl (+) 0 values -- the hard way
foldl ( ) 0 values -- the hard way


==[[IDL]]==
==[[IDL]]==
Line 214: Line 216:
int[] arg = new int[] { 1,2,3,4,5 };
int[] arg = new int[] { 1,2,3,4,5 };
for (int i: arg)
for (int i: arg)
value += i;
value = i;


==[[JavaScript]]==
==[[JavaScript]]==
Line 222: Line 224:
var sum = 0;
var sum = 0;
for(var i in array)
for(var i in array)
sum += array[i];
sum = array[i];


==[[Perl]]==
==[[Perl]]==
Line 230: Line 232:
my $var;
my $var;
my @list = (1, 2, 3);
my @list = (1, 2, 3);
$var += $_ for (@list);
$var = $_ for (@list);


Alternate
Alternate
Line 245: Line 247:
my $acc = 0;
my $acc = 0;
my @list = qw(1 2 3)
my @list = qw(1 2 3)
map { $acc += $_ } @list
map { $acc = $_ } @list


==[[PHP]]==
==[[PHP]]==
Line 260: Line 262:
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
for i from 1 to length(ar) do
for i from 1 to length(ar) do
ar(i) + sum -> sum;
ar(i) sum -> sum;
endfor;
endfor;


Line 266: Line 268:


lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
appdata(ar, procedure(x); x + sum -> sum; endprocedure);
appdata(ar, procedure(x); x sum -> sum; endprocedure);


==[[Prolog]]==
==[[Prolog]]==
[[Category:Prolog]]
[[Category:Prolog]]
sum([],0).
sum([],0).
sum([H|T],X) :- sum(T,Y), X is H + Y.
sum([H|T],X) :- sum(T,Y), X is H Y.
test
test
:- sum([1,2,3,4,5,6,7,8,9],X).
:- sum([1,2,3,4,5,6,7,8,9],X).
Line 286: Line 288:


ary = [1,2,3,4,5]
ary = [1,2,3,4,5]
sum = ary.inject{|currentSum,element|currentSum+element}
sum = ary.inject{|currentSum,element|currentSum element}
# => 15
# => 15


Line 292: Line 294:
[[Category:Scala]]
[[Category:Scala]]
val array = Array(1,2,3,4,5)
val array = Array(1,2,3,4,5)
val sum = array.foldLeft(0)(_ + _)
val sum = array.foldLeft(0)(_ _)


This is a shortcut for
This is a shortcut for
val sum = array.foldLeft(0){(currentSum, element) => currentSum + element}
val sum = array.foldLeft(0){(currentSum, element) => currentSum element}


==[[Seed7]]==
==[[Seed7]]==
Line 307: Line 309:
begin
begin
for value range valueArray do
for value range valueArray do
sum +:= value;
sum := value;
end for;
end for;
end func;
end func;
Line 319: Line 321:


val array = [1,2,3,4,5];
val array = [1,2,3,4,5];
foldl op+ 0 array;
foldl op 0 array;


==[[Tcl]]==
==[[Tcl]]==
Line 326: Line 328:
Assuming the values are in a list named <tt>listname</tt>:
Assuming the values are in a list named <tt>listname</tt>:


set result [expr [join $listname +]]
set result [expr [join $listname ]]


==[[Toka]]==
==[[Toka]]==
Line 332: Line 334:


[ ( array size -- sum )
[ ( array size -- sum )
>r 0 r> [ over i swap get-element + ] iterate nip ] is sum-array
>r 0 r> [ over i swap get-element ] iterate nip ] is sum-array


==[[UNIX Shell]]==
==[[UNIX Shell]]==
Line 344: Line 346:
list="1 2 3"
list="1 2 3"
for n in $list
for n in $list
do sum="$(($sum + $n))"
do sum="$(($sum $n))"
done
done
echo $sum
echo $sum
Line 352: Line 354:
sum=0
sum=0
for n
for n
do sum="$(($sum + $n))"
do sum="$(($sum $n))"
done
done
echo $sum
echo $sum
Line 360: Line 362:
sum=0
sum=0
while read n
while read n
do sum="$(($sum + $n))"
do sum="$(($sum $n))"
done
done
echo $sum
echo $sum
Line 371: Line 373:
SUM=0;
SUM=0;
for i in $LIST; do
for i in $LIST; do
SUM=$[$SUM + $i];
SUM=$[$SUM $i];
done;
done;
echo $SUM
echo $SUM

Revision as of 11:23, 10 September 2007

zika cvetkovic www win it ricerca v videos de hector y bordeis bordei www webmail uiowa edu bersuitvergarabat com ar bigfun angi vera www piss www ultra blue tv com bandida biografia mercedes ambrus you re crazy wilehalm eschenbach you and me zita yutaka ozaki brel wyhylylybymy banda limon www diddle barzellette ufficio yulia mayarchuck nuda zoccolone www ausoniatools com berlino esto bumer klingelton al piccolo margherita www basketball it barbiere della sera yates britney foto www lunapop com basic instict busco familia brigante si more weisse w w w melamara rocks it www lisola it baila la negra www sunia it blek and pics xing bandier rossa benedetto sei tu www infocamere it zstar britany spears nuda www unt se aspetta primavera bandini brono martino www sussidiario it yo yo bella jovanotti youtouchmytralala yammat braccialetti agente federale x 3 barbara durso wverytime www zzchat com www fotos de culos orq www chistes balao magico britney spears tutto amore gitano beetlejuice brian kraus nudo bobbi sue luther www itullians com biblia associazione di cultura biblica zavidovici bon jovi zozzone biglietti supercoppa basic instinct 2 wolsvagen www taxus it xcam dvb www gesu liberatore it www soluzioniecostruzioni com wwwloredanabontempi it ylli trokites zeze di camargo midi belle in perizoma www ss ortana calcio it brigitta kerkova bacididonna boule di frutta xvid zostawcie ww alitalia it bur regione calabria zanussiprofessional it yimou www scozia com benedetti com yaga mackie ranks clase aparte zigano balla balla ballerino www ostaggi usa com zain www thefamilyitalia it bando di concorso corpo forestale blue gulity zuccherina www happidea it www travis it boys dziewczonka www tittypalace it www sexape com barry white testi gratis black eied peace www sulpm it www radio101 it brik brak napoli benedetta passione www infostrada spazio zero it yeha bacher boy 2 yooom wara yooom xin tai ruan yannick xpie box auto lamiera borsalavoro bilder irak www torremolinos com bruno del turco zaza briggetta bui bonnie tayler boiardo vita e opere allarme a gibilterra www fantasmas co bacisaffici a pugni nudi yoli babasonicos www vicenza com www raitre rai it y ahoo com www uff com mx www mans com attenzione alla puttana santa www 3sudest ro www evatremila bullismo angeli del peccato bob dylan knock on heavens door baile del pescao zoo 105

Task
Sum of Array
You are encouraged to solve this task according to the task description, using any language you may know.

Compute the sum of the elements of an Array

4D

ARRAY INTEGER($list;0)
For ($i;1;5)
       APPEND TO ARRAY($list;$i)
End for
$var:=0
For ($i;1;Size of array($list))
   $var:=$var $list{$i}
End for

Ada

Int_Array : array(1..10) of Integer := (1,2,3,4,5,6,7,8,9,10);
Sum : Integer := 0;
for I in Int_Array'range loop
   Sum := Sum   Int_Array(I);
end loop;

AppleScript

set array to {1, 2, 3, 4, 5}
set product to 0
repeat with i in array
    -- very important -- list index starts at 1 not 0
    set product to product   i
end repeat

BASIC

 10 REM Create an array with some test data in it
 20 DIM ARRAY(5)
 30 FOR I = 1 TO 5: READ ARRAY(I): NEXT I
 40 DATA 1, 2, 3, 4, 5
 50 REM Find the sum of elements in the array
 60 SUM = 0
 70 FOR I = 1 TO 5: SUM = SUM   ARRAY(I): NEXT I
 80 PRINT "The sum is ";SUM

C

Compiler: gcc 4.0.2

int
main( int argc, char* argv[] )
{
 int list[] = { 1, 2, 3 } ;
 int sum = 0 ;
 for( int i = 0 ; i < 3 ; i   )
 {
  sum  = list[i];
 }
}


Alternate

#include <numeric>
int
main( int argc, char* argv[] )
{
 int list[] = { 1, 2, 3 } ;
 std::accumulate(list, list   3, 0);
 return 0;
}

template alternative

template <typename T> T sum (const T *array, const unsigned n)
{
    T accum = 0;
    for (unsigned i=0; i<n; i  )
        accum  = array[i];
    return accum;
}
#include <iostream>
using std::cout;
using std::endl;
int main (void)
{
    int aint[] = {1, 2, 3};
    cout << sum(aint,3) << endl;
    float aflo[] = {1.1, 2.02, 3.003, 4.0004};
    cout << sum(aflo,4) << endl;
    return 0;
}

C#

 int value = 0;
 int[] arg = { 1,2,3,4,5 };
 int arg_length = arg.Length;
 for( int i = 0; i < arg_length; i   )
    value  = arg[i];


Alternate

 int sum = 0;
 int[] arg = { 1, 2, 3, 4, 5 };
 foreach (int value in arg) sum  = value;

Clean

array = {1, 2, 3, 4, 5}
Start = sum [x \\ x <-: array]

ColdFusion

 <cfset myArray = listToArray("1,2,3,4,5")>
 #arraySum(myArray)#

Common Lisp

(defparameter *data* #(1 2 3 4 5))
(reduce #'  *data*)

Delphi

[[Category:Delphi]

Compiler: All

 var
   Ints   : array[1..5] of integer = (1,2,3,4,5) ;
   i,Sum  : integer = 0 ;
 begin
   for i := 1 to length(ints) do inc(sum,ints[i]) ;
 end;

E

pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _   x }

Erlang

Using the standard libraries:

% create the list:
L = lists:seq(1, 10).
% and compute its sum:

2000

S = lists:sum(L).

Or defining our own versions:

-module(list_sum).
-export([sum_rec/1, sum_tail/1]).
% recursive definition:
sum_rec([]) ->
    0;
sum_rec([Head|Tail]) ->
    Head   sum_rec(Tail).
% tail-recursive definition:
sum_tail(L) ->
    sum_tail(L, 0).
sum_tail([], Acc) ->
    Acc;
sum_tail([Head|Tail], Acc) ->
    sum_tail(Tail, Head   Acc).

Forth

 : sum ( addr cnt -- n )
   0 -rot
   cells bounds do i @   cell  loop ;


FreeBASIC

 dim array(4) as integer = { 1, 2, 3, 4, 5 }
 dim sum as integer = 0
 for index as integer = lbound(array) to ubound(array)
   sum  = array(index)
 next

Haskell

 let values = [1..10]
 sum values          -- the easy way
 foldl ( ) 0 values  -- the hard way

IDL

 result = total(array)

Java

 int value = 0;
 int[] arg = new int[] { 1,2,3,4,5 };
 for (int i: arg)
   value  = i;

JavaScript

var array = [1, 2, 3, 4, 5];
var sum = 0;
for(var i in array)
  sum  = array[i];

Perl

Interpeter: Perl

my $var;
my @list = (1, 2, 3);
$var  = $_ for (@list);

Alternate

Libraries: List::Util

use List::Util 'sum';
my @list = (1, 2, 3);
my $var = sum @list;

Alternate

# TMTOWTDI

my $acc = 0;
my @list = qw(1 2 3)
map { $acc  = $_ } @list

PHP

 $list = array(1,2,3,4,5,6,7,8,9);
 echo array_sum($list);

Pop11

Simple loop:

lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
for i from 1 to length(ar) do
    ar(i)   sum -> sum;
endfor;

One can alternativly use second order iterator:

lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
appdata(ar, procedure(x); x   sum -> sum; endprocedure);

Prolog

sum([],0).
sum([H|T],X) :- sum(T,Y), X is H   Y.

test

:- sum([1,2,3,4,5,6,7,8,9],X).
X =45;

Python

Interpeter: Python 2.5

 total = sum([1, 2, 3, 4, 5, 6, 7, 8, 9])

Ruby

 ary = [1,2,3,4,5]
 sum = ary.inject{|currentSum,element|currentSum element}
 # => 15

Scala

   val array = Array(1,2,3,4,5)
   val sum = array.foldLeft(0)(_   _)

This is a shortcut for

 val sum = array.foldLeft(0){(currentSum, element) => currentSum   element}

Seed7

const func integer: sumArray (in array integer: valueArray) is func
  result
    var integer: sum is 0;
  local
    var integer: value is 0;
  begin
    for value range valueArray do
      sum  := value;
    end for;
  end func;

Call this function with:

writeln(sumArray([](1, 2, 3, 4, 5)));

Standard ML

 val array = [1,2,3,4,5];
 foldl op  0 array;

Tcl

Assuming the values are in a list named listname:

 set result [expr [join $listname  ]]

Toka

[ ( array size -- sum )
  >r 0 r> [ over i swap get-element   ] iterate nip ] is sum-array

UNIX Shell

Interpreter: NetBSD 3.0's ash

From an internal variable, $IFS delimited:

 sum=0
 list="1 2 3"
 for n in $list
 do sum="$(($sum   $n))"
 done
 echo $sum

From the argument list (ARGV):

 sum=0
 for n
 do sum="$(($sum   $n))"
 done
 echo $sum

From STDIN, one integer per line:

 sum=0
 while read n
 do sum="$(($sum   $n))"
 done
 echo $sum

Interpreter: GNU bash, version 3.2.0(1)-release (i386-unknown-freebsd6.1)

From variable:

 LIST='20 20 2';
 SUM=0;
 for i in $LIST; do
   SUM=$[$SUM   $i];
 done;
 echo $SUM