Difference between let, fun and function in F #

I am studying F # and I can’t understand what is the difference between let, fun and function, and my tutorial doesn’t really explain it. As an example:

let s sym = function | V x -> Map.containsKey x sym | A(f, es) -> Map.containsKey f sym && List.forall (s sym) es;; 

Can't I write this without the function keyword? Or could I write this with fun instead of function ? And why do I have to write let when I saw some examples where you write

 fun sx = ... 

What is the difference really?

+5
source share
2 answers

I think you really should ask MSDN , but in a nutshell:

let binds the value to the character. The value can be a simple type of type int or string , but it can also be a function. FP functions have values ​​and can be processed in the same way as those types.

fun is a keyword that introduces an anonymous function - think of a lambda expression if you are familiar with C #.

These are two important things, in the sense that all the other habits that you saw can be considered syntactic sugar for the two. So, to define a function, you can say something like this:

  let myFunction = fun firstArg secondArg -> someOperation firstArg secondArg 

And this is a very clear way to say it. You declare that you have a function, and then bind it to the myFunction symbol.

But you can save yourself some typing by simply combining an anonymous function declaration and binding it to a character with let :

  let myFunction firstArg secondArg = someOperation firstArg secondArg 

What makes function bit more complicated is that you combine an anonymous declaration with one argument with a match expression, matching an implicit argument. So these two equivalents:

 let myFunction firstArg secondArg = match secondArg with | "foo" -> firstArg | x -> x let myFunction firstArg = function | "foo" -> firstArg | x -> x 

If you are just starting out with F #, I avoid this. It has its own capabilities (mainly to provide shorter functions of a higher order for cards / filters, etc.), but it leads to the fact that the code becomes less readable at first sight.

+12
source

These things are shortcuts to each other.

The most fundamental thing is let . This keyword names the materials:

 let name = "stuff" 

More technically, the let keyword defines an identifier and associates it with a value:

 let identifier = "value" 

After that, you can use the name and identifier words in your program, and the compiler will know what they mean. Without let there would be no way to name the material, and you would always have to write all your data into a string, instead of referring to chunks of that name.

Now the meanings have different tastes. There are "some string" strings, there are 42 integers, 5.3 floating-point numbers, true , etc. Feature of function is function. Functions are also values, much like strings and numbers. But how do you write a function? You use double quotes to write a string, but what about a function?

Well, to write a function, you use the special word fun :

 let squareFn = fun x -> x*x 

Here I used the let keyword to define the squareFn identifier and bind this identifier to the value of the function view. Now I can use the word squareFn in my program, and the compiler finds out that whenever I use it, I mean the function fun x -> x*x .

This syntax is technically sufficient, but not always convenient to write. Therefore, to make it shorter, let binding takes on additional responsibility and provides a shorter way to write above:

 let squareFn x = x*x 

That should do it for let vs fun .

Now the function keyword is just a short form for fun + match . The notation function equivalent to the notation fun x -> match x with , period.

For example, the following three definitions are equivalent:

 let f = fun x -> match x with | 0 -> "Zero" | _ -> "Not zero" let fx = // Using the extra convenient form of "let", as discussed above match x with | 0 -> "Zero" | _ -> "Not zero" let f = function // Using "function" instead of "fun" + "match" | 0 -> "Zero" | _ -> "Not zero" 
+9
source

All Articles