# ループ: for...in 式Loops: for...in Expression

このループ構造は、列挙可能なコレクション内のパターンの一致を反復処理するために使用されます。たとえば、範囲式、シーケンス、リスト、配列、または列挙をサポートするその他の構造体です。This looping construct is used to iterate over the matches of a pattern in an enumerable collection such as a range expression, sequence, list, array, or other construct that supports enumeration.

## 構文Syntax

for pattern in enumerable-expression do
body-expression


## コメントRemarks

..演算子を使用して範囲を表現する場合は、次の構文を使用できます。When you express a range by using the .. operator, you can use the following syntax.

パターンに一致した値は、本文の式でも使用できます。Values matched in the pattern can also be used in the body expression.

// Looping over a list.
let list1 = [ 1; 5; 100; 450; 788 ]
for i in list1 do
printfn "%d" i


1
5
100
450
788


let seq1 = seq { for i in 1 .. 10 -> (i, i*i) }
for (a, asqr) in seq1 do
printfn "%d squared is %d" a asqr


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


let function1() =
for i in 1 .. 10 do
printf "%d " i
printfn ""
function1()


Function1 の出力は次のようになります。The output of function1 is as follows.

1 2 3 4 5 6 7 8 9 10


let function2() =
for i in 1 .. 2 .. 10 do
printf "%d " i
printfn ""
function2()


の出力はfunction2次のとおりです。The output of function2 is as follows.

1 3 5 7 9


let function3() =
for c in 'a' .. 'z' do
printf "%c " c
printfn ""
function3()


の出力はfunction3次のとおりです。The output of function3 is as follows.

a b c d e f g h i j k l m n o p q r s t u v w x y z


let function4() =
for i in 10 .. -1 .. 1 do
printf "%d " i
printfn " ... Lift off!"
function4()


の出力はfunction4次のとおりです。The output of function4 is as follows.

10 9 8 7 6 5 4 3 2 1 ... Lift off!


let beginning x y = x - 2*y
let ending x y = x + 2*y

let function5 x y =
for i in (beginning x y) .. (ending x y) do
printf "%d " i
printfn ""

function5 10 4


この入力にfunction5よるの出力は次のとおりです。The output of function5 with this input is as follows.

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18


let mutable count = 0
for _ in list1 do
count <- count + 1
printfn "Number of elements in list1: %d" count


Number of elements in list1: 5


Noteは、シーケンスfor...in式やその他のコンピュテーション式で使用できます。その場合は、 for...in式のカスタマイズされたバージョンが使用されます。Note You can use for...in in sequence expressions and other computation expressions, in which case a customized version of the for...in expression is used. 詳細については、「シーケンス非同期ワークフロー、およびコンピュテーション式」を参照してください。For more information, see Sequences, Asynchronous Workflows, and Computation Expressions.