Question

Qu'est-ce que l'instance pliable pour ce type de données ressembler?

data X t = X t [X t]

J'ai essayé ceci:

instance Foldable X where
    foldMap f (X x xs) = f x `mappend` foldMap f xs

Mais eu cette erreur:

Occurs check: cannot construct the infinite type: a = X a
When generalising the type(s) for `foldMap'
In the instance declaration for `Foldable X'
Était-ce utile?

La solution

xs est une liste d'articles et foldMap doit être appliqué aux articles individuels et non la liste elle-même. Faire cela avec map donne une liste des résultats qui peuvent être combinés avec mconcat:

instance Foldable X where
  foldMap f (X x xs) = f x `mappend` mconcat (map (foldMap f) xs)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top