F # Learning Notes (function Basics 2 pattern matching)

Source: Internet
Author: User

Pattern-Matching Expressions:

[<EntryPoint>]let main argv=Let eval x=match x with|5-"Excellent"|4-"Liang"|3-"in"|_"Poor"Let y= eval4Let s= Array.map Eval [|3;2;1;5|]     0 //returns an integer exit code

Here in the expression Array.map function, each element of the array is applied to the eval for pattern matching.

To use conditional expressions in pattern matching, define a segmented function as follows:

[<entrypoint>]let main argv=Let f x=match x with| _ When (X <-1.0),1.0+x| _ When (x <=1.0),0.0| _,1.0-x PRINTFN"f ( -2) =%f F (0.5) =%f F (3) =%f"(F-2.5) (f0.5) (f3.0)    0 //returns an integer exit code

Multi-parameter functions:

[<entrypoint>=              = match x, y with |         (1, _)- y        | (_,12*x        2*x+y    "%i"  21)    0//  return integer exit code

Multiple modes can also be combined with a vertical bar:

Let main argv =Let check pwd=match pwd with|"AAA"|"AAA"-"Password is correct"| _,"Password Error"Let grade R=match R with| (S, _, X) when x >= -Printfn"%s through"s| (S,'female', _) & (_, _, X) when x >= -Printfn"%s through"s| (S, _, _)-PRINTFN"%s did not pass"S ("Li Ming",'male', the) |>Grade"AAA"|> Check |> PRINTFN"%s"    "AAA"|> Check |> PRINTFN"%s"    0 //returns an integer exit code

Use a pipe character to apply a function using a & representation or, I think it's more graceful to write.

There are two important issues to be aware of when using pattern matching:

1. Pattern matching combination to complete

2. Prevent a pattern that will never be matched

Record union, optional type match

Record matching:

Type FullName = {first:string; Last:string; } [<EntryPoint>]let main argv=Let IsFamily x s=match x with| {first = _; Last = fi} when fi = Strue| _,falseLet fn="Strong"Let B1= IsFamily {first ="Xiao Qiang"; Last ="Strong"} fn Let B2= IsFamily {first ="Ouyang"; Last ="Yang"} fn B1. ToString ()|> PRINTFN"%s"B2. ToString ()|> PRINTFN"%s"     0 //returns an integer exit code

Union type Matching:

Type Vehicle = | Car | Truck |bus[<EntryPoint>]let main argv=Let speed x=match x with| Car -| Truck -| Bus- theLet mutable v1=Car v1|> Speed |> PRINTFN"%i"v1<-Bus v1|> Speed |> PRINTFN"%i"    0 //returns an integer exit code

The following is the use of pattern matching and union types to convert the length values of various units into millimeters:

Type Length = | Meter offloat| Millimeter offloat| Foot offloat| Inch offloat[<EntryPoint>]let main argv=Let Tomm x=match x with| Meter X-X *1000.0| Millimeter Xx| Foot X-X *304.8| Inch X-X *25.4Let l1,l2,l3= Meter3.2, Foot0.5, Inch8.7PRINTFN"l1:%.2fmm, L2:%.2FMM, L3:%.2FMM"( tomm L1) (Tomm L2) (Tomm L3)0 //returns an integer exit code

The optional type matches, and the optional type itself is a union type, so you can also use pattern matching in this way:

[<entrypoint>]let main argv=Let valid x=match x with| Some (1) | Some (2),"inferior lattice"| Some (x)"Pass"| None-"Lack of test"Let x1, x2= Some (5), None let Y1, y2=valid x1, valid x2 y1|> PRINTFN"%s"y2|> PRINTFN"%s"       0 //returns an integer exit code

F # Learning Notes (function Basics 2 pattern matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.