문제

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.

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top