Creating Set in F# with elements from 1111 to 6666

2019-10-03 04:14发布

问题:

How do i create a Set in F# with elements from 1111 to 6666 without any values being 0, 7 or higher.

E.g. [1111,1112,1113,1114,1115,1116,1121] I'd like to make it a set.

Thanks in advance

回答1:

You can use a sequence comprehension:

let values = seq {
    for i in 1110 .. 10 .. 6660 do
      for j in 1 .. 6 do
        yield i + j
    }

and create a set using Set.ofSeq e.g.

let s = Set.ofSeq values


回答2:

There must be an easier way than:

let values = seq {
for a in 1000 .. 1000 .. 6000 do
for b in 100 .. 100 .. 600 do
for c in 10 .. 10 .. 60 do
for d in 1 .. 6 do
    yield a + b + c + d
}


标签: f# set sequence