我正在尝试创建一个删除列表前 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/