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


get "libhdr"
 
let reduce(f, v, len, seed) = 
    len = 0 -> seed,
    reduce(f, v+1, len-1, f(!v, seed))
 
let start() be
$(  let add(x, y) = x+y
    let mul(x, y) = x*y
 
    let nums = table 1,2,3,4,5,6,7
 
    writef("%N*N", reduce(add, nums, 7, 0))
    writef("%N*N", reduce(mul, nums, 7, 1))
$)