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

14,089

Solution 1

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.

Solution 2

In F# it is

List.contains <element> <list>

Example:

List.contains 5 [2..2..10]

-->

val it : bool = false

contains is also defined for the other container types.

Share:
14,089
vik santata
Author by

vik santata

Updated on June 04, 2022

Comments

  • vik santata
    vik santata almost 2 years

    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?