Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.



Task

Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.



See also

 Wikipedia article:   Fold
 Wikipedia article:   Catamorphism


for i = 1 to 10 :n(i) = i:next i
 
print "  +: ";" ";cat(10,"+")
print "  -: ";" ";cat(10,"-")
print "  *: ";" ";cat(10,"*")
print "  /: ";" ";cat(10,"/")
print "  ^: ";" ";cat(10,"^")
print "min: ";" ";cat(10,"min")
print "max: ";" ";cat(10,"max")
print "avg: ";" ";cat(10,"avg")
print "cat: ";" ";cat(10,"cat")
 
function cat(count,op$)
cat = n(1)
for i = 2 to count 
 if op$ = "+" 	then cat = cat + n(i)
 if op$ = "-" 	then cat = cat - n(i)
 if op$ = "*" 	then cat = cat * n(i) 
 if op$ = "/" 	then cat = cat / n(i)
 if op$ = "^" 	then cat = cat ^ n(i)
 if op$ = "max"	then cat = max(cat,n(i))
 if op$ = "min"	then cat = min(cat,n(i))
 if op$ = "avg"	then cat = cat + n(i)
 if op$ = "cat"	then cat$ = cat$ + str$(n(i))
next i
if op$ = "avg" then cat = cat / count
if op$ = "cat" then cat = val(str$(n(1))+cat$)
end function