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'函数。当我将其放入文件并尝试对其进行编译时。它发出错误消息。我不知道为什么。需要帮忙; 谢谢。
[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]
putStrLn
接受String
(其类型为putStrLn :: String -> IO ()
),而不是数字类型。你想要的是print
...
print ret
print
可以采用具有Show
实例的任何类型。它的类型是print :: Show a => a -> IO ()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句