Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does F# have the ternary ?: operator?

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?

like image 535
Tim Lovell-Smith Avatar asked Feb 16 '15 23:02

Tim Lovell-Smith


People also ask

What does FX -> Y mean?

f:x↦y means that f is a function which takes in a value x and gives out y.

Does f mean derivative?

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.

What does f represent in calculus?

f″ denotes the second derivative of f; that is to say, it is the derivative of the derivative of f.

What does an “F” mean?

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.

What does it mean when someone writes f in a text?

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".

Is F (2) = 7 or 9?

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.)

Where does the word “F” come from in Call of duty?

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.


3 Answers

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#.

like image 179
Gus Avatar answered Oct 17 '22 20:10

Gus


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")

like image 9
Chris Woodward Avatar answered Oct 17 '22 21:10

Chris Woodward


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.

like image 6
Turkka Mannila Avatar answered Oct 17 '22 22:10

Turkka Mannila