Question

I have been messing around doing some programming exercises online of F# and I have gotten stuck on this.

So if i have ['a';'b';'c'] and [‘a’;’x’;’y’;’b’;’c’;’e’] it should return true but if I have something like ['a';'b';'c'] and [‘a’;’x’;’a’;’y’;’c’;’e’] it should be false.

Was it helpful?

Solution

You could create a set of these values, and use Set.IsProperSubsetOf<'T> to check for whether the values are contained.

let list1 = ['a';'b';'c']
let list2 = ['a';'x';'y';'b';'c';'e']
let list3 = ['a';'x';'a';'y';'c';'e']

let first = Set.ofList list1
let second = Set.ofList list2
let third = Set.ofList list3

let containedInSecond = first.IsProperSubsetOf second
let containedInThird = first.IsProperSubsetOf third

Running this in F# Interactive will print (for the last two lines) your desired results:

val containedInSecond : bool = true
val containedInThird : bool = false
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top