Ocaml 类型与函数 div 中的列表不匹配

问题描述 投票:0回答:1

我正在尝试编写一个除法函数(作为合并排序的一部分),它接受一个列表并返回几个长度大致相同的列表。 我想使用 List.length() 函数来做到这一点。

我的代码是:

let rec transRec l1 l2 = 
  match l1, l2 with 
  | l1, [] -> l1, []
  | l1, h :: tail -> 
      if List.length l1 < List.length l2 then 
        transRec h :: l1 tail
      else 
        l1, l2
in 
transRec [] liste;;

编译时出现以下错误,我不明白:

line 4, characters 5-26:
Error: This expression has type 'a list
       but an expression was expected of type 'b list * 'c list

我不明白为什么

transRec h :: l1 tail
被视为一个列表。

list types ocaml
1个回答
1
投票

您需要在

(h :: l1)
两边加上括号。如果没有括号,表达式的解析如下:

(transRec h) :: (l1 tail)

这是因为函数应用在 OCaml 中具有很高的优先级。

编译器告诉您表达式必须是某种列表(因为中间有

::
运算符),但您需要一对列表。

© www.soinside.com 2019 - 2024. All rights reserved.