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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:07:46+00:00 2026-05-15T05:07:46+00:00

I am completely new to F# (and functional programming in general) but I see

  • 0

I am completely new to F# (and functional programming in general) but I see pattern matching used everywhere in sample code. I am wondering for example how pattern matching actually works? For example, I imagine it working the same as a for loop in other languages and checking for matches on each item in a collection. This is probably far from correct, how does it actually work behind the scenes?

  • 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-15T05:07:46+00:00Added an answer on May 15, 2026 at 5:07 am

    It depends on what kind of pattern matching do you mean – it is quite powerful construct and can be used in all sorts of ways. However, I’ll try to explain how pattern matching works on lists. You can write for example these patterns:

    match l with
    | [1; 2; 3] ->  // specific list of 3 elements
    | 1::rest ->    // list starting with 1 followed by more elements
    | x::xs ->      // non-empty list with element 'x' followed by a list
    | [] ->         // empty list (no elements)
    

    The F# list is actually a discriminated union containing two cases – [] representing an empty list or x::xs representing a list with first element x followed by some other elements. In C#, this might be represented like this:

    // Represents any list
    abstract class List<T> { }
    // Case '[]' representing an empty list
    class EmptyList<T> : List<T> { }
    // Case 'x::xs' representing list with element followed by other list
    class ConsList<T> : List<T> {
      public T Value { get; set; } 
      public List<T> Rest { get; set; }
    }
    

    The patterns above would be compiled to the following (I’m using pseudo-code to make this simpler):

    if (l is ConsList) && (l.Value == 1) &&
       (l.Rest is ConsList) && (l.Rest.Value == 2) &&
       (l.Rest.Rest is ConsList) && (l.Rest.Rest.Value == 3) &&
       (l.Rest.Rest.Rest is EmptyList) then
       // specific list of 3 elements
    else if (l is ConsList) && (l.Value == 1) then
       var rest = l.Rest;
       // list starting with 1 followed by more elements
    else if (l is ConsList) then
       var x = l.Value, xs = l.Rest;
       // non-empty list with element 'x' followed by a list
    else if (l is EmptyList) then 
       // empty list (no elements)
    

    As you can see, there is no looping involved. When processing lists in F#, you would use recursion to implement looping, but pattern matching is used on individual elements (ConsList) that together compose the entire list.

    Pattern matching on lists is a specific case of discriminated union which is discussed by sepp2k. There are other constructs that may appear in pattern matching, but essentially all of them are compiled using some (complicated) if statement.

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

Sidebar

Related Questions

No related questions found

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.