algorithm - 将数字添加到函数 OCaml 中的列表

标签 algorithm ocaml ml

这是我所拥有的,我很遗憾地遇到的错误是

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/

相关文章:

python - 求解 x^2 + y^2 + z^2 = N 得到 x, y, z 的所有唯一组合

c++ - 反转数组时遇到问题

algorithm - Ocaml函数调用,递归调用自身

ocaml - ML中有评估吗?

algorithm - 打印(或输出到文件)欧几里得算法的步骤数表

android - 应用程序可以保密吗?

ocaml - 我可以从运行时选择的 OCaml 类继承吗?

ocamlfind:在 ubuntu 17.04 上找不到包 `lablgtk2.gnomecanvas'

ubuntu - apt remove 和 apt purge 后 Ocaml 留在系统中

f# - 通过翻译 ML 的等效项来使用 F# 实现 take