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]