F # explicit match with function syntax

Sorry for the vague name, but part of this question is what these two syntax styles call:

let foo1 x = match x with | 1 -> "one" | _ -> "not one" let foo2 = function | 1 -> "one" | _ -> "not one" 

The other part is what is the difference between the two, and when would I like to use one or the other?

+64
syntax f #
Dec 03 '09 at 10:23
source share
8 answers

The matching version is called the pattern matching expression. The version of the function is called the "pattern matching function". Found in section 6.6.4 spec .

Using one over the other is a matter of style. I prefer to use only the functional version when I need to define a function that is just a match expression.

+39
Dec 03 '09 at 17:36
source share

About the second syntax is that when used in lambda it can be a little more concise and readable.

 List.map (fun x -> match x with | 1 -> "one" | _ -> "not one") [0;1;2;3;1] 

against

 List.map (function 1 -> "one" | _ -> "not one") [0;1;2;3;1] 
+58
Dec 03 '09 at 13:01
source share

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 ??" 
+14
Dec 03 '09 at 20:13
source share

They do the same in your case - the function keyword acts as a combination of the fun keyword (to create an anonymous lambda), followed by the match keyword.

So technically the two are the same, with the addition of fun :

 let foo1 = fun x -> match x with | 1 -> "one" | _ -> "not one" let foo2 = function | 1 -> "one" | _ -> "not one" 
+10
Dec 03 '09 at 10:35
source share

Just for the sake of completeness, I just got to page 321 Expert FSharp :

"Note. Listing 12-2 uses the expression form function pattern-rules -> expression . This is equivalent to (fun x -> match x with pattern-rules -> expression) and is especially convenient as a way to define functions that work directly on discriminatory unions."

+6
Dec 08 '09 at 7:03
source share

a function allows only one argument, but allows matching patterns, and fun is a more general and flexible way to define a function. Have a look here: http://caml.inria.fr/pub/docs/manual-ocaml/expr.html

+4
Jan 01 '10 at 22:23
source share

Two syntaxes are equivalent. Most programmers choose one or the other and then use it sequentially.

The first syntax remains more readable when a function takes several arguments before starting work.

+2
Dec 03 '09 at 10:35
source share

This is an old question, but I will throw my $ 0.02.

All in all, I like the version of match since I came from the world of Python, where “explicit is better than implicit”.

Of course, if parameter type information is not needed, the version of function cannot be used.

OTOH I like the argument made by Stringer , so I will start using function in simple lambdas.

0
Jul 27 '17 at 10:48
source share



All Articles