如何访问 OCaml 中的列表
我想编写一个函数来检查列表中的每个项目是 true
或 false
。如果至少有一个元素为 false
,它将返回 true
,因此:
assert_eq "checkFalse [true; false; true]" (checkFalse [true; true; true]) false;
assert_eq "checkFalse [false; false]" (checkFalse [false; true]) true;
我是 OCaml 的绝对初学者,我不知道如何解决这个问题。我尝试使用 for 循环,例如:
let rec checkFalse (bools: bool list) : bool =
for i = 0 to bools.length do
if bools.length == false then false
else... (I don't know how to continue)
然后它说“未绑定记录字段......”
我也尝试使用 find ,例如: if (find false bools != Not_found) then true else false
但我的方法不起作用。我有 Java 背景。
I want to write a function that could check every item in a list is true
or false
. If at least one element is false
, it will return true
, so that:
assert_eq "checkFalse [true; false; true]" (checkFalse [true; true; true]) false;
assert_eq "checkFalse [false; false]" (checkFalse [false; true]) true;
I am an absolute beginner in OCaml and I don't know how to approach this. I tried using a for loop, something like:
let rec checkFalse (bools: bool list) : bool =
for i = 0 to bools.length do
if bools.length == false then false
else... (I don't know how to continue)
Then it said "Unbound record field...."
I also tried using find like:if (find false bools != Not_found) then true else false
But my ways did not work. I came from a Java background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法就是
let checkFalse = List.exists not
。List.exists
将一个函数和一个列表作为参数,并告诉您传递的函数是否对列表中的任何元素返回 true。not
返回布尔值的否定。The simplest way would just be
let checkFalse = List.exists not
.List.exists
takes a function and a list as arguments, and tells if the function you passed returns true for any element in the list.not
returns the negation of a bool.让 checkFalse = List.exists (fun elem -> elem = false) your_list in
doc:
val 存在:('a -> bool) -> '列表-> bool
存在 p [a1; ...; an] 检查列表中是否至少有一个元素满足谓词 p。
也就是说,它返回 (p a1) || (p a2) || ... || (平底锅)。
let checkFalse = List.exists (fun elem -> elem = false) your_list in
doc:
val exists : ('a -> bool) -> 'a list -> bool
exists p [a1; ...; an] checks if at least one element of the list satisfies the predicate p.
That is, it returns (p a1) || (p a2) || ... || (p an).
看一下
List
模块: http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html 特别是exists
方法。对于您想要的,您可以简单地执行以下操作:如果列表中的任何元素满足谓词(函数),则
exists
函数将返回 true。在这种情况下,谓词是fun x ->不是 x
,如果 x 为 false,则返回 true。对于一般列表访问,通常使用模式匹配和递归,或使用函数
iter
、map
、fold_left
和来完成此操作Fold_right
(等等)。这是使用模式匹配的exists
实现:编辑:正如 Chuck 所发布的,而不是
fun x ->; not x
你可以简单地使用not
。另一种可能性是使用 mem 函数:
Take a look at the
List
module: http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html specifically theexists
method. For what you want, you can simply do this:The
exists
function will return true if any element in the list satisfies the predicate (the function). In this case, the predicate isfun x -> not x
which will return true ifx
is false.For general list access, you generally do this using pattern matching and recursion, or using the functions
iter
,map
,fold_left
, andfold_right
(among others). Here's an implementation ofexists
using pattern matching:edit: as Chuck has posted, instead of
fun x -> not x
you can just simply usenot
.Another possibility is to use the
mem
function: