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


include ..\Utilitys.pmt
 
def add + enddef
def sub - enddef 
def mul * enddef
 
def reduce >ps
    1 get
    swap len 2 swap 2 tolist for
        get rot swap tps exec swap
    endfor
    ps> drop
    swap
enddef
 
 
( 1 2 3 4 5 )
getid add reduce ?
getid sub reduce ?
getid mul reduce ?