模式匹配

This commit is contained in:
Sanchime 2022-02-24 20:56:52 +08:00
parent 6665adb75b
commit 80614a0a3b
2 changed files with 50 additions and 0 deletions

View File

@ -15,5 +15,6 @@
<Compile Include="CurryingFunction.fs" />
<Compile Include="Loop.fs" />
<Compile Include="TypeCreating.fs" />
<Compile Include="PatternMatching.fs" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,49 @@
module PatternMatching
// match value_to_match with | patterns
//
match 1 with
| 1 -> printfn "1"
| 2 -> printfn "2"
| 3 -> printfn "3"
| _ -> printfn ""
//
match "one" with
| "one" -> printfn "one"
| "two" -> printfn "two"
| _ -> printfn ""
type Shape = Square | Circular
//
match Square with
| Square -> printfn"Square"
| Circular -> printfn"Circular"
//
match 1 with
| 1 | 2 | 3 -> printfn "123"
| _ -> printfn""
// cons
let rec sum_of_list list =
match list with
| [] -> 0
| [x] -> x
| x::xs -> x + sum_of_list xs
[] |> sum_of_list |> printfn "%A"
[1] |> sum_of_list |> printfn "%A"
[1; 2; 3] |> sum_of_list |> printfn"%A"
// function,
1
|> function
| 1 -> printfn "1"
| 2 -> printfn "2"
| 3 -> printfn "3"
| _ -> printfn ""