2011-04-16 36 views
0

錯誤消息:如何使用實例?

ho8.hs:17:19: 
    Ambiguous type variable `a0' in the constraints: 
     (PPLetter a0) arising from a use of `ppLetter' at ho8.hs:17:19-26 
     (Num a0) arising from the literal `3' at ho8.hs:17:28 
    Probable fix: add a type signature that fixes these type variable(s) 
    In the first argument of `print', namely `(ppLetter 3)' 
    In the expression: print (ppLetter 3) 
    In the expression: do { print (ppLetter 3) } 
Failed, modules loaded: none. 

源代碼:

module Main where 
import Data.List(nub) 

import qualified Text.PrettyPrint.HughesPJ as PP 
import Text.PrettyPrint.HughesPJ(Doc,text,int,(<>),(<+>),($+$),render) 

class PPLetter a where 
ppLetter :: a -> Doc 

instance PPLetter Int where 
ppLetter a = text ("p"++show a) 

instance PPLetter Char where 
ppLetter = PP.char 


main = do {print (ppLetter 3);} 

回答

2

爲您更新的問題,更換

main = do {print (ppLetter 3);} 

main = do {print (ppLetter (3 :: Int));} 
4

顯示約爲::一個 - >字符串

原來的值成一個字符串,但是並不打印向屏幕。

需要一個IO動作實際打印字符串來篩選,通常

putStr :: String -> IO() 

這樣試試:

main = do { putStr (show (ppLetter 3)) } 

更簡潔打印結合了這兩種:

main = do { print (ppLetter 3) } 
+0

我更新的問題,並得到另一個錯誤 – Jo0o0 2011-04-16 11:19:52