haskell - 从主模块调用 Haskell 函数时遇到问题

标签 haskell

我在名为 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/

相关文章:

haskell - Mac OS、LLVM 3.7 和缺少 math.h header

haskell - 组合 2 参数函数链

haskell - Haskell 中的 floor 和 truncate 有区别吗

haskell - 为什么我没有因使用 ‘it’ 而获得 (Num (m0 b0)) 的实例

haskell - 当类型类只有一个时,具有 2 个参数的类型的类型类实例

Haskell Cabal 为所有已安装的软件包重新生成文档

haskell - fold 可以用来创建无限列表吗?

haskell - 柯里化(Currying)是如何工作的?

haskell - 如何获取 `ghci` 来使用我的 `show` 函数?

postgresql - 带有 postgresql-simple 的嵌套类型的 FromRow 实例