F#中for ... in循环

2018-12-15 10:05 更新
此循环结构用于遍历可枚举集合中的模式匹配,例如范围表达式,序列,列表,数组或支持枚举的其他构造。

语法

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

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号