Monday, January 21, 2013

Tips for F# pattern match (1)

(In learning from "Programming F# 3.0, 2nd Edition")

When you want to deal "an another value" in F# pattern match, there are two ways - "wild card" and "value capture".
let greet name =
  match name with
    | "Robert" -> printfn "Hello, Bob"
    | "William" -> printfn "Hello, Bill"
    | _ -> printfn "Hello, %s" name;;
let greet name =
  match name with
    | "Robert" -> printfn "Hello, Bob"
    | "William" -> printfn "Hello, Bill"
    | x -> printfn "Hello, %s" x;;

What is different? The difference is just binding "an another value" to an new variable or not.

(continue to phosphorescence: Tips for F# pattern match (2))

No comments:

Post a Comment