我已经定义了一个表示树的新数据类型。我还实现了一个walk
遍历树的所有元素的函数,该函数的功能版本是正确的,但不是他的一元版本walkM
。
module Hdot where
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
data RDoll a = Null | RDoll a [RDoll a] deriving (Show)
test :: RDoll Int
test = RDoll 1 [RDoll 2 [Null], RDoll 3 [RDoll 4 [Null]]]
walk :: (a -> b) -> RDoll a -> [b]
walk f Null = []
walk f (RDoll x rds) = ((f x): (concatMap (\x -> walk f x) rds))
walkM :: (Monad m) => (a -> m b) -> RDoll a -> m [b]
walkM f Null = return []
walkM f (RDoll rd rdss) = do
x <- f rd
xs <- concatMap (walkM f) rdss
return (x:xs)
有类型错误
Couldn't match type `b' with `[b]'
...
有人能帮帮我吗!
感谢您的回复。