Search code examples
f#contain

Does F# has a function to tell if a list contains a specific value?


Haskell has "elem" predicate to tell like:

Prelude> 5 `elem` [2,4..10]
False

In F#, how to conveniently tell whether a value is in a list, or array, or seq or map, or dictionary?


Solution

  • You can use:

    List.exists ((=) 5) [1..5]
    

    Or as suggested in the other answer, directly List.contains if you have the latest F# version.

    The same functions are available for Seq.