开发者

Trouble calling a Haskell function from the main module

I've defined a function called findPaths in a Haskell Module called BinaryTree and I am trying to call that function in the main module I've created. The type of the function call is

findPaths :: Tree -> [Path]

Where Tree is a data type defined as:

data Tree = Leaf | Node Tree Tree

and Path is defined as:

data Path = LeftTurn Path | RightTurn Path | This

In the main function I'm doing this and only this:

module Main where
import BinaryTree
findPaths (Node Leaf Leaf)

But when I try and compile this with the following command:

ghc -o --make Main Main.hs BinaryTree.hs

I get this error:

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...

I'm at a loss... I don't know what I'm doing wrong. Suggestions, no matter how straight forward and obvious are greatly welcome. Thank you.

UPDATE

Thank you, all of you, for your help.

@Travis Aside from what everyone suggested I ended up doing this last night before I read your message:

import BinaryTree

main = do
    print (findPaths (Node Leaf Leaf))

It works the way I expected it to. But in the future, I'll make sure I follow the proper semant开发者_开发技巧ics you referenced me.

UPDATE 2

I had responded last night with some other answers but apparently there was a power outage and 4 hours worth of answers and questions were lost. Thought maybe I had dreamt answering those questions. Good to know I'm not crazy.


To add to what Jonno_FTW said, you need a main routine in your Main module and it needs to do IO. So your Main.hs should be something like this:

module Main where
import BinaryTree
main = putStrLn . show . findPaths $ Node Leaf Leaf


You're seeing this error because findPaths (Node Leaf Leaf) is an expression at the top level of your Main module, which should only contain declarations.

You can get the same error from GHC by trying to compile a file containing only a string literal, for example:

travis@sidmouth% echo '"Hello world"' > Test.hs
travis@sidmouth% ghc Test.hs

Test.hs:1:0:
    Couldn't match expected type `Language.Haskell.TH.Syntax.Q
                                    [Language.Haskell.TH.Syntax.Dec]'
           against inferred type `[Char]'
    In the expression: "Hello world"

I have no idea why GHC gives an error message related to Template Haskell here—it's an arcane and confusing response to a simple mistake. All you actually need to do is change the expression to a declaration. The following should work just fine:

module Main where
import BinaryTree
paths = findPaths (Node Leaf Leaf)
main = putStrLn "Do something here."


I think the problem here is in your calling of ghc, try this:

ghc -o main --make Main.hs
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜