F# 创建 x 的倍数列表?

发布于 2024-12-07 10:04:31 字数 274 浏览 0 评论 0原文

我想创建一个数字的倍数列表。例如[2; 4; 6; 8; 10] 将是 0 到 10 之间 2 的倍数。

我如何动态创建这样一个 x 倍数的列表?是否可以在不设置上限的情况下做到这一点?

一种方法是创建一个介于 0 和某个疯狂的大数字之间的列表,然后使用 mod 函数对其进行过滤。尝试测试这一点时,创建一个 0 到某个疯狂大数字的列表会导致内存不足异常(经过 30 秒左右的等待后)。

我觉得 F# 有一些超级简单且很棒的方法来构建这样的列表,但我还是个新手,还不知道它是什么。帮助?

I want to create a list that is the multiples of a number. For example [2; 4; 6; 8; 10] would be the multiples of 2 between 0 and 10.

How would I dynamically create such a list of the multiples of x? Is it possible to do it without setting an upper bound?

One way to do it would be to create a list between 0 and some crazy large number and then filter it using the mod function. Trying to test this, creating a list of 0 to some crazy large number caused an out of memory exception (after a 30 second or so wait).

I feel like F# has some super simple and awesome way to build such a list but I'm too much of a newb to know what it is, yet. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寻梦旅人 2024-12-14 10:04:31

这会产生一个无限的倍数序列:

let multiples n = Seq.unfold (fun i -> Some(i, i + n)) n

multiples 3 |> Seq.take 3 //seq [3; 6; 9]

代码稍多,但速度更快:

let multiples n =
  let rec loop i =
    seq {
      yield i
      yield! loop (i + n)
    }
  loop n

它基本上相当于以下 C#:

static IEnumerable<int> Multiples(int n) {
    int i = n;
    while (true) {
        yield return i;
        i += n;
    }
}

This produces an infinite sequence of multiples:

let multiples n = Seq.unfold (fun i -> Some(i, i + n)) n

multiples 3 |> Seq.take 3 //seq [3; 6; 9]

This is a bit more code, but faster:

let multiples n =
  let rec loop i =
    seq {
      yield i
      yield! loop (i + n)
    }
  loop n

It's basically equivalent to the following C#:

static IEnumerable<int> Multiples(int n) {
    int i = n;
    while (true) {
        yield return i;
        i += n;
    }
}
月隐月明月朦胧 2024-12-14 10:04:31

序列(IEnumerables)在这里给出了你想要的惰性:

let multiplesOfN n =
    seq {
        for i in 1 .. 1000000 do
            yield i * n
    }

let first6multsof3 = 
    multiplesOfN 3 |> Seq.take 6

printfn "%A" (first6multsof3 |> Seq.toList)

或者使用你的过滤器模型策略:

seq { 1 .. 1000000} |> Seq.filter (fun x -> x%3=0) |> Seq.take 6 |> Seq.toList 

Sequences (IEnumerables) give the laziness you want here:

let multiplesOfN n =
    seq {
        for i in 1 .. 1000000 do
            yield i * n
    }

let first6multsof3 = 
    multiplesOfN 3 |> Seq.take 6

printfn "%A" (first6multsof3 |> Seq.toList)

or with your filter-mod strategy:

seq { 1 .. 1000000} |> Seq.filter (fun x -> x%3=0) |> Seq.take 6 |> Seq.toList 
蔚蓝源自深海 2024-12-14 10:04:31
[ firstValue..Step..endValue]

[ 2..2..10] => [2; 4; 6; 8; 10]

其他方式

Seq.initInfinite id |> Seq.map (((+) 1) >> ((*) 2))
[ firstValue..Step..endValue]

[ 2..2..10] => [2; 4; 6; 8; 10]

other way

Seq.initInfinite id |> Seq.map (((+) 1) >> ((*) 2))
情绪失控 2024-12-14 10:04:31
List.init 10 ((*) 3)
val it : int list = [0; 3; 6; 9; 12; 15; 18; 21; 24; 27]

您可以使用参数和 Seq.skip 来获得您需要的任何内容。

例如,对于 [2; 4; 6; 8; 10]

List.init 6 ((*) 2)
|> List.tail

或者:

List.init 6 ((*) 2)
|> Seq.skip 1
|> List.ofSeq
List.init 10 ((*) 3)
val it : int list = [0; 3; 6; 9; 12; 15; 18; 21; 24; 27]

You can play with the arguments and Seq.skip to get whatever you need.

For example, for [2; 4; 6; 8; 10]:

List.init 6 ((*) 2)
|> List.tail

Or:

List.init 6 ((*) 2)
|> Seq.skip 1
|> List.ofSeq
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文