The version of the function is a short hand for the syntax of full correspondence in the special case when the correspondence operator is an entire function and the function has only one argument (the number of tuples is considered one). If you want to have two arguments, you need to use the full match syntax *. You can see this in the following two functions.
//val match_test : string -> string -> string let match_test xy = match x, y with | "A", _ -> "Hello A" | _, "B" -> "Hello B" | _ -> "Hello ??" //val function_test : string * string -> string let function_test = function | "A", _ -> "Hello A" | _, "B" -> "Hello B" | _ -> "Hello ??"
As you can see, the correspondence version takes two separate arguments, while the version of the function takes one alternating argument. I use the version of the function for most functions of a single argument, since I found that the syntax of the function looks cleaner.
* If you really want the function version to have the correct type signature, but in my opinion it looks pretty ugly - see the example below.
//val function_match_equivalent : string -> string -> string let function_match_equivalent xy = (x, y) |> function | "A", _ -> "Hello A" | _, "B" -> "Hello B" | _ -> "Hello ??"
Joshua Dec 03 '09 at 20:13 2009-12-03 20:13
source share