我需要做出一個可摺疊實例玫瑰樹數據結構:哈斯克爾幺折一朵
data Rose a = a :> [Rose a]
deriving (Eq, Show)
用下面的獨異與玫瑰有關的類/實例:
instance Functor Rose where
fmap f (a :> bs) = (f a) :> (map (fmap f) bs)
class Monoid a where
mempty :: a
(<>) :: a -> a -> a
instance Monoid [a] where
mempty = []
(<>) = (++)
我嘗試:
instance Foldable Rose where
fold (a:>b) = a <> (foldMap fold b)
但是,這不能正常工作,系統檢查我得到的錯誤:
*** Failed! Exception: 'Prelude.undefined':
[] :> []
但我不知道爲什麼它不起作用,有人能幫我嗎?
在此先感謝!
最好的問候, Skyfe。
而不是更新您的問題的解決方案,爲什麼不把它寫在這裏作爲答案? – Sibi 2014-10-08 13:21:02
好的,沒有想到這種可能性! – user2999349 2014-10-08 15:32:12
如何用'{--LANGUAGE DeriveFoldable - }'派生(可摺疊)'? – viorior 2014-10-08 16:19:14