0% found this document useful (0 votes)
6 views8 pages

F# Data Types and Variable Management

The document provides an overview of various data types in F#, including floating point, text, boolean, and variable declarations. It explains the characteristics of each data type, such as size and range, and illustrates variable declaration and mutability with examples. Additionally, it highlights that variables in F# are immutable by default, requiring the 'mutable' keyword for changeable values.

Uploaded by

gobobi8535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

F# Data Types and Variable Management

The document provides an overview of various data types in F#, including floating point, text, boolean, and variable declarations. It explains the characteristics of each data type, such as size and range, and illustrates variable declaration and mutability with examples. Additionally, it highlights that variables in F# are immutable by default, requiring the 'mutable' keyword for changeable values.

Uploaded by

gobobi8535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

F#

c: 36
d: 212
e: 504
f: 716

Floating Point Data Types


The following table provides the floating point data types of F#.

F# Type Size Range Example Remarks

42.0F 32-bit signed floating


±1.5e-45 to
float32 4 bytes point number (7
±3.4e38 -11.0F significant digits)

42.0 64-bit signed floating


±5.0e-324 to
float 8 bytes point number (15-16
±1.7e308 -11.0 significant digits)

42.0M 128-bit signed floating


16 ±1.0e-28 to
decimal point number (28-29
bytes ±7.9e28 -11.0M significant digits)

Arbitrary precision
42N rational number. Using
At least Any rational
BigRational this type requires a
4 bytes number. -11N reference to
[Link].

Example

(* 32-bit signed floating point number *)


(* 7 significant digits *)
let d = 212.098f
let e = 504.768f
let f = d + e

printfn "d: %f" d


printfn "e: %f" e
printfn "f: %f" f

24
F#

(* 64-bit signed floating point number *)


(* 15-16 significant digits *)
let x = 21290.098
let y = 50446.768
let z = x + y

printfn "x: %g" x


printfn "y: %g" y
printfn "z: %g" z

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

d: 212.098000
e: 504.768000
f: 716.866000
x: 21290.1
y: 50446.8
z: 71736.9

Text Data Types


The following table provides the text data types of F#.

F#
Size Range Example Remarks
Type

'x' Single unicode


char 2 bytes U+0000 to U+ffff
'\t' characters

20 + (2 * string's 0 to about 2 billion "Hello"


string Unicode text
length) bytes characters "World"

Example

let choice = 'y'

25
F#

let name = "Zara Ali"


let org = "Tutorials Point"

printfn "Choice: %c" choice


printfn "Name: %s" name
printfn "Organisation: %s" org

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

Choice: y
Name: Zara Ali
Organisation: Tutorials Point

Other Data Types


The following table provides some other data types of F#.

F#
Size Range Example Remarks
Type

Only two possible values, true Stores boolean


bool 1 byte
true or false false values

Example

let trueVal = true


let falseVal = false
printfn "True Value: %b" (trueVal)
printfn "False Value: %b" (falseVal)

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

True Value: true


False Value: false

26
6. F# – Variables F#

A variable is a name given to a storage area that our programs can manipulate. Each variable
has a specific type, which determines the size and layout of the variable's memory; the range
of values that can be stored within that memory; and the set of operations that can be applied
to the variable.

Variable Declaration in F#
The let keyword is used for variable declaration:

For example,

let x = 10

It declares a variable x and assigns the value 10 to it.

You can also assign an expression to a variable:

let x = 10
let y = 20
let z = x + y

The following example illustrates the concept:

Example

let x = 10
let y = 20
let z = x + y

printfn "x: %i" x


printfn "y: %i" y
printfn "z: %i" z

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

x: 10

27
F#

y: 20
z: 30

Variables in F# are immutable, which means once a variable is bound to a value, it can’t be
changed. They are actually compiled as static read-only properties.

The following example demonstrates this.

Example

let x = 10
let y = 20
let z = x + y

printfn "x: %i" x


printfn "y: %i" y
printfn "z: %i" z

let x = 15
let y = 20
let z = x + y

printfn "x: %i" x


printfn "y: %i" y
printfn "z: %i" z

When you compile and execute the program, it shows the following error message:

Duplicate definition of value 'x'


Duplicate definition of value 'y'
Duplicate definition of value 'z'

28
F#

Variable Definition with Type Declaration


A variable definition tells the compiler where and how much storage for the variable should
be created. A variable definition may specify a data type and contains a list of one or more
variables of that type as shown in the following example.

Example

let x:int32 = 10
let y:int32 = 20
let z:int32 = x + y

printfn "x: %d" x


printfn "y: %d" y
printfn "z: %d" z

let p:float = 15.99


let q:float = 20.78
let r:float = p + q

printfn "p: %g" p


printfn "q: %g" q
printfn "r: %g" r

When you compile and execute the program, it shows the following error message:

x: 10
y: 20
z: 30
p: 15.99
q: 20.78
r: 36.77

29
F#

Mutable Variables
At times you need to change the values stored in a variable. To specify that there could be a
change in the value of a declared and assigned variable, in later part of a program, F#
provides the mutable keyword. You can declare and assign mutable variables using this
keyword, whose values you will change.

The mutable keyword allows you to declare and assign values in a mutable variable.

You can assign some initial value to a mutable variable using the let keyword. However, to
assign new subsequent value to it, you need to use the <- operator.

For example,

let mutable x = 10
x <- 15

The following example will clear the concept:

Example

let mutable x = 10
let y = 20
let mutable z = x + y
printfn "Original Values:"
printfn "x: %i" x
printfn "y: %i" y
printfn "z: %i" z

printfn "Let us change the value of x"


printfn "Value of z will change too."
x <- 15
z <- x + y
printfn "New Values:"
printfn "x: %i" x
printfn "y: %i" y
printfn "z: %i" z

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

30
F#

Original Values:
x: 10
y: 20
z: 30
Let us change the value of x
Value of z will change too.
New Values:
x: 15
y: 20
z: 35

31

You might also like