我在名为 BinaryTree 的 Haskell 模块中定义了一个名为 findPaths
的函数,并且我试图在我创建的主模块中调用该函数。函数调用的类型是
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)
但是当我尝试使用以下命令编译它时:
ghc -o --make Main Main.hs BinaryTree.hs
我收到此错误:
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)
I get the same error if I try to export the data types in the BinaryTree module:
module BinaryTree (Tree(..), Path(..), allPaths) where...
我很茫然......我不知道我做错了什么。建议,无论多么直接和明显,都非常受欢迎。谢谢。
更新
谢谢大家的帮助。
@Travis 除了每个人的建议之外,我昨晚在读到你的消息之前最终这样做了:
import BinaryTree
main = do
print (findPaths (Node Leaf Leaf))
它按照我预期的方式工作。但将来,我将确保遵循您引用我的正确语义。
更新2
我昨晚回复了一些其他答案,但显然有一个 power outage 4 小时的答案和问题都丢失了。我想也许我梦想着回答这些问题。很高兴知道我没有疯。
最佳答案
要补充 Jonno_FTW 所说的内容,您的主模块中需要一个 main
例程并且它需要执行 IO。所以你的 Main.hs
应该是这样的:
module Main where
import BinaryTree
main = putStrLn . show . findPaths $ Node Leaf Leaf
关于haskell - 从主模块调用 Haskell 函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3895835/