These two are equivalent:
let f(x) =
10
let g = fun(x) ->
10
I think? They seem to do the same thing, but are there any cases where the behavior of the two would vary? I find the second version useful (even if more verbose) because you can use the <| and << operators to implement python-style decorator patterns; is there any case where I have to use the first version?
Furthermore, I fully understand how the second one works (the stuff on the right is just a function expression, which I dump into g) but how about the first one? Is there some compiler trick or special case that converts that syntax from a simple assignment statement into a function definition?
As Brian already answered, the two are equivalent. Returning
funinstead of declaring function usingletmakes difference if you want to do something (i.e. some initialization) before returning the function.For example, if you wanted to create function that adds random number, you could write:
Here, the function
f1creates newRandominstance every time it is executed, butf2uses the same instance ofrndall the time (sof2is better way of writing this). But if you returnfunimmediately, then the F# compiler optimizes the code and the two cases are the same.