F#中for ... in循環(huán)

2018-12-15 10:05 更新
此循環(huán)結構用于遍歷可枚舉集合中的模式匹配,例如范圍表達式,序列,列表,數(shù)組或支持枚舉的其他構造。

語法

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
當編譯和執(zhí)行程序時,它會產生以下輸出 

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

公眾號
微信公眾號

編程獅公眾號