如何模式匹配purescript中的列表头部和尾部

时间:2016-08-24 21:01:28

标签: purescript

我是在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中声明一个?

1 个答案:

答案 0 :(得分:1)

这里的问题是f返回两种不同的类型:

let f (x:xs) = x

返回列表的一个元素(aIntf alet f Nil = Nil ,而

List

正在返回{{1}}。