Lesson 13 of 15

Tuples

Tuples

Tuples group multiple values without naming them:

let point = (3, 4)
let name, age = "Alice", 30  // destructuring
printfn $"{name} is {age}"

Use fst and snd to access the first and second element of a pair:

let p = (10, 20)
printfn $"{fst p}"  // 10
printfn $"{snd p}"  // 20

Destructure in function parameters:

let addPair (a, b) = a + b
printfn $"{addPair (3, 4)}"  // 7

Your Task

Write a function swap that takes a tuple (a, b) and returns (b, a). Then write a function sumPair that takes a tuple of two integers and returns their sum.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.