如何访问 OCaml 中的列表

发布于 2024-10-08 13:47:56 字数 694 浏览 8 评论 0原文

我想编写一个函数来检查列表中的每个项目是 truefalse。如果至少有一个元素为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半暖夏伤 2024-10-15 13:47:57
let rec checkFalse xs =
    match xs with [] -> false
    | false :: _ -> true
    | _ :: tl -> checkFalse tl;;
let rec checkFalse xs =
    match xs with [] -> false
    | false :: _ -> true
    | _ :: tl -> checkFalse tl;;
音栖息无 2024-10-15 13:47:57

最简单的方法就是 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.

莫相离 2024-10-15 13:47:57

让 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).

有深☉意 2024-10-15 13:47:56

看一下 List 模块: http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html 特别是 exists 方法。对于您想要的,您可以简单地执行以下操作:

List.exists (fun x -> not x) [true;true;...;false;...]

如果列表中的任何元素满足谓词(函数),则 exists 函数将返回 true。在这种情况下,谓词是 fun x ->不是 x,如果 x 为 false,则返回 true。

对于一般列表访问,通常使用模式匹配和递归,或使用函数 itermapfold_left来完成此操作Fold_right(等等)。这是使用模式匹配的 exists 实现:

let rec exists f l = match l with
  | [] -> false (* the list is empty, return false *)
  | h::t -> if (f h) then true (* the list has a head and a (possibly empty) tail.  Check the return value of the predicate 'f' when applied to the head *)
    else exists f t (* the predicate is false, recursively call the `exists` function on the tail *)

编辑:正如 Chuck 所发布的,而不是 fun x ->; not x 你可以简单地使用not

另一种可能性是使用 mem 函数:

List.mem false bools

Take a look at the List module: http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html specifically the exists method. For what you want, you can simply do this:

List.exists (fun x -> not x) [true;true;...;false;...]

The exists function will return true if any element in the list satisfies the predicate (the function). In this case, the predicate is fun x -> not x which will return true if x is false.

For general list access, you generally do this using pattern matching and recursion, or using the functions iter, map, fold_left, and fold_right (among others). Here's an implementation of exists using pattern matching:

let rec exists f l = match l with
  | [] -> false (* the list is empty, return false *)
  | h::t -> if (f h) then true (* the list has a head and a (possibly empty) tail.  Check the return value of the predicate 'f' when applied to the head *)
    else exists f t (* the predicate is false, recursively call the `exists` function on the tail *)

edit: as Chuck has posted, instead of fun x -> not x you can just simply use not.

Another possibility is to use the mem function:

List.mem false bools
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文