Nov 17, 2010

Pattern matching shorthand in function definition

Pattern matching is powerful construct in F#, but sometime it is confusing to beginner, below is pattern matching shorthand in function definition. The paramter is implied in the matching pattern, essentially you can rewrite it as below.


let listOfList = [[2; 3; 5]; [7; 11; 13]; [17; 19; 23; 29]]

let rec concatList =
    function head :: tail -> head @ (concatList tail)
            | [] -> []

//let rec concatList l =
//    match l with
//    | head :: tail -> head @ (concatList tail)
//    | [] -> []

let primes = concatList listOfList;

printfn "%A" primes