reducing lists   (skip ahead to 3.14)

"reducing a list by +" means adding up  the elements of the list


reduce(+, 0, [1,2,3,4])

==>  1+2+3+4

==>  10


0 is called the unit ; it is the value when the list is empty

reduce(+,0,[]) ==> 0

 

reduce(*,1,[3,7,5]) ==> 105

         

                     unit for *

recall append  creates a new list by following the elements of the first list by those of

append([4,5,7],[6,4,1]) ==> [4,5,7,6,4,1]

 

 

How to append together a list of lists?

[[1,2], [3,4,5], [6,7]]

==> [1,2,3,4,5,6,7]

Use reduce.  What is the unit for append ?

[  ]

reduce(append, [], LoL)

list of lists

 

mappend(f, range(1,N)) ==>

reduce(append, [], [f(1),f(2),º,f(N)])

            

result of map(f, range(1,N))

We want

f(1) = [1]

f(2) = [1,2]

f(3) = [1,2,3]

      :

      :

f(N) = [1,2,3,º,N]

f(i) = range(1,i)

 

mappend combines map & append

 mappend(f,L)

 = reduce(append, [], map(f,L))

 

Example  (synthesis)

Make a list

 

Overall Solution

map would have given

[[1], [1,2], [1,2,3], º [1,2,3,ºN]]

 

To Next Slide To Previous Slide To Contents