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 6909615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:42:43+00:00 2026-05-27T08:42:43+00:00

I have a List<List<int>> in F#. I need to iterate through it looking for

  • 0

I have a List<List<int>> in F#. I need to iterate through it looking for a given value val.
In C# I would do something like:

public bool contains(List<List<int>> list, int value)
    foreach (l in list ){
        foreach(val in l){
             if (val == value)
                 return true; //found value
        }
    }
    return false;
} 

I’m looking for the equivalent in F#. I tried the following but I’m doing something wrong, because I’m not still used to F# syntax:

type foo = 
    {
     l : List<List<float>>

    }

let contains (value: float) : bool = 
   for row in foo.l do
       for val in row do
            if (val == value)
                true
   false

The code above is wrong.

Could anyone suggest me how to achieve that result?

  • 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-27T08:42:44+00:00Added an answer on May 27, 2026 at 8:42 am

    This is a direct translation of your C# code:

       let contains value (ls: _ list list) = 
           let mutable found = false
           for row in ls do
               if not found then
                   for el in row do
                        if not found && el = value then
                            found <- true
           found
    

    To modify value of a variable in F#, you should use mutable or ref keywords. However, in F# doing the functional way:

    let contains value ls  = 
         ls |> List.exists (List.exists ((=) value))
    

    Different from for .. in ... do, which is a syntactic sugar, the higher-order function List.exists will stop immediately when it finds the answer. This version does not scale well if your lists are big. You can convert list to set to be able to find the element faster:

    let contains value (ls: _ list list) = 
         ls |> List.concat |> Set.ofList |> Set.contains value
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some financial data gathered at a List[(Int, Double)], like this: val snp
I have a list of objects (mainly int, string, double) And I would like
i have List of int which consists of value 0,0,0,1,2,3,4,0,0 now i like to
I have the following List<int> collection and I need to find the highest integer
I have this Java code which is used for JSF pagination: public List<ActiveSessionObj> list(int
Lets say, we have string ABCAD, now we need to iterate through all possible
In the code below, I have a List object. I want to iterate through
I have a point class like: class Point { public: int x, y; Point(int
I have List I want to sort Desc by Priority, which is int and
In the top Form1 level I have: List<float> cyclicSelectedIndex = new List<float>(2); int currentCyclicIndex;

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.