Lesson 9 of 15

Tuples

Tuples

Tuples group a fixed number of values of possibly different types:

let pair = (1, "hello")
let triple = (1, 2.0, "three")

Accessing Elements

For pairs, use fst and snd:

let p = (10, 20)
let a = fst p   (* 10 *)
let b = snd p   (* 20 *)

Destructuring

You can pattern-match tuples directly:

let (x, y) = (3, 4)
(* x = 3, y = 4 *)

Your Task

Write a function swap that takes a pair (a, b) and returns (b, a). Print the swapped pair's elements.

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