2009-04-19 7 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) 
관련 문제