list 返回 OCaml 中列表的第 n 个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9795504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return the nth element of a list in OCaml?
提问by Allan Jiang
I am new to Ocaml, just want to make sure how to perform a simple function such as return the nth element of a list by using recursive function?
我是 Ocaml 的新手,只想确定如何执行一个简单的函数,例如使用递归函数返回列表的第 n 个元素?
Prototype like get_nth (list, n)
with int list * int -> int
原型样get_nth (list, n)
带int list * int -> int
for example get_nth ([1,2,3], 1) -> 2
例如 get_nth ([1,2,3], 1) -> 2
Thank you
谢谢
回答by pad
You may not notice but List.nth
function is already there in List module.
您可能没有注意到,但List.nth
函数已经在List 模块中。
If you want to write it using recursion:
如果要使用递归编写它:
let rec get_nth = function
| [], _ -> raise (Failure "get_nth")
| _, n when n < 0 -> raise (Invalid_argument "get_nth")
| x::_, 0 -> x
| x::xs, n -> get_nth(xs, n-1)
回答by LiKao
Using tuples as parameters like this is not common in OCaml. Usually you would use currying and define your function like this:
像这样使用元组作为参数在 OCaml 中并不常见。通常你会使用柯里化并像这样定义你的函数:
let get_nth list n = ...
This would have the signature 'a list -> int -> 'a
. Also note that you have a 'a
paramter here, which means, there is no real reason to restrict your function to ints alone.
这将有签名'a list -> int -> 'a
。另请注意,您在'a
此处有一个参数,这意味着没有真正的理由将您的函数单独限制为整数。
Now let's look at the problem. If you want to get the zeroth element, what would your function look like?
现在让我们来看看问题。如果你想得到第零个元素,你的函数会是什么样子?
let get_nth list 0 = List.head list (* this is not actually valid in OCaml *)
now if you have a function to get the nth element from a list of m items (N.B. n > m), how could you use that function to build another function which get's the n+1st element from a list of m+1 elements? Let that function for n+1 elements be get_nth'
现在,如果您有一个函数可以从 m 个项目的列表中获取第 n 个元素(NB n > m),您如何使用该函数来构建另一个函数,该函数从 m+1 个元素的列表中获取第 n+1 个元素?让 n+1 个元素的函数为get_nth'
let get_nth' list n' = get_nth (List.tail list) (n'-1)
Now all you need to do is to combine the two and you are done. I'll leave that last part up to you.
现在你需要做的就是将两者结合起来,你就完成了。我会把最后一部分留给你。
If you follow this advice you will get something which is more complicated, than it has to be. However it is easier to understand what is happening this way.
如果你遵循这个建议,你会得到一些比它必须更复杂的东西。然而,通过这种方式更容易理解正在发生的事情。
回答by Omar
(In my opinion) A simpler solution without using a tuple can be:
(在我看来)不使用元组的更简单的解决方案可以是:
let rec get_nth mylist index = match mylist with
| [] -> raise (Failure "empty list")
| first::rest ->
if index = 0 then first
else get_nth rest (index-1)
;;
回答by Pie 'Oh' Pah
I've read herethat using Result
instead of raising error can be nicer since you don't have to use a try ... with
. (Code edited from @Omar)
我在这里读到,使用Result
而不是引发错误会更好,因为您不必使用try ... with
. (从@Omar 编辑的代码)
let rec get_nth mylist index = match mylist with
| [] -> Error "empty list"
| first::rest ->
if index = 0 then Ok first
else get_nth rest (index-1)
;;
let result [1; 2; 3] 2 in
match result with
| Error reason -> print_string reason
| Ok num -> print_int num
;;
Result
is part of Core.Std
, if I'm correct.
Result
是 的一部分Core.Std
,如果我是对的。