Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use 'function' instead of 'fun'? [duplicate]

Tags:

f#

Possible Duplicate:
F# explicit match vs function syntax

Hello,

I am learning F# and am confused by 'fun' and 'function' keywords. My understanding is that these are sort of the same thing.

// Use 'fun'
let testFunction1 = fun argument -> match argument with
                                    | Some(x) -> x
                                    | None    -> 0

// Use 'function'
let testFunction2 = function
                    | Some(x) -> x
                    | None    -> 0

Is 'function' just shorthand for "fun x -> match x with"? Is there any runtime / optimization difference between the two? Why would I prefer to use one over the other?

As it stands, 'function' just seems to make code harder to read. Am I missing something?

like image 582
Vault Boy Avatar asked Jan 01 '11 22:01

Vault Boy


1 Answers

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

like image 109
kamaci Avatar answered Sep 20 '22 13:09

kamaci