Search code examples
f#is-empty

How to check in f# if the set is Empty?


How can I check if the set is empty, I have implemented the following code if the set has an empty set then I should get the value true else false, for example: [1; 2; []] this set should result in true and if the set is [1; 2; 3] this should result in false because this one does not have an empty set in it. Following is the code that gives me an error:

let rec isEmpty S =
   match S with
  |[] -> true
  |_ -> false
  |e::rest -> ([]=e) || (isEmpty [] rest)

Solution

  • Unfortunately for your use case F# is quite a bit more 'strongly typed' than you would like here.

    In F# a value like [1; 2; 3] is an int list, and an int list won't accept a value like [1; 2; []].

    Maybe you could try to define a new (recursive) datatype for your problem.