ocaml - 删除列表中的前 n 个项目

标签 ocaml

我正在尝试创建一个删除列表前 n 项的函数:

let rec drop n h =
   if n == 0 then h else (drop n-1 (match h with a::b -> b));;

这是给:
Characters 43-49:
   if n == 0 then h else (drop n-1 (match h with a::b -> b));;
                          ^^^^^^
Error: This expression has type 'a -> 'b but is here used with type int

这里有什么问题?这是我在 OCAML 中的第一天(一般是函数式编程),我只是在学习互联网上的手册和教程。我不知道这条消息是什么意思。

此外,这是一个更大的作业的一部分,除了函数定义之外不需要使用 Let,也不需要使用额外的库

最佳答案

drop n-1 被解析为 (drop n) - 1 ,你想要 drop (n-1)

关于ocaml - 删除列表中的前 n 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971522/

相关文章:

arrays - Ocaml 中的数组操作

types - 使用不带标签的 Core.Std.List.fold_left

ocaml - 在ocaml中检查记录大小?

dictionary - 为什么 Map.make.fold 更像 List.fold_right (非尾递归)?

使用 BuckleScript 的 ReasonML 中的 JavaScript 构造函数行为

functional-programming - 像SML/NJ中的 `use mine.sml`一样,如何在OCaml的顶层加载ml文件?

ocaml - 使用它编译C lib和OCaml exe,全部使用ocamlfind

types - ocaml 类型内的类型

OCaml:如何正确处理 sum 类型?

find - 如何访问 OCaml 中的列表