次の方法で共有


match 式

match式は、式と一連のパターンの比較に基づく分岐制御を提供します。

構文

// Match expression.
match test-expression with
| pattern1 [ when condition ] -> result-expression1
| pattern2 [ when condition ] -> result-expression2
| ...

// Pattern matching function.
function
| pattern1 [ when condition ] -> result-expression1
| pattern2 [ when condition ] -> result-expression2
| ...

注釈

パターン マッチング式を使用すると、テスト式と一連のパターンの比較に基づく複雑な分岐が可能になります。 match式では、テスト式が各パターンと順番に比較され、一致が見つかると、対応する結果式が評価され、結果の値が一致式の値として返されます。

前の構文で示したパターン マッチング関数は、パターン マッチングが引数に対してすぐに実行されるラムダ式です。 前の構文で示したパターン マッチング関数は、次と同じです。

fun arg ->
    match arg with
    | pattern1 [ when condition ] -> result-expression1
    | pattern2 [ when condition ] -> result-expression2
    | ...

ラムダ式の詳細については、「 ラムダ式: fun キーワード」を参照してください。

パターンのセット全体は、入力変数の可能なすべての一致をカバーする必要があります。 多くの場合、ワイルドカード パターン (_) を最後のパターンとして使用して、以前に一致しなかった入力値と一致させます。

次のコードは、 match 式を使用する方法の一部を示しています。 使用可能なすべてのパターンのリファレンスと例については、「 パターン マッチング」を参照してください。

let list1 = [ 1; 5; 100; 450; 788 ]

// Pattern matching by using the cons pattern and a list
// pattern that tests for an empty list.
let rec printList listx =
    match listx with
    | head :: tail -> printf "%d " head; printList tail
    | [] -> printfn ""

printList list1

// Pattern matching with multiple alternatives on the same line.
let filter123 x =
    match x with
    | 1 | 2 | 3 -> printfn "Found 1, 2, or 3!"
    | a -> printfn "%d" a

// The same function written with the pattern matching
// function syntax.
let filterNumbers =
    function | 1 | 2 | 3 -> printfn "Found 1, 2, or 3!"
             | a -> printfn "%d" a

パターンでのガード

when句を使用して、変数がパターンに一致するために満たす必要がある追加の条件を指定できます。 このような句は 、ガードと呼ばれます。 when キーワードに続く式は、そのガードに関連付けられているパターンに一致しない限り評価されません。

次の例は、ガードを使用して変数パターンの数値範囲を指定する方法を示しています。 ブール演算子を使用して複数の条件を組み合わせることに注意してください。

let rangeTest testValue mid size =
    match testValue with
    | var1 when var1 >= mid - size/2 && var1 <= mid + size/2 -> printfn "The test value is in range."
    | _ -> printfn "The test value is out of range."

rangeTest 10 20 5
rangeTest 10 20 10
rangeTest 10 20 40

リテラル以外の値はパターンで使用できないため、入力の一部を値と比較する必要がある場合は、 when 句を使用する必要があります。 これは次のコードに示されています。

// This example uses patterns that have when guards.
let detectValue point target =
    match point with
    | (a, b) when a = target && b = target -> printfn "Both values match target %d." target
    | (a, b) when a = target -> printfn "First value matched target in (%d, %d)" target b
    | (a, b) when b = target -> printfn "Second value matched target in (%d, %d)" a target
    | _ -> printfn "Neither value matches target."
detectValue (0, 0) 0
detectValue (1, 0) 0
detectValue (0, 10) 0
detectValue (10, 15) 0

和集合パターンがガードでカバーされている場合、ガードは、最後のものだけでなく、すべてのパターンに適用されます。 たとえば、次のコードを考えると、ガード when a > 41A aB aの両方に適用されます。

type Union =
    | A of int
    | B of int

let foo() =
    let test = A 40
    match test with
    | A a
    | B a when a > 41 -> a // the guard applies to both patterns
    | _ -> 1

foo() // returns 1

こちらも参照ください