Tuple is a new custom type that allows you to group the different data types under a single type. It is similar to Structs in Golang, type in Typescript. It is used to create data structures on demand for processing.

Syntax

(field1,field2,... fieldN)
struct(field1,field2,... fieldN)
  • tuple elements are ordered and unnamed
  • Each field is a Valid f# expression
  • Fields contain the same or different mixed data types
  • Tuples passed as an argument to functions
  • Elements are enclosed in () and separated by comma.

Examples

// simple tuple
(4,5)
// String tuple
("one","two")

// tuple with different datatypes
("one",5,1.1f,false)

// simple tuple
(m+1,n+11)

How to get tuple values

There are multiple ways, we can get tuple individual values

let (first,second, third) = ("one", "two","three")
printfn "%s" first
printfn "%s" second
printfn "%s" third

fst and snd functions return the first and second elements of a tuple.

let numbers = ("one", "two","three")
printfn "%A" (fst numbers)
printfn "%A" (snd numbers)
printfn "%A" (trd numbers)

Pass Tuple to a function in F

Here is a function that accepts tuple arguments.

let sum (first, second,third) =
    first+second+third

let result:int = sum (1,2,3)
printfn "Numbers Sum: %d" result