我有以下玩具哈希克尔代码为二叉搜索树。函数preorderV是用遍历每个元素的函数遍历树的。它对正常功能正常工作。但是,如果我应用函数“打印”,我得到编译错误。如何使用preorderV进行与IO工作相关的功能?在Haskell树上遍历打印
感谢
代码:
data BSTree a = EmptyTree | Node a (BSTree a) (BSTree a) deriving (Show)
data Mode = IN | POST | PRE
singleNode :: a -> BSTree a
singleNode x = Node x EmptyTree EmptyTree
bstInsert :: (Ord a) => a -> BSTree a -> BSTree a
bstInsert x EmptyTree = singleNode x
bstInsert x (Node a left right)
| x == a = Node a left right
| x < a = Node a (bstInsert x left) right
| x > a = Node a left (bstInsert x right)
buildTree :: String -> BSTree String
buildTree = foldr bstInsert EmptyTree . words
preorderV :: (a->b) -> BSTree a -> BSTree b
preorderV f EmptyTree = EmptyTree
preorderV f (Node x left right) = Node (f x) (preorderV f left) (preorderV f right)
错误:
Couldn't match type ‘BSTree’ with ‘IO’
Expected type: IO (IO())
Actual type: BSTree (IO())
In a stmt of a 'do' block: preorderV print $ buildTree content
你能张贴包含'print'的代码?我很好奇它为什么期望类型'IO(IO())'而不是'IO()'(它会给出一个错误,但我不希望在这里看到IO(IO())' )。我怀疑这是代码的另一个可能独立的问题。 – 2014-10-06 04:28:27
打印是前奏的功能 – 2014-10-07 01:10:16
我知道,我的意思是你没有在你的文章中围绕这行代码的代码:'preorderV print $ buildTree content' – 2014-10-07 01:59:10