【发布时间】:2021-02-12 08:07:15
【问题描述】:
我从 Fsharp 开始,我有这个问题。 假设我有两个长度相同的列表 a 和 b,我同时遍历这些列表,并在每一步测试 a 和 b 的条件,并使用先前的微积分的结果。如果此测试失败,则无需继续。 我写了这段代码:
let mutable (i : int) = 0
let mutable (good : bool) = true
let mutable (previous : int) = 0
while good && i < len do
good <- test a.[i] b.[i] previous
previous <- my_func a.[i] b.[i]
i <- i + 1
我看到这段代码要好得多:
List.zip a b |> List.fold (fun (x, y) (a,b) -> (p && test a b y, my_func a b) (true, 0)
但是,对于我的代码,一旦测试失败,过程就完成了,而不是第二个代码。 有没有办法,使用第二个代码的设计来停止进程?
谢谢
【问题讨论】:
标签: f#