小编use*_*154的帖子

树作为仿函数和可折叠的实例

我正在为一个hw问题编写一些代码,它要求我们将树的定义作为functor和foldable的一个实例.当我写下面的代码时:

 import Data.Foldable
 import Data.Monoid

 data Tree a = Leaf a
              | Node [Tree a] 
     deriving (Show) 

 instance Functor (Tree) where
    fmap f (Leaf a) = Leaf (f a) 
    fmap f (Node [Tree a]) = fmap f [Tree a]

 instance Foldable (Tree) where
    foldMap f (Leaf a) = f a
    foldMap f (Node [Tree a]) = foldMap f `mappend` [Tree a]
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

hw.hs:10:19:
Not in scope: data constructor `Tree'
Perhaps you meant `True' (imported from Prelude)

hw.hs:10:38:
Not in scope: …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming functor data-structures

8
推荐指数
1
解决办法
1687
查看次数