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


 
      DIM a(4)
      a() = 1, 2, 3, 4, 5
      PRINT FNreduce(a(), "+")
      PRINT FNreduce(a(), "-")
      PRINT FNreduce(a(), "*")
      END
 
      DEF FNreduce(arr(), op$)
      REM!Keep tmp, arr()
      LOCAL I%, tmp
      tmp = arr(0)
      FOR I% = 1 TO DIM(arr(), 1)
        tmp = EVAL("tmp " + op$ + " arr(I%)")
      NEXT
      = tmp
 