Understanding F# Data Types

We are going to explore F# Data types in this article, including the most basic types, such as numbers and strings, as well as the different types of numbers and strings in F#.

Here, we will take a closer look at how these types can be used to represent and manipulate data in various ways.

The F# programming language was created with the intent of being highly expressive and concise. This makes it the ideal language for developing complex applications that rely heavily on data to process.

As one of F#’s strongest features, the powerful type system is one of the most important features. With an abundance of types and constructs, F# provides a robust set of options for working with data in a type-safe and efficient manner.

After finishing this article, you should have a good idea of what the F# Data types are and how you can use them effectively in the development of your own F# applications.



F# Data Types

F# offers a set of data types which can be classified in the following ways:

  • Integral types
  • Floating point types
  • Text types
  • Other types

Integral Data Type

Here is a table that includes the integral data types that are available in F#.

Basically, these are types of data that can be expressed as integers.

F# TypesSizeRangeExamplesRemarks
sbyte1 byte-128 to 12742y

-11y

8-bit signed integer
byte1 byte0 to 2556uy

5uy

8-bit unsigned integer
int162 bytes-32768 to 327674s

9s

16-bit signed integer
uint162 bytes0 to 65,53542us

200us

16-bit unsigned integer
int/int324 bytes-2,147,483,648 to 2,147,483,64735

44

32-bit signed integer
uint324 bytes0 to 4,294,967,295212u

504u

32-bit unsigned integer
int648 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8077878L

4548L

64-bit signed integer
uint648 bytes0 to 18,446,744,073,709,551,615454521UL

544000UL

64-bit unsigned integer
bigintAt least 4 bytesany integer42I

149999999999

999999999999

9999999I

arbitrary precision integer

Example: 

//signed 1 byte integer let a = 121.97f let b = 145.58f let c = a + bprintfn "a: %f" a printfn "b: %f" b printfn "The sum of single byte signed integers a + b is: %f" c//unsigned 1 byte natural numberlet x = 5uy let y = 6uy let z = x + yprintfn "x: %i" x printfn "y: %i" y printfn "The sum of single byte unsigned integers x + y is: %i" z//signed 2 byte integerlet m = 4s let n = 9s let o = m + nprintfn "m: %i" m printfn "n: %i" n printfn "The sum of signed 2 byte intergers m + n is: %i" o//signed 4 byte integerlet d = 212u let e = 504u let f = d + eprintfn "d: %i" d printfn "e: %i" e printfn "The sum of signed 4 byte integers d + e is: %i" f//signed 8 byte integerlet g = 7878l let h = 4548l let i = g + hprintfn "g: %i" g printfn "h: %i" h printfn "The sum of signed 8 byte integers g + h is: %i" i

Floating Point Data Types

A list of F# floating point data types can be found in the following table.

F# TypesSizeRangeExamplesRemarks
float324 bytes“±1.5e-45 to ±3.4e38”35.0F

45.0F

7 significant digits, 32-bit signed floating point number
float8 bytes“±5.0e-324 to ±1.7e308”35.0

45.0

15-16 significant digits in a 64-bit signed floating point number
decimal16 bytes“±1.0e-28 to ±7.9e2”445.0M

1546.0M

It has 28-29 significant digits and is signed in 128 bits
BigRationalAt least 4 bytesAny rational number.78N

1315N

Random rational number. If you want to use this type, you need to refer to the FSharp.PowerPack.dll library.

Example: 

Example

//32-bit signedlet a = 687.145f let b = 687.794f let c = a + bprintfn "a: %f" a printfn "b: %f" b printfn "Addition of a + b is: %f" c//64-bit signed let x = 45687.098 let y = 12144.768 let z = x + yprintfn "x: %g" x printfn "y: %g" y printfn "Addition of x + y is: %g" z
F# Data Types

Example: 

//32-bit signedlet a = 687.145f let b = 687.794f let c = a * bprintfn "a: %f" a printfn "b: %f" b printfn "Multiplication of a * b is: %f" c//64-bit signed let x = 45687.098 let y = 12144.768 let z = x * yprintfn "x: %g" x printfn "y: %g" y printfn "Multiplication of x * y is: %g" z
Output:
a: 687.145000
b: 687.794000
Multiplication of a * b is: 472614.200000
x: 45687.1
y: 12144.8
Multiplication of x * y is: 5.54859e+08

F# Data Types – Text

Here is a table that shows the types of text data that F# support.
F# TypesSizeRangeExamplesRemarks
char2 bytesU+0000 to U+ffffxSingle unicode characters
string20 + (2 * strings length) bytes0 to about 2 billion characters“Hello”

“World”

Unicode text

Example: 

let choice = 'a' let name = "John Wick" let website = "mrexamples"printfn "Choice: %c" choice printfn "Name: %s" name printfn "Website: %s" website

Output

Choice: a
Name: John Wick
Website: mrexamples

Example: 

let choice = 'a' let greet = "Hello from mrexamples"printfn "Choice: %c" choice printfn "%s" greet
Output:
Choice: a
Hello from mrexamples

Other Data Types

This table provides some information on some of the other data types available in F#.

F# TypesSizeRangeExamplesRemarks
bool1 byteTrue or false are the only two possible values that can be assigned to ittrue

false

This function stores boolean values
Example: 

Example

let true_value = true let false_value = falseprintfn "True Value: %b \n" (true_value) printfn "False Value: %b" (false_value)
Output
True Value: true 
False Value: false
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *