I'm learning F# coming from C# and I've just tried compiling an expression like
let y = Seq.groupBy (fun x -> (x < p ? -1 : x == p ? 0: 1))
but see 'unexpected integer literal in expression'. Does F# have a ternary operator? If not, what should I use instead?
f:x↦y means that f is a function which takes in a value x and gives out y.
This function is written f′ and is called the derivative function or the derivative of f. Sometimes f has a derivative at most, but not all, points of its domain. The function whose value at a equals f′(a) whenever f′(a) is defined and elsewhere is undefined is also called the derivative of f.
f″ denotes the second derivative of f; that is to say, it is the derivative of the derivative of f.
Here’s where this illustrious piece of internet history comes from and why an “F” means to pay respects. What Is an “F?” You’ve just described, in great detail, a terrible day to your friend via text. You got rained on, you lost your wallet, a dog bit your leg, and your partner broke up with you.
In some pc games, you had to press the button F for showig respect to a fallen companion. It became a common action in many games, so when someone write F means like “you’re dead” or something is lost forever Typing "F" comes from the popular internet meme "press F to pay respects".
So "f (2) = 7 or 9" is not right! When a relationship does not follow those two rules then it is not a function ... it is still a relationship, just not a function. ... ... So it follows the rules. (Notice how both 4 and -4 relate to 16, which is allowed.)
The origin of “F” is a classic internet story. In 2014, Activision released Call of Duty: Advanced Warfare, the eleventh installment of the Call of Duty franchise. For the most part, it was a typical major FPS release, with millions of players loading up the game on its release day.
Yes, it's called if .. then .. else
In fact in F# everything is an expression, even an if .. then .. else
block.
In C# var x = true ? 0 : 1;
In F# let x = if true then 0 else 1
So in your case:
let y = Seq.groupBy (fun x -> if x < p then -1 else if x = p then 0 else 1)
you can shorten it a bit with elif
let y = Seq.groupBy (fun x -> if x < p then -1 elif x = p then 0 else 1)
Another option to consider in F# specially when you have more than 2 cases is pattern matching:
let f p x =
match x with
| x when x < p -> -1
| x when x = p -> 0
| _ -> 1
let y = Seq.groupBy (f p)
But in your particular case I would use the if .. then .. elif .. then.
Finally note that the test-equality operator is =
not ==
as in C#.
If you want to save the typing you can define your own
let (?=) (q: bool) (yes: 'a, no: 'a) = if q then yes else no
Note that you can't use : in operators so ?= is the nearest you can get.
Usage: maybe ?= ("true", "false")
You can also implement this using pattern matching with function using guards:
let y = Seq.groupBy (function |x when x < p -> -1
|x when x = p -> 0
|_ -> 1)
Pattern matches may seem longer ternary operator but they are much easier to read when logic gets more complex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With