|
이 문서는 수동으로 번역한 것입니다. 원본 텍스트를 보려면 포인터를 문서의 문장 위로 올리십시오. 추가 정보
|
번역
원본
|
일치 식(F#)
// 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
| ...
|
|
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
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
// 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