当前位置: 首页 > 文档资料 > F# 中文教程 >

for … in expression

优质
小牛编辑
129浏览
2023-12-01

此循环结构用于迭代可枚举集合中模式的匹配,例如范围表达式,序列,列表,数组或支持枚举的其他构造。

语法 (Syntax)

for pattern in enumerable-expression do
   body-expression

例子 (Example)

以下计划说明了这一概念 -

// 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