1. If statement, F # the return value does not need to explicitly write the return type. When writing the if statement and other process controls, you must control the return value information, for example, if statement blocks and else statement blocks must match the return value types. If statements are returned, else statements are not returned, and only one single condition is returned, if only one If statement is displayed and the returned value is returned, but no else statement is returned, if the if statement is not met, the returned value may not be returned.
Let fun1 x = // define the fun1 Method for returning a string
If X> 10 then "the input value is greater than 10" // when x> 10, the system returns information greater than 10.
Else "the input value is less than or equal to 10" // conversely, the returned value is less than or equal to 10, because the returned else statement block cannot be missing. Otherwise, if the if condition is met, the returned value may not be met.
Printfn "% s" (fun1 10)
Let num1 = // define a method num1 without parameters. The return type is integer.
Let X = 10
If X> 10 then
10
Else
X
Printfn "% I" num1
Let outputnum x = // defines a method without a return value.
If X> = 10 then printfn "% I" 10
Else printfn "% I" x
Outputnum 10
Let outputnum2 x = // defines a method without a return value.
If X> = 10 then printfn "% I" 10
Outputnum2 10
Let checknum x = // define a method without return values to determine whether the input parameter value is equal to 10
If X = 10 then printfn "% s" "X is equal to 10" // (make sure that the single equals sign is used)
The value of else printfn "% s" "X is not equal to 10"
Checknum 10
2. Type constraints (or type comments) enclose parameters or identifiers in parentheses and declare specific data types using the type method.
Let (X: INT) = 1 // declarative integer identifier x
Let Hello (Name: string) = // The parameter name of the hello method is of the string type.
Printfn "Hello % s" Name
Let (names: List <string>) = ["A"; "B"; "C"; "D"]
Let add (X: INT) (Y: INT) = x + y
3. pattern matching is similar to the C # Switch statement.
Let rec Luc x =
Match X
| X when x <= 0-> failwith "value must be greater than 0"
| 1-> 1
| 2-> 3
| X-> LUC (X-1) + LUC (x-1-2)
Let printout x =
Match X
| X-> printf "% s" x
Printout ""
Let getlanguage (Lid: INT) =
Match lid
| 1-> "Mandarin"
| 2-> "English"
| Lid when lid <= 0-> failwith "error"
| Lid-> "unknown"
Printfn "% s" (getlanguage-3)
Let getmonthdays x = // when the results of Multiple matching items are the same, it can be used continuously | to separate multiple cases. _ wildcards similar to SQL indicate other matching values
Match X
| 1 | 3 | 5 | 6 | 8 | 10 | 12-> 31
| 4 | 7 | 9 | 11-> 30
| 2-> 28
| _-> Failwith "Incorrect month" // X _ can all match
Printfn "% I" (getmonthdays 2)
Printfn "% I" (getmonthdays 5)
Printfn "% I" (getmonthdays 7)
Let getmonthdays2 m y =
Let checkrun year =
(Year % 4 = 0 | year % 100 = 0) & (Year % 400 <> 0)
Match m
| 1 | 3 | 5 | 6 | 8 | 10 | 12-> 31
| 4 | 7 | 9 | 11-> 30
| 2 when (checkrun y) = true-> 29
| 2 when (checkrun y) = false-> 28
| _-> Failwith "Incorrect month" // X _ can all match
Printfn "% I" (getmonthdays2 13 2000)
4. List and pattern match (list and pattern match). When the list matches the pattern, you can use head and tail to represent the list header and Column End, when you understand this, you can regard the list as a linked list head, indicating the head node. Tail indicates the end chain S except the head node, for example, [1; 2; 3; 4] The head is 1, tail is [2; 3; 4], for example, [1] Head is 1, and tail is []
Let outputlisthead L =
Match L
| HEAD: tail-> printfn "% A" head // when matching, case should be a list, head: tail, head and tail are connected to a list ,(:: the connector returns a new list based on the list element and list connection. @ is used to connect two lists)
Outputlisthead [1; 2; 3; 4] // output 1
Let rec outputlist (L: List <int>) = // elements in the recursive output list. The output list is equal to the output list header and then the output end list.
Match L
| HEAD: tail-> printfn "% A" head; outputlist tail;
| []-> Printfn "% s" "end ";
Outputlist [For I in 1... 2... 9-> I]