【发布时间】:2013-03-13 11:37:37
【问题描述】:
我正在学习 OCaml,我给自己的练习问题之一是在创建的列表中查找元素的索引。到目前为止,我以为我拥有它,但我已经重写这个代码块很长时间了,似乎无法理解为什么返回值不正确。
let rec indexer_helper list element index pos found=
match l with
[] -> if (found = false) then
(-1)
else
index
| (h::t) -> if (h = e) then
index = pos
pos = pos + 1
indexer_helper t element index pos true
else
pos = pos + 1
indexer_helper t element index pos found;;
let rec indexer list element = indexer_helper list element 0 0 false;;
编辑:问题已解决。问题是我在“更改”不可变变量时忘记使用 let 语句。
【问题讨论】:
-
@JeffreyScofield 在现场。您也可以在
if (h = e) then行中问自己这个e是什么。 -
请保留原来的问题,这样答案对未来的读者有意义。告诉您问题已解决的 stackoverflow 方法是接受您所做的答案。
标签: list recursion indexing ocaml