这是我所拥有的,我很遗憾地遇到的错误是
Error: This function has type 'a * 'a list -> 'a list
It is applied to too many arguments; maybe you forgot a `;'.
为什么会这样?我计划将两个列表传递给 deleteDuplicates 函数,一个排序列表和一个空列表,并期望删除列表 r 中的重复项,一旦原始列表到达 []
就会返回条件。
会带着更新的代码回来
let myfunc_caml_way arg0 arg1 = ...
rather than
let myfunc_java_way(arg0, arg1) = ...
Then you can call your function in this way:
myfunc_caml_way "10" 123
rather than
myfunc_java_way("10, 123)
最佳答案
我不知道这可能有多大用处,但这里有一些代码可以满足您的需求,以相当标准的 OCaml 风格编写。花一些时间确保您了解它的工作原理和原因。也许您应该从更简单的事情开始(例如您将如何对整数列表的元素求和?)。 实际上,您可能应该从 OCaml 教程开始,仔细阅读并确保您理解代码示例。
let deleteDuplicates u =
(*
u : the sorted list
v : the result so far
last : the last element we read from u
*)
let rec aux u v last =
match u with
[] -> v
| x::xs when x = last -> aux xs v last
| x::xs -> aux u (x::v) x
in
(* the first element is a special case *)
match u with
[] -> []
| x::xs -> List.rev (aux xs [x] x)
关于algorithm - 将数字添加到函数 OCaml 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21591833/