2009-04-19 19 views
1

このデータ型のFoldableインスタンスはどのように見えますか?一見単純なFoldableインスタンスの定義

data X t = X t [X t] 

私はこの試みた:

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

をしかし、このエラーを得た:

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

答えて

6

xsは、個々のアイテムに適用される項目とfoldMapニーズのリスト、ではないリストであります自体。 mapでこれを行うと、mconcatと組み合わせることができる結果のリストが得られます。

instance Foldable X where 
    foldMap f (X x xs) = f x `mappend` mconcat (map (foldMap f) xs) 
関連する問題