我在一個名爲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