Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OCaml不喜欢函数的返回类型。这个函数和我所有的函数都有操作,但都给了我错误?_Ocaml - Fatal编程技术网

OCaml不喜欢函数的返回类型。这个函数和我所有的函数都有操作,但都给了我错误?

OCaml不喜欢函数的返回类型。这个函数和我所有的函数都有操作,但都给了我错误?,ocaml,Ocaml,这个函数应该递归地告诉两个列表中每个元素的差异。但是当我运行它时,ocaml不喜欢类型和连接 let rec diffImRow image1Row image2Row = if List.length image1Row = 0 then [] else ((List.hd image2Row) - (List.hd image1Row)) :: diffImRow (List.hd image1Row), (List.hd image2Row) ;; 这段代码有几个

这个函数应该递归地告诉两个列表中每个元素的差异。但是当我运行它时,ocaml不喜欢类型和连接

let rec diffImRow image1Row image2Row =
    if List.length image1Row = 0
    then []
    else ((List.hd image2Row) - (List.hd image1Row)) :: diffImRow (List.hd image1Row), (List.hd image2Row)
;;

这段代码有几个问题:diffImRow List.hd image1Row,List.hd image2Row首先,您要在::操作符的右侧发送每个列表的标题。您希望递归,每个列表的尾部位于右侧。此外,参数之间还有一个逗号。该守则的运作如下:

let rec diffImRow image1Row image2Row =
  if List.length image1Row = 0
  then []
  else ((List.hd image2Row) - (List.hd image1Row)) :: diffImRow (List.tl image1Row) (List.tl image2Row)

我认为这只是一个输入错误,您在::的右侧编写了List.hd而不是List.tl,如果启用了,.List.length,您应该使用模式匹配lst和|[]->……|x::xs->。。。分解列表。这还将列表的头和尾分别绑定到x和xs。您可以组合模式匹配,例如,将image1Row、image2Row与|[]、[]->……|进行匹配x::xs,y::ys->,等等。这将确保您已经处理了代码中的所有异常情况。例如,如果在上面的函数中image2Row为[],则对其调用hd两次会怎么样?。。。这就是为什么List.hd和List.tl在OCaml中被视为“代码气味”;您应该进行模式匹配,以便让您自己和编译器有机会考虑代码可以采用的所有情况。