【问题标题】:how to pattern match a list head and tail in purescript如何在purescript中模式匹配列表头和尾
【发布时间】:2016-08-24 21:01:28
【问题描述】:

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

【问题讨论】:

    标签: purescript


    【解决方案1】:

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

    let f (x:xs) = x
    

    返回列表的一个元素(所以a,或Intf a),而

    let f Nil = Nil
    

    正在返回 List

    【讨论】:

    • 我该如何修复它以使f 返回Maybe 类型?
    • f (x:xs) = Just x f Nil = Nothing
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 2022-12-01
    • 2020-12-28
    • 2011-07-07
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多