【问题标题】:f# for loop continuef# for 循环继续
【发布时间】:2021-06-01 12:39:18
【问题描述】:

我想在else 之后继续循环,所以列表将被填充,然后返回列表

let isPoli num =
    let x = string num
    let fhf = x.Substring(0, (int) (x.Length / 2))

    let shf =
        x.Substring((int) (x.Length / 2), (int) (x.Length / 2))
        |> Seq.rev
        |> Seq.toArray
        |> System.String

    if fhf = shf then true else false


let biggestPoliProduct (range: list<int>) =
    let acc = []

    for i in range do
        for j in range do
            if isPoli (i * j) then (i * j) :: acc
            else []
    acc

biggestPoliProduct [ 1 .. 100 ]

【问题讨论】:

  • 表达式(i * j) :: acc 不会修改acc 的值。它会创建一个全新的列表,然后什么也不做。

标签: for-loop f# f#-interactive


【解决方案1】:

这对list comprehension来说是个好情况:

let biggestPoliProduct (range: list<int>) =
    [
        for i in range do
            for j in range do
                if isPoli (i * j) then
                    yield (i * j)
    ]

不需要else 分支。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多