这个函数应该递归地告诉两个列表的每个元素的差异。但是当我运行它时,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)