Slide 19
Slide 19 text
Monoids: Simple "Natural" Examples
Listing 15: Haskell
1 -- OP ID List
2 foldr (+) 0 [1,2,3,4] -- 10
3 foldr (*) 1 [1,2,3,4] -- 24
4 foldr (++) [] [[1], [1,2]] -- [1,1,2]
5 foldr (&&) True [True, False, True] -- False
6 foldr (||) False [True, False, True] -- True
7 mconcat = foldr mappend mempty
Listing 16: Same in Scala: WTF?
1 List(1,2,3,4).foldRight(0)(_+_) // 10
2 List(1,2,3,4).foldRight(1)(_*_) // 24
3 List(List(1),List(1,2)).foldRight(List[Int]())(_++_)
4 List(true,false,true).foldRight(true)(_&&_)
5 List(true,false,true).foldRight(false)(_||_)