Reduction of List

reduceLeft and foldLeft

  • reduceLeft insert a given operator between adjacent elements of a list

List(x1, …, xn) reduceLeft op = x1 op x2 op … xn


  • foldLeft is like reduceLeft but takes accumulator, z as an addition parameter and is returned when foldLeft is call on an empty list

(List(x1, …, xn) foldLeft z)(op) = z op x1 op x2 … xn


  • Implementation of foldLeft and reduceLeft

foldRight and reduceRight

List(x1, …, x{n-1}, xn) reduceRight op = x1 op ( … (x{n-1} op xn) … ) 

(List(x1, …, xn) foldRight acc)(op) = x1 op ( … (xn op acc) … )



You May Also Like

About the Author: Phuong Ta Thi Thao

Leave a Reply

Notify of