F# - for...in 循环
此循环构造用于迭代可枚举集合中模式的匹配项,例如范围表达式、序列、列表、数组或支持枚举的其他构造。
语法
for pattern in enumerable-expression do body-expression
示例
下面的程序说明了这个概念 −
// Looping over a list. let list1 = [ 10; 25; 34; 45; 78 ] for i in list1 do printfn "%d" i // Looping over a sequence. let seq1 = seq { for i in 1 .. 10 -> (i, i*i) } for (a, asqr) in seq1 do printfn "%d squared is %d" a asqr
当您编译并执行该程序时,它会产生以下输出 −
10 25 34 45 78 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 6 squared is 36 7 squared is 49 8 squared is 64 9 squared is 81 10 squared is 100