Collecting values from Functors?

Ralf Hinze ralf@informatik.uni-bonn.de
Thu, 5 Jun 2003 07:43:55 +0200


> Rather than using fmap, why not use gmap in GHC.
> Using an appropriate generic traversal scheme,
> say listify, all the code you write is the
> following expression:
>
> listify (const True) mytree :: [Int]
>
> if you are looking for all the Ints in
> mytree = Fork (Leaf 42) (Fork (Leaf 88) (Leaf 37))
> So this would give you [42,88,37].

What happens if the tree contains additional integers to record,
say, balancing information. The information a functor provides
is vital here. Take as the simplest example

  newtype Weighted a = WithWeight (Int, a)

Without the functor definition there is no way to distinguish the
two Ints in Weighted Int.

Cheers, Ralf