C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We unpack the tuple's items into 3 variables (animal, weight, feet). We can then address those variables directly.
F# program that uses tuples
// Create a tuple with three items.
// ... It has a string and 2 ints.
let data = ("cat", 100, 4)
printfn "%A" data
// Unpack the tuple into 3 variables.
let (animal, weight, feet) = data
printfn "%A" animal
printfn "%A" weight
printfn "%A" feet
Output
("cat", 100, 4)
"cat"
100
4
F# program that returns tuple from function
// This function returns a tuple with the argument and its English word.
// ... It uses a match construct.
let testFunction x =
match x with
| 0 -> (0, "zero")
| 1 -> (1, "one")
| _ -> (-1, "unknown")
// Test the function with three values.
let result0 = testFunction 0
printfn "%A" result0
let result1 = testFunction 1
printfn "%A" result1
let result2 = testFunction 2
printfn "%A" result2
Output
(0, "zero")
(1, "one")
(-1, "unknown")
Here: We create a 2-item tuple (a pair) and then set "size" to the first item in the pair. The second item (a string) is ignored.
F# program that uses underscore, tuple
// This tuple contains two items.
let info = (10, "bird")
// Unpack the tuple, but ignore the second item.
// ... Use an underscore to ignore it.
let (size, _) = info
printfn "%d" size
Output
10
Quote: A tuple allows you to keep things organized by grouping related values together, without introducing a new type (F# Language Specification).