F# – Operators

  • Post author:
  • Post category:F#
  • Post comments:1 Comment
F# - Operators

F# operators is a symbol that tells the compiler to perform specific mathematical or logical manipulations. F# is rich in built-in operators.

Following types of F# Operators −

  • Arithmetic Operators
  • Comparison Operators
  • Boolean Operators
  • Bitwise Operators

Arithmetic Operators

The following table shows all the arithmetic operators supported by F# language. Assume variable A holds 10 and variable B holds 20 then −

OperatorDescriptionExample
+Adds two operandsA + B will give 30
Subtracts second operand from the firstA – B will give -10
*Multiplies both operandsA * B will give 200
/Divides numerator by de-numeratorB / A will give 2
%Modulus Operator and the remainder of after an integer divisionB % A will give 0
**Exponentiation Operator raises an operand to the power of anotherB**A will give 2010

Show Example

let a : int32 = 21
let b : int32 = 10

let mutable c = a + b
printfn "Line 1 - Value of c is %d" c

c <- a - b;
printfn "Line 2 - Value of c is %d" c

c <- a * b;
printfn "Line 3 - Value of c is %d" c

c <- a / b;
printfn "Line 4 - Value of c is %d" c

c <- a % b;
printfn "Line 5 - Value of c is %d" c

When you compile and execute the program, it yields the following output −

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1

Comparison Operators

The following table shows all the comparison operators supported by the F# language. These binary comparison operators are available for integral and floating-point types. These operators return values of type bool.

Assume variable A holds 10, and variable B holds 20, then −

OperatorDescriptionExample
=Checks if the values of two operands are equal or not, if yes then the condition becomes true.(A == B) is not true.
<>Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true.(A <> B) is true.
>Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true.(A > B) is not true.
<Checks if the value of the left operand is less than the value of the right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true.(A >= B) is not true.
<=Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true.(A <= B) is true.

Show Example

let mutable a : int32 = 21
let mutable b : int32 = 10

if (a = b) then
   printfn "Line 1 - a is equal to b"
else
   printfn "Line 1 - a is not equal to b"

if (a < b) then
   printfn "Line 2 - a is less than b"
else
   printfn "Line 2 - a is not less than b"

if (a > b) then
   printfn "Line 3 - a is greater than b"
else
   printfn "Line 3 - a is not greater than b"

(* Lets change value of a and b *)
a <- 5
b <- 20

if (a <= b) then
   printfn "Line 4 - a is either less than or equal to b"
else
   printfn "Line4 - a is a is greater than b"

When you compile and execute the program, it yields the following output −

Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b

Boolean Operators

The following table shows all the Boolean operators supported by the F# language. Assume variable A holds true and variable B holds false, then −

OperatorDescriptionExample
&&Called Boolean AND operator. If both the operands are non-zero, then the condition becomes true.(A && B) is false.
||Called Boolean OR Operator. If any of the two operands are non-zero, then the condition becomes true.(A || B) is true.
notCalled Boolean NOT Operator. Use to reverse the logical state of its operand. If a condition is true then the Logical NOT operator will make it false.not (A && B) is true.

Show Example

let mutable a : bool = true;
let mutable b : bool = true;

if ( a && b ) then
   printfn "Line 1 - Condition is true"
else
   printfn "Line 1 - Condition is not true"

if ( a || b ) then
   printfn "Line 2 - Condition is true"
else
   printfn "Line 2 - Condition is not true"

(* lets change the value of a *)

a <- false
if ( a && b ) then
   printfn "Line 3 - Condition is true"
else
   printfn "Line 3 - Condition is not true"

if ( a || b ) then
   printfn "Line 4 - Condition is true"
else
   printfn "Line 4 - Condition is not true"

When you compile and execute the program, it yields the following output −

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

Bitwise Operators

Bitwise operators work on bits and perform the bit-by-bit operations. The truth tables for &&& (bitwise AND), ||| (bitwise OR), and ^^^ (bitwise exclusive OR) are as follows −

pqp &&& qp ||| qp ^^^ q
00000
01011
11110
10011

Assume if A = 60; and B = 13; now in binary format they will be as follows −

A = 0011 1100

B = 0000 1101—————–

A&&&B = 0000 1100

A|||B = 0011 1101

A^^^B = 0011 0001

~~~A = 1100 0011

The Bitwise operators supported by the F# language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then −

OperatorDescriptionExample
&&&Binary AND Operator copies a bit to the result if it exists in both operands.(A &&& B) will give 12, which is 0000 1100
|||Binary OR Operator copies a bit if it exists in either operand.(A ||| B) will give 61, which is 0011 1101
^^^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^^^ B) will give 49, which is 0011 0001
~~~Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.(~~~A) will give -61, which is 1100 0011 in 2’s complement form.
<<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A <<< 2 will give 240 which is 1111 0000
>>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >>> 2 will give 15 which is 0000 1111

Show Example

let a : int32 = 60 // 60 = 0011 1100
let b : int32 = 13 // 13 = 0000 1101
let mutable c : int32 = 0

c <- a &&& b // 12 = 0000 1100
printfn "Line 1 - Value of c is %d" c

c <- a ||| b // 61 = 0011 1101
printfn "Line 2 - Value of c is %d" c

c <- a ^^^ b // 49 = 0011 0001
printfn "Line 3 - Value of c is %d" c

c <- ~~~a // -61 = 1100 0011
printfn "Line 4 - Value of c is %d" c

c <- a <<< 2 // 240 = 1111 0000
printfn "Line 5 - Value of c is %d" c

c <- a >>> 2 // 15 = 0000 1111
printfn "Line 6 - Value of c is %d" c

When you compile and execute the program, it yields the following output −

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is 49
Line 5 - Value of c is 240
Line 6 - Value of c is 15

Operators Precedence

The following table shows the order of precedence of operators and other expression keywords in the F# language, from the lowest precedence to the highest precedence.

OperatorAssociativity
asRight
whenRight
| (pipe)Left
;Right
letNon-associative
function, fun, match, tryNon-associative
ifNon-associative
Right
:=Right
,Non-associative
or, ||Left
&, &&Left
< op, >op, =, |op, &opLeft
&&& , |||, ^^^, ~~~, <<<, >>>Left
^ opRight
::Right
:?>, 😕Non-associative
– op, +op, (binary)Left
* op, /op, %opLeft
** opRight
f x (function application)Left
| (pattern match)Right
prefix operators (+op, -op, %, %%, &, &&, !op, ~op)Left
.Left
f(x)Left
f<types>Left

Show Example

let a : int32 = 20
let b : int32 = 10
let c : int32 = 15
let d : int32 = 5

let mutable e : int32 = 0
e <- (a + b) * c / d // ( 30 * 15 ) / 5
printfn "Value of (a + b) * c / d is : %d" e

e <- ((a + b) * c) / d // (30 * 15 ) / 5
printfn "Value of ((a + b) * c) / d is : %d" e

e <- (a + b) * (c / d) // (30) * (15/5)
printfn "Value of (a + b) * (c / d) is : %d" e

e <- a + (b * c) / d // 20 + (150/5)
printfn "Value of a + (b * c) / d is : %d" e

When you compile and execute the program, it yields the following output −

Value of (a + b) * c / d is : 90 
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90 
Value of a + (b * c) / d is : 50

Next Topic – Click Here

This Post Has One Comment

Leave a Reply