Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3595282
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:49:54+00:00 2026-05-18T19:49:54+00:00

I want to write a function that could check every item in a list

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:49:55+00:00Added an answer on May 18, 2026 at 7:49 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that would return the boolean true if the
My function iterates through every node of an instance of an XMLDocument . It
I want to write a simple Ruby DSL to translate some statements and expressions
Can PHP dissect its own syntax? For example, I'd like to write a function
I want to check a file on the server only once each day (NSURL).
I'm new to LINQ,My knowledge on that library is from Jon Skeet's book C#
I want to be able to store a backgroundcolor for a panel in my
I've just installed VS2008 and have run into a problem that I'm sure can
Is it possible to detect the email client in an html message? Specifically outlook
We are supplying a blob of HTML content to be imported and displayed on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.