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

The Archive Base Latest Questions

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

I’m learning to deal with Lists and Tuples in F# and a problem came

  • 0

I’m learning to deal with Lists and Tuples in F# and a problem came up. I have two lists: one of names and one with names,ages.


let namesToFind = [ "john", "andrea" ]
let namesAndAges = [ ("john", 10); ("andrea", 15) ]

I’m trying to create a function that will return the first age found in namesAndAges given namesToFind. Just the first.

So far I have the following code which returns the entire tuple (“john”, 10).


let findInList source target = 
            let itemFound = seq { for n in source do
                                    yield target |> List.filter (fun (x,y) -> x = n)  }                                
                                |> Seq.head    
            itemFound

I tried using fst() in the returning statement but it does not compile and gives me “This expression was expected to have type ‘a * ‘b but here has type (‘c * ‘d) list”

Thanks for any help!

  • 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:56:42+00:00Added an answer on May 18, 2026 at 7:56 pm

    There are lots of functions in the Collections.List module that can be used. Since there are no break or a real return statement in F#, it is often better to use some search function, or write a recursive loop-function. Here is an example:

    let namesToFind = [ "john"; "andrea" ]
    let namesAndAges = [ "john", 10; "andrea", 15 ]
    
    let findInList source target =
      List.pick (fun x -> List.tryFind (fun (y,_) -> x = y) target) source
    
    findInList namesToFind namesAndAges
    

    The findInList function is composed of two functions from the Collections.List module.

    • First we have the List.tryFind predicate list function, which returns the first item for which the given predicate function returns true.

      The result is in the form of an option type, which can take two values: None and Some(x). It is used for functions that sometimes give no useful result.

      The signature is: tryFind : ('T -> bool) -> 'T list -> 'T option, where 'T is the item type, and ('T -> bool) is the predicate function type.

      In this case it will search trough the target list, looking for tuples where the first element (y) equals the variable x from the outer function.

    • Then we have the List.pick mapper list function, which applies the mapper-function to each one, until the first result that is not None, which is returned.

      This function will not return an option value, but will instead throw an exception if no item is found. There is also an option-variant of this function named List.tryPick.

      The signature is: pick : ('T -> 'U option) -> 'T list -> 'U, where 'T is the item type, 'U is the result type, and ('T -> 'U option) is the mapping function type.

      In this case it will go through the source-list, looking for matches in the target array (via List.tryFind) for each one, and will stop at the first match.


    If you want to write the loops explicitly, here is how it could look:

    let findInList source target =
      let rec loop names =
        match names with
        | (name1::xs) -> // Look at the current item in the
                         // source list, and see if there are
                         // any matches in the target list.
            let rec loop2 tuples =
              match tuples with
              | ((name2,age)::ys) ->  // Look at the current tuple in
                                      // the target list, and see if
                                      // it matches the current item.
                  if name1 = name2 then
                    Some (name2, age) // Found a  match!
                  else
                    loop2 ys          // Nothing yet; Continue looking.
              | [] -> None            // No more items, return "nothing"
            match loop2 target with           // Start the loop
            | Some (name, age) -> (name, age) // Found a match!
            | None -> loop rest               // Nothing yet; Continue looking.
        | [] -> failwith "No name found" // No more items.
    
      // Start the loop
      loop source
    

    (xs and ys are common ways of writing lists or sequences of items)

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

Sidebar

Related Questions

I've tracked down a weird MySQL problem to the two different ways I was
I have been unable to fix a problem with Java Unicode and encoding. The
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. 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.