F#:Seq.forall奇怪吗?

Mil*_*oDC 1 f# seq

鉴于let ra = ResizeArray<int> ():

Seq.forall (fun i ->
                    let q = i % 2
                    if 0 = q then ra.Add i
                    true ) <| seq { 1..10 }
Run Code Online (Sandbox Code Playgroud)

如果我这样做,ra.Count退货5.

Seq.forall (fun i ->
                    let q = i % 2
                    if 0 = q then ra.Add i
                    0 = q ) <| seq { 1..10 }
Run Code Online (Sandbox Code Playgroud)

如果我这样做,ra.Count退货0.

那么,除非lambda函数的每次迭代求值为true,否则实际上函数中没有任何代码被执行,或者什么?

这里发生了什么?

Joh*_*zen 5

使用值"false"来停止Seq.forall处理其他元素的功能.

由于1 % 2 = 0为false,因此会在第一次迭代时停止评估.

  • @MiloDC - 您考虑过"Seq.iter"吗? (4认同)