我在一个名为BinaryTree的Haskell模块中定义了一个名为findPaths
的函数,我试图在创建的主模块中调用该函数。该类型的函数调用的是在主模块中调用Haskell函数时遇到问题
findPaths :: Tree -> [Path]
哪里Tree
是被定义为一种数据类型:
data Tree = Leaf | Node Tree Tree
和Path
被定义为:
data Path = LeftTurn Path | RightTurn Path | This
在主函数中,我这样做,只有这样:
module Main where
import BinaryTree
findPaths (Node Leaf Leaf)
但是,当我尝试用下面的命令编译:如果我尝试导出的数据类型在
Couldn't match expected type `Language.Haskell.TH.Syntax.Q [Language.Haskell.TH.Syntax.Dec]' against inferred type `[Path]' In the expression: findPaths (Node Leaf Leaf)
我得到了同样的错误:
ghc -o --make Main Main.hs BinaryTree.hs
我得到这个错误BinaryTree模块:
module BinaryTree (Tree(..), Path(..), allPaths) where...
我很茫然......我不知道我做错了什么。建议,不管前进多么明显,都非常受欢迎。谢谢。
UPDATE
谢谢大家,你们的帮助。从
@Travis除了每个人都建议我结束之前我看了你的消息,这样做昨晚:
import BinaryTree
main = do
print (findPaths (Node Leaf Leaf))
它的工作原理我预期的方式。但在将来,我会确保遵循您引用我的正确语义。
更新2
我昨晚回应与其他一些答案,但显然有值得答案和问题的power outage和4小时丢失了。想到也许我曾梦想着回答这些问题。很高兴知道我不疯狂。
2.是的,如果你使用'putStrLn'因为'putStrLn'需要一个字符串和树是不是一个字符串的'show'是必要的。您可以使用'show'将树转换为字符串。你导出'Show'的原因是,你的Tree类型甚至有一个'show'函数。如果你没有派生(或手动实例化)'Show',你就不能调用'show'。 – sepp2k 2010-10-09 09:14:22