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


( ( fold
  =   f xs init first rest
    .   !arg:(?f.?xs.?init)
      & ( !xs:&!init
        |   !xs:%?first ?rest
          & !f$(!first.fold$(!f.!rest.!init))
        )
  )
&   out
  $ ( fold
    $ ( (=a b.!arg:(?a.?b)&!a+!b)
      . 1 2 3 4 5
      . 0
      )
    )
& (product=a b.!arg:(?a.?b)&!a*!b)
& out$(fold$(product.1 2 3 4 5.1))
);