【问题标题】:Ocaml Type error: This expression has type 'a * 'b but an expression was expected of type 'c listOcaml 类型错误:此表达式的类型为 'a * 'b 但表达式应为 'c 列表类型
【发布时间】:2020-01-26 08:13:22
【问题描述】:

我需要输出一对列表,但我不明白为什么我返回的这对不是正确的类型。

let rec split l = match l with
  | [] -> []
  | [y] -> [y]
  | x :: xs ->
      let rec helper l1 acc = match l1 with
        | [] -> []
        | x :: xs ->
            if ((List.length xs) = ((List.length l) / 2)) then
                (xs, (x :: acc))
            else helper xs (x :: acc)
      in helper l []

【问题讨论】:

    标签: ocaml


    【解决方案1】:

    (请花时间在 SO 上复制/粘贴和格式化您的代码,而不是提供指向图像的链接。这样更容易提供帮助,并且在未来更有用。)

    您的辅助函数中match 的第一种情况不返回一对。 match 的所有情况都需要返回相同的类型(当然)。

    请注意,您最外面的 match 的案例也是不同类型的(如果您假设 helper 返回一对)。

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多