Jun 29, 2011

f# essential - part 3 matching shortcut

F# provide a shortcut to define a function which purely use input parameter for matching purpose. For example the following are equal.

let printInt =
    function 1 -> printfn "1"
            | 2 -> printfn "2"
            | _ -> printfn "something else"

let printInt number =
    match number with 1 -> printfn "1"
                      | 2 -> printfn "2"
                      | _ -> printfn "something else"

let printInt number =
    match number with
        | 1 -> printfn "1"
        | 2 -> printfn "2"
        | _ -> printfn "something else"