9

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?

vik santata
  • 2,989
  • 8
  • 30
  • 52

2 Answers2

15

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.

BitTickler
  • 10,905
  • 5
  • 32
  • 53
12

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.

Gus
  • 25,839
  • 2
  • 51
  • 76
  • 1
    Also available for `Array`. It is also worth mentioning that if you are on an older version of F# than 4.0, you can instead use the Linq extension methods: e.g. `[1..5].Contains(5)` – torbonde Feb 02 '16 at 07:45