Skip to content

concat

First, if we have a container of lists, of type t [a], then we can take the lists in this container and concatenate them to produce a single list:

GHCi
>>> concat [[1], [2,3], [4,5]]
[1,2,3,4,5]

or for a tree of lists:

GHCi
>>> tree = Node Empty 5 (Node (Node Empty 3 Empty) 1 (Node Empty 2 Empty))
>>> concat $ fmap singleton tree
[5,3,1,2]

The implementation of concat is simply the specialization of fold to the list monoid. fold works for arbitrary monoids:1

concat :: Foldable t => t [a] -> [a]
concat = fold

  1. Interestingly, even though it doesn't get any simpler than this, this is not the implementation provided in Data.Foldable, even though it has the same effect.