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

  • SEARCH
  • Home
  • 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 4574152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:53:02+00:00 2026-05-21T19:53:02+00:00

I want to go through an array and return a list of ints (the

  • 0

I want to go through an array and return a list of ints (the value of indexes) when a value in the array matches true.

The array is a boolean array of just true/false values.

let get_elements (i:int)(b:bool) : int = 
    if b = true then (i::l)
    else (())
;;

let rec true_list (b: bool array) : int list = 
    (fun i l -> get_elements i l)
;;

The syntax is wrong for my code and I am confused on exactly how to return a list of ints.I only want to return the indexes of those elements that are true in the array.

  • 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-21T19:53:03+00:00Added an answer on May 21, 2026 at 7:53 pm

    You refer to ‘l’ in get_elements, but it’s not in the scope of that function.

    Here’s an approach using a ref to an integer list (a mutable list):

     boolarray = [|true; false; true; false; false; true|] ;;
     type ilist = (int list) ref ;;
     let intlist () : ilist = ref [] ;;
     let push ( l: ilist) (x: int) : unit = l := x::(!l) ;;
     let lst = intlist () ;;
     Array.iteri ( fun i b -> if b = true then (push lst i )) boolarray ;;
     !lst ;; (* => int list = [5; 2; 0] *)
    

    Or, if you’d rather avoid refs (which is usually a good idea) this is cleaner:

    let get_true_list (b: bool array) : int list =
      let rec aux i lst  =     
        if (i = Array.length b)  then lst else
          (if b.(i) = true then ( aux (i+1) (i::lst)) else (aux (i+1) lst))  in
       aux 0 [] ;;
     (* using boolarray defined above *)
     get_true_list boolarray ;; (* => int list = [5; 2; 0] *)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of values I want to insert into a table via
I would like to take bites out of a list (or array) of a
I am dealing with a set of native functions that return data through dynamically-allocated
I want to pass the result of a query through a $.post. function GetAllTasks()
I have a case where I have an array of keywords. I want to
I want to select from a table, get all the values and put them
I'm finding I can't log in to my Kohana site through IE. (just IE,
I've got this class, let's call it Refund (because that's what it's called). I
I am really new in PHP and need a suggestion about array search. If
I'm having a problem with my work that hopefully reduces to the following: I

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.