In f #, is there a language construct for testing if a number is between two other numbers (in a range)?

In f # I am looking for a more succinct equivalent:

myNumber >= 2 && myNumber <= 4 

I imagine something like

 myNumber >=< (2, 4) 

Is there some kind of operation like this?

+4
source share
3 answers

There is no operator of your own, but you can define your own.

 let inline (>=<) a (b,c) = a >= b && a<= c 
+11
source

John's answer is exactly what you asked for, and the most practical solution. But I wondered if the operator (s) could be defined so that the syntax came close to normal mathematical notation, i.e. a <= b <= c .

Here is one such solution:

 let inline (<=.) left middle = (left <= middle, middle) let inline (.<=) (leftResult, middle) right = leftResult && (middle <= right) let inline (.<=.) middleLeft middleRight = (middleLeft .<= middleRight, middleRight) 1 <=. 3 .<=. 5 .<= 9 // true 1 <=. 10 .<= 5 // false 

A few comments about this:

generated IL

+7
source

Is there some kind of operation like this?

Great question! The answer is no, no, but I wanted it to be.

The answer to Latin is good, but it is not designed for short circuit. Therefore, if the first test fails, the remaining subexpressions are still evaluated, although their results are not relevant.

FWIW, in Mathematica you can do 1<x<2 just like math.

+3
source

Source: https://habr.com/ru/post/1493998/


All Articles