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


with javascript_semantics
function add(integer a, b)  return a + b  end function
function sub(integer a, b)  return a - b  end function
function mul(integer a, b)  return a * b  end function
 
function reduce(integer rid, sequence s)
    object res = s[1]
    for i=2 to length(s) do
        res = rid(res,s[i])
    end for
    return res      
end function
 
?reduce(add,tagset(5))
?reduce(sub,tagset(5))
?reduce(mul,tagset(5))
