I am having trouble with the following:
let safeDiv x y =
match (x,y) with
| (_, Some 0) -> None
| (Some xx, Some yy) -> Some (xx/yy)
| _ -> None
When I go to run this simple function in the interactive window of Visual Studio like so:
safeDiv 4 2
I get the following error...
This expression was expected to have type int option but here has type int.
Could it be I'm meant to use safeDiv
Some(4)
Some(2)
? This doesn't work either...
Ok, this is overkill but I actually did something similar to this recently.
First I defined a computation expression builder for the option type:
And then I defined a function sub of type float -> float -> float option
And finally I used the OptionBuilder to define saveDiv as float option -> float option -> float option
You can read more about computation expressions on wikibooks: http://en.wikibooks.org/wiki/F_Sharp_Programming/Computation_Expressions
And if you want to dive deeper into the theory behind this, you can read this paper by Tomas Petricek and Don Syme: http://www.cl.cam.ac.uk/~tp322/drafts/notations.pdf
You constructed a function that has the signature
safeDiv : int option -> int option -> int option
. You need to use an entry likesafeDiv (Some 4) (Some 2)
to use your function as is.Your second version was close.
It should be
The extra brackets are required to make sure that functions are applied in the correct order.
The problem is in the matching of (4, 2), of type int*int, with the expressions (_, Some 0) and (Some xx, Some yy). The whole function can be simplified:
Making the following call valid