我是在psci repl中这样做的。这是我的功能:
$> import Data.List
$> let f (x:xs) = x
$> let f Nil = Nil
$> let a = 1:2:3:Nil
$> f a
我收到此错误:
Could not match type
Int
with type
List t0
while trying to match type List Int
with type List (List t0)
while checking that expression a
has type List (List t0)
in value declaration it
where t0 is an unknown type
我理解这种情况正在发生,因为我的函数f
没有返回List Int
的签名。但是如何在repl中声明一个?
答案 0 :(得分:1)
这里的问题是f
返回两种不同的类型:
let f (x:xs) = x
返回列表的一个元素(a
为Int
,f a
为let f Nil = Nil
,而
List
正在返回{{1}}。