I'm new to F# and haven't seen anywhere how to do the following correctly
let fun1 a b =
match a b with
| a + b > 0 -> fun2
| a + b < 0 -> fun3
I have to do this with pattern matching—for homework—but I don't know how to do it correctly. I thought I could be use tuples as follows:
let fun1 (a,b) =
match (a,b) with
| (a,b) when a + b > 0 -> fun2
| (a,b) when a + b < 0 -> fun3
But that doesn't work too. How do I proceed?
You have the right idea, you just forgot that indentation matters in F#. Adding spaces makes you solution work:
let fun1 (a,b) =
match (a,b) with
| (a,b) when a + b > 0 -> fun2
| (a,b) when a + b < 0 -> fun3
This actually patterns matches twice, so we might want to rewrite it:
let fun1 t = function
| (a,b) when a + b > 0 -> fun2
| (a,b) when a + b < 0 -> fun3
The compiler/interpreter will let this go through, but with a warning:
warning FS0025: Incomplete pattern matches on this expression.
I'll leave getting rid of that as an exercise :-)
PS. Good of you to be upfront about this being homework.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments