确定列表1中的元素是否包含在列表2 F#中

时间:2014-05-05 23:12:12

标签: list f#

我一直在搞乱F#的在线编程练习,我一直坚持这个。

所以如果我有['a';'b';'c']和['a';'x';'y';'b';'c';'e']它应该返回true但如果我有['a';'b';'c']和['a';'x';'a';'y';'c';'e']这样的东西应该是假的。

1 个答案:

答案 0 :(得分:-1)

您可以创建一组这些值,并使用Set.IsProperSubsetOf<'T>检查值是否包含在内。

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

在F#Interactive中运行此功能将打印(最后两行)您想要的结果:

val containedInSecond : bool = true
val containedInThird : bool = false