2014-06-22 47 views
1
head' :: [a] -> a 
head' [] = error "Cannot call head on emply list." 
head' (x:_) = x 

main = do 
    putStrLn "hello" 
    let ret = head' [4,5,6] 
    putStrLn ret 

上面的代碼,我可以加載它在ghci中並正確調用head的函數。 當我把它放入一個文件並嘗試編譯它。 IT會出錯。 我想不通這是爲什麼。需要幫忙; 謝謝。haskell,爲什麼ghci中的頭函數沒有問題,但無法編譯?

[1 of 1] Compiling Main    (head.hs, head.o) 

head.hs:7:22: 
    No instance for (Num String) arising from the literal `4' 
    Possible fix: add an instance declaration for (Num String) 
    In the expression: 4 
    In the first argument of head', namely `[4, 5, 6]' 
    In the expression: head' [4, 5, 6] 

回答

4

putStrLn需要String(其類型爲putStrLn :: String -> IO()),而不是數字類型。你想要的是print

... 
    print ret 

print可以採取具有Show實例的任何類型。它的類型是print :: Show a => a -> IO()

相關問題