Data.Foldable
class Foldable t where
...
foldr :: (a -> b -> b) -> b -> t a -> b
... - Für Datentypen mit zwei Konstruktoren
Beispiel Binärbaum
data Tree a = Leaf a | Node (Tree a) (Tree a)
instance Foldable Tree where
foldr f v (Leaf x) = f x v
foldr f v (Node l r) = foldr f (foldr f v r) l
> foldr (*) 1 (Node (Node (Leaf 2) (Leaf 4)) (Leaf 6))
48