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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:19:48+00:00 2026-06-13T14:19:48+00:00

If there’s another way to achieve what I’m trying to do below, please let

  • 0

If there’s another way to achieve what I’m trying to do below, please let me know. Suppose I have the following sample code

type FooBar = 
  | Foo
  | Bar

let foobars = [Bar;Foo;Bar]

let isFoo item  = 
  match item with
  | Foo _ -> true
  | _ -> false

foobars |> Seq.filter isFoo

I want to write a generic/higher-order version of isFoo that allows me to filter my list based on all other types of the discriminated union (Bar in this case).

Something like the following, where ‘a can be either Foo or Bar

let is<'a> item  = 
  match item with
  | a _ -> true
  | _ -> false

However, this attempt yields the following error:

error FS0039: The pattern discriminator ‘a’ is not defined

  • 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-06-13T14:19:50+00:00Added an answer on June 13, 2026 at 2:19 pm

    If you just want to filter a list, then the easiest option is to use function to write standard pattern matching:

    [ Foo; Bar; Foo ]
    |> List.filter (function Foo -> true | _ -> false)
    

    If you wanted to write some more complicated generic function that checks for a case and then does something else, then the easiest option (that will work in general) is to take a predicate that returns true or false:

    let is cond item  = 
      if cond item then
        true
      else
        false
    
    // You can create a predicate using `function` syntax
    is (function Foo -> true | _ -> false) <argument>
    

    In your specific example, you have a discriminated union where none of the cases has any parameters. This is probably an unrealistic simplification, but if you only care about discriminated unions without parameters, then you can just use the cases as values and compare them:

    let is case item = 
      if case = item then
        true
      else
        false
    
    // You can just pass it 'Foo' as the first parameter to 
    // `is` and use partial function application
    [ Foo; Bar; Foo ]
    |> List.filter (is Foo)
    
    // In fact, you can use the built-in equality test operator
    [ Foo; Bar; Foo ] |> List.filter ((=) Foo)
    

    This last method will not work if you have more complicated discriminated union where some cases have parameters, so it is probably not very useful. For example, if you have a list of option values:

    let opts = [ Some(42); None; Some(32) ]
    opts |> List.filter (is Some) // ERROR - because here you give 'is' a constructor 
                                  // 'Some' instead of a value that can be compared. 
    

    You could do various tricks using Reflection (to check for cases with a specified name) and you could also use F# quotations to get a bit nicer and safer syntax, but I do not think that’s worth it, because using pattern matching using function gives you quite clear code.

    EDIT – Just out of curiosity, a solution that uses reflection (and is slow, not type safe and nobody should actually use it in practice unless you really know what you’re doing) could look like this:

    open Microsoft.FSharp.Reflection
    open Microsoft.FSharp.Quotations
    
    let is (q:Expr) value = 
      match q with
      | Patterns.Lambda(_, Patterns.NewUnionCase(case, _)) 
      | Patterns.NewUnionCase(case, _) ->
          let actualCase, _ = FSharpValue.GetUnionFields(value, value.GetType())
          actualCase = case
      | _ -> failwith "Wrong argument"
    

    It uses quotations to identify the union case, so you can then write something like this:

    type Case = Foo of int | Bar of string | Zoo
    
    [ Foo 42; Zoo; Bar "hi"; Foo 32; Zoo ]
    |> List.filter (is <@ Foo @>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is any way to set the generic type (T) of class in the
There seems to be no way to find a specific entry in the commit
There is this strange situation I'm fighting on. I have 3 pages, les call
There is a text file with 1,000,000 lines of code floating around and Ctrl+F
There have been multiple questions regarding this topic but I have never really settled
There is my code that I want to alert test once when mousemove ,
There is some contradiction in the api documentation: on one location: https://developer.foursquare.com/docs/responses/user on another
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
There is a component that I have been using since IE7 and had never
I know there's a lot of other questions out there that deal with this

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.