**Introduction**

F# contains a number of basic operators. Operators are a more aesthetic way to call a function. This is an interesting and compelling feature in F#. F# supports two types of Operators. The Prefix and the Infix type. F# provide a diverse set of Operators which you can use with Boolean, numeric, string and Collection Type. The Operators defined in F# and its libraries are too numerous.

**Prefix Operators**

A Prefix operator is an operator that has one operand. Prefix means that the operator precedes its operand such as (-a). A Prefix operator appears before its operand.

**Infix Operators**

An Infix operator is an operator that appears between the first and second operands. An Infix operator takes two or more arguments. It will take a form like (a+b).

**Operator Overloading**

F# operators are overloaded, which means you can use more than one Type with an operator and both operands must be the same type. Otherwise the compiler will generate an Error. F# also allows the user to define and redefine operators. Operators in F# follow a set of rules like C# for operator overloading, therefore, any class in the BCL or any .NET libraries written to support operator overloading in C# will support it in F#. You can use the + Operator to concatenate strings as well as to add a System.TimeSpan to a System.DateTime, because these types support an overload of + operator.

**Example-**let words = "Dea " + "and " + "Dazzy"

open System

let nextyear =

DateTime.Now + new TimeSpan(365, 0, 0, 0, 0)

Operators are not values like functions, so they cannot be passed to other functions as a parameter. However if you need to use an Operator as a value, you can do this by surrounding it in parentheses. Then the Operator will behave like a Function.

The user can define their own Operator or redefine any of the existing ones. Consider the following example that redefines + to perform a Subtraction.

Example-

let (+) X Y = X - Y

printfn "%i" (6 + 4)

Output-

Operators

Math Operators in F#

The given table shows Math Operators.

Operator Name |
Form of Expression |
Description |

abs | abs a | Overloaded absolute value |

acos | acos a | Overloaded inverse cosine |

asin | asin a | Overloaded inverse sine |

atan | atan a | Overloaded inverse tangent |

atan2 | atan2 ab | Overloaded of inverse tangent of a/b |

ceil | ceil a | Overloaded floating point ceiling |

cos | cos a | Overloaded cosine |

cosh | cosh a | Overloaded Hyperbolic Cosine |

exp | exp a | Overloaded exponent |

floor | floor a | Overloaded floating point floor |

log | log a | Overloaded natural logarithm |

log10 | log10 a | Overloaded base-10 logarithm |

(**) | a**y | Overloaded exponential |

pown | pown ab | Overloaded integer exponential |

round | round a | Overloaded rounding |

sign | sign a | Overloaded sign function |

sin | sin a | Overloaded sine function |

sinh | sinh a | Overloaded hyperbolic sine function |

sqrt | sqrt a | Overloaded square root function |

tan | tan a | Overloaded tangent function |

tanh | tanh a | Overloaded hyperbolic tangent function |

The Basic Arithmetic Operators in F#

The given table shows the Arithmetic operators.

Operator Name |
Form Of Expression |
Description |

(+) | a+b | Overloaded addition |

(-) | a-b | Overloaded subtraction |

(*) | a*b | Overloaded multiplication |

(/) | a/b | Overloaded divison |

(%) | a%b | Overloaded modulus |

(~-) | -a | Overloaded unary negation |

not | not a | Boolean negation |

**Bitwise Manipulation Operators**

**in F#.**

The given table shows Bitwise Operators.

Operator Name | Form of Expression | Description |

(>>>) | a>>>b | Overloaded bitwise shift left |

(<<<) | a<<<b | Overloaded arithmetic shift right shift |

(^^^) | a^^^b | Overloaded bitwise exclusive or |

(&&&) | a&&&b | Overloaded bitwise and |

(|||) | a|||b | Overloaded bitwise or |

(~~~) | ~~~a | Overloaded bitwise negation |

**Comparison and Equality Operators in F#**

The given table shows the Comparison and Equality operators.

Operator Name |
Form of Expression |
Description |

(>) | a>b | Generic less-than |

(>=) | a>=b | Generic less-than-or-equal |

(<) | a<b | Generic greater-than |

(<=) | a<=b | Generic greater-than-or-equal |

(=) | a=b | Generic equality |

(<>) | a<>b | Generic disequality |

max | max ab | Generic maximum |

min | min ab | Generic minimum |

**Exception Operators in F#**

The given table shows Exception operators.

Operator Name |
Form of Expression |
Description |

failwith | failwith a | Raise a FailureException exception |

invalid_arg | invalid_arg a | Raise an ArgumentException exception |

raise | raise a | Raise an exception |

rethrow | rethrow{} | Special Operator to raise an exception |

**Pipelining and Composition Operators in F#**

The given table shows the composition and pipelining Operators.

Operator Name |
Form of Expression |
Description |

(|>) | a |> f | pipelining |

(>>) | f>>g | function composition |

(<|) | f<|a | backward pipelining |

(<<) | g<<f | backward function composition |

ignore | ignore a | compute and discard a value |

**Pair Operators**

**in F#**

The given table shows the pair Operators.

Operator Name |
Form of Expression |
Description |

fst | fst p | Take the first element of a pair |

snd | snd p | Take the second Element of a Pair |

**Object Transform operators in F#**

The given table shows the Object Transform operators.

Operator Name |
Form of Expression |
Description |

box | box b | Convert to object representation |

hash | hash b | Generic hashing operator |

sizeof | sizeof<type> | Compute the size of a value of given type |

typeof | typeof<type> | Compute the system type representation of given type |

typedefof | typedefof<type> | Compute the System type representation of given types and calls |

unbox | unbox b | Convert from object representation |

ref | ref b | Allocate a mutable reference cell |

(!) | !b | Read a mutable reference cell |

Note: Some Operators have been added in the CTP release and new to existing users like pown and also some operators give special type when you apply them on Floating point argument with unit of measure annotations. **Example-**

How to generate a factorial of a given number with a negation operator in F# interactive.

Setting environment for using Microsoft Visual Studio 2010 x86 tools.

D:\Dea saddler\VC>fsi.exe

Microsoft (R) F# 2.0 Interactive build 4.0.30319.1

Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> !10

- ;;

val it : int = 3628800

>

Output-

**Summary**

In this article I have covered all types of operators in F#.