// define let f x = if x %2=0 then "Even" else "Odd"
// invoke f 1 Odd
1 2 3 4 5 6 7 8
let g x = if x =1then"One" elif x=2then"Two" elif x=3then"Three" else"Other"
// Other g 11
练习:
string -> int
“one” -> 1
“two” -> 2
“three” -> 3
_ -> 0
match with
1 2 3 4 5 6 7
let f x = match (x %2) with |0->"Even" |1->"Odd"
f 2 // Even
1 2 3 4 5 6 7 8 9
let g x = match x with |1->"One" |2->"Two" |3->"Three" | _ ->"Other"
g 7 // Other
1 2 3 4 5 6
let h x = match x with |1|2|3->"OneTwoThree" | _ ->"Others"
h 2
1 2 3 4 5 6 7
let compare x y = match x with | _ when x<y ->"smaller" | _ when x>y ->"larger" | _ ->"equal"
compare 12
练习 h: a: int -> b: int -> unint h: a: int * b: int -> unit if a is odd then print “a = b + c” if a is even then print “a = b - c”
1 2 3 4
let g x = match x with | (var1, var2) when var1 %2=0->printfn"%d = %d + (%d)" var1 var2 (var1 + var2) | (var1, var2) when var1 %2=1->printfn"%d = %d - %d" var1 var2 (var1 - var2)