【发布时间】:2014-02-16 00:17:17
【问题描述】:
我正在尝试使用 Data.Traversable 遍历 haskell 中数据结构的所有成员,该数据记录在以下网址中:
http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-Traversable.html http://www.haskell.org/haskellwiki/Foldable_and_Traversable
到目前为止,我已经提出了以下代码,据我所知,它缺少 Tr.Traversable 实例的正确实现。
import qualified Data.Traversable as Tr
import qualified Data.Foldable as Fl
import Control.Monad
import Control.Applicative
data Test = Test { desc :: String
, value :: Int
}
data Data t = Data { foo :: t
, bar :: t
}
exampleData = Data { foo = Test "Foo" 1
, bar = Test "Bar" 2
}
instance Show Test where
show f = (desc f) ++ ": " ++ (show $ value f)
instance (Show a) => Show (Data a) where
show f = show (foo f)
instance Functor Data where
fmap = Tr.fmapDefault
instance Fl.Foldable Data where
foldMap = Tr.foldMapDefault
instance Tr.Traversable Data where
traverse f = Data f -- Try to show a Test entry inside the Data structure
--
-- traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
--
main = do
putStrLn $ show exampleData
Tr.traverse (putStrLn show) exampleData
我正在尝试打印 exampleData 中的所有项目,逐个成员并使用 show。我是否走在正确的轨道上?应该如何实现可遍历的实例化?
【问题讨论】:
标签: haskell functor traversable