Operators in F#
Learn arithmetic, comparison, logical, and functional operators in F#, including the pipe operator and function composition
Operators in F# are not just symbols sprinkled between values — they are functions in their own right. Because F# is a functional-first language with static, strong, inferred typing, operators carry the same first-class status as named functions: you can pass them around, partially apply them, and define new ones.
This makes operators particularly important in F#. While most languages treat + and - as built-in syntax, F# treats them as functions that happen to use infix notation. That same principle gives rise to the pipe operator (|>) and the composition operator (>>), which are central to idiomatic F# code.
In this tutorial you will see arithmetic, comparison, logical, and bitwise operators, plus the functional operators that make data flow through F# programs feel natural and readable. We will use a single comprehensive script that you can run end-to-end with the .NET SDK Docker image.
Arithmetic Operators
F# supports the usual infix arithmetic operators. Unlike many dynamic languages, F# performs no implicit numeric conversion: adding an int to a float is a type error. You convert explicitly using functions like float or int.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 2 + 3 |
- | Subtraction | 10 - 4 |
* | Multiplication | 6 * 7 |
/ | Division | 15 / 4 |
% | Modulus (remainder) | 17 % 5 |
** | Power (floats only) | 2.0 ** 8.0 |
Integer division truncates toward zero. To get a float result, at least one operand must be a float.
Comparison and Logical Operators
Comparison operators return a bool and work on any type that supports structural comparison (numbers, strings, tuples, records, discriminated unions). Note that F# uses a single = for equality (not ==) and <> for inequality (not !=).
Logical operators are && (and), || (or), and not as a prefix function. Both && and || short-circuit.
The Pipe and Composition Operators
These two operators define the F# style:
|>— forward pipe.x |> fis equivalent tof x, but reads left-to-right.>>— forward composition.f >> gis the functionfun x -> g (f x).
Piping is so common that you will see entire programs structured as a sequence of pipes feeding data through transformations. It is the F# answer to method chaining in OOP languages.
A Comprehensive Example
Create a file named operators.fsx:
| |
Running with Docker
| |
The dotnet fsi command launches F# Interactive, which executes the script directly without a separate compile step.
Expected Output
Arithmetic:
2 + 3 = 5
10 - 4 = 6
6 * 7 = 42
15 / 4 = 3 (integer division truncates)
17 % 5 = 2
2.0 ** 8.0 = 256.0
float 15 / 4.0 = 3.75
Comparison:
3 = 3 : true
3 <> 4 : true
5 < 10 : true
"abc" < "abd" : true
Logical:
true && false : false
true || false : true
not true : false
String concat: Hello, F#!
Bitwise:
6 &&& 3 = 2 (AND)
6 ||| 3 = 7 (OR)
6 ^^^ 3 = 5 (XOR)
1 <<< 4 = 16 (left shift)
Pipe result (sum of squares > 5): 50
addThenDouble 3 = 8
(+) 4 5 = 9
List.reduce (+) [10;20;30;40] = 100
5 .+. 3 (custom) = 16
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Key Concepts
- Operators are functions.
(+),(*), and even(|>)are normal functions wrapped in parentheses; you can pass them to higher-order functions likeList.reduce. - Equality uses
=, inequality uses<>. Coming from C-family languages, this is the most common F# stumble. - No implicit numeric conversion. Mixing
intandfloatis a compile error — usefloat,int,decimal, etc. to convert explicitly. - Integer division truncates.
15 / 4is3, not3.75. Use floats for fractional results. - The pipe operator
|>is idiomatic F#. It turns nested callsf (g (h x))into a clear left-to-right pipelinex |> h |> g |> f. - Composition
>>builds new functions. Use it when you want to name a pipeline rather than apply it immediately. - Custom operators are easy. Define
let (.+.) x y = ...and you have a new infix operator with the same first-class status as+. - Bitwise operators are triple-character (
&&&,|||,^^^) so they cannot be confused with logical&&and||.
Running Today
All examples can be run using Docker:
docker pull mcr.microsoft.com/dotnet/sdk:9.0
Comments
Loading comments...
Leave a Comment