I’ve defined a new data type representing a tree. I’ve also implemented a function walk to walk over all elements of the tree, the functional version of the function is correct but not his monadic version 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)
There is a type error
Couldn't match type `b' with `[b]'
...
Can somebody help me !
thanks for any reply.
Generally, you should give the full error message, since it has valuable context:
So there’s some confusion between lists of values
[b]and actionsm b.The suspicious code is your use of concatMap to run
walkMrecursively. I think you mean to useconcatMapM(e.g.mapMandconcat):As a note on style, I’d try to write things a bit differently. Take a look at the rose tree in the base library. In particular, don’t define
walkandwalkM, define instances for Functor, Monad and reuse existing library functions.