2010-10-09 86 views
3

我在一個名爲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小時丟失了。想到也許我曾夢想着回答這些問題。很高興知道我不瘋狂。

+1

2.是的,如果你使用'putStrLn'因爲'putStrLn'需要一個字符串和樹是不是一個字符串的'show'是必要的。您可以使用'show'將樹轉換爲字符串。你導出'Show'的原因是,你的Tree類型甚至有一個'show'函數。如果你沒有派生(或手動實例化)'Show',你就不能調用'show'。 – sepp2k 2010-10-09 09:14:22

回答

3

你會看到這個錯誤,因爲findPaths (Node Leaf Leaf)爲您Main模塊,should only contain declarations的頂層的表達式。

您可以嘗試編譯只包含一個字符串的文件,例如得到GHC了同樣的錯誤:

[email protected]% echo '"Hello world"' > Test.hs 
[email protected]% 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" 

我不知道爲什麼GHC給相關模板哈斯克爾這裏 - 一個錯誤信息對一個簡單的錯誤來說,這是一個神祕和混亂的迴應。你實際需要做的就是將表達式改爲聲明。以下應該只是罰款:

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

GHC隱式拼接模板哈斯克爾聲明在最高水平的6.12.1版本:如果'decls'是類型'Q [月]的值爲'你可以只寫'decls'而不是'$(decls)傳統的語法'。這個工程即使沒有啓用TemplateHaskell語言擴展:一個錯誤,這似乎是固定GH​​C 7:http://hackage.haskell.org/trac/ghc/ticket/4042 – 2010-10-10 15:35:59

2

我覺得這裏的問題是你的GHC的電話,試試這個:

ghc -o main --make Main.hs 
4

要添加到什麼Jonno_FTW說,你需要一個main常規的主模塊,它需要做的IO在。所以,你應該Main.hs是這樣的:

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

這是正確的,問題提問者似乎認爲裸功能的主模塊調用將被調用,以... – yatima2975 2010-10-09 07:19:56

+0

我實際上做了,因爲我做的東西有點類似之前,所以我估計它會工作。但在那個代碼現在看,我看到,我有一個線,然後'主要= putStrLn的「HelloWorld」''插入5(節點1葉葉)''那裏是insert'在另一個模塊中定義。 – 2010-10-09 07:34:50

+0

'putStrLn。 show'與'print'是一樣的btw。 – sepp2k 2010-10-09 09:12:00