gpt4 book ai didi

haskell - 用一个简单的例子测试 Haskell 可遍历

转载 作者:行者123 更新时间:2023-12-04 22:46:55 28 4
gpt4 key购买 nike

我正在尝试使用 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。我是否走在正确的轨道上,应该如何实现可遍历的实例化?

最佳答案

我只会使用语言扩展来为我派生它:

{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
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 } deriving (Functor, Tr.Traversable, Fl.Foldable)

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)

main = do
putStrLn $ show exampleData
Tr.traverse (putStrLn . show) exampleData
> runhaskell test_traversable.hs
Foo: 1
Foo: 1
Bar: 2
()

如果你想知道编译器是如何自动实现它的,你可以用 -ddump-deriv 编译它。标志(清理了一下):
instance Functor Data where
fmap f (Data foo' bar') = Data (f foo') (f bar')

instance Tr.Traversable Data where
tr.traverse f (Data foo' bar') = Data <$> (f foo') <*> (f bar')

instance Fl.Foldable Data where
Fl.foldr f z (Data foo' bar') = f foo' (f bar' z)

关于haskell - 用一个简单的例子测试 Haskell 可遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342241/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com