Fork me on GitHub
Source file: swap.fut

Swapping two elements of an array

You can swap two elements of an array as follows:

def swap 't (i: i64) (j: i64) (A: *[]t) =
  let tmp = copy A[j]
  let A[j] = copy A[i]
  let A[i] = tmp
  in A

This uses in-place updates, which interacts with the uniqueness type system, which is why the A parameter has the * annotation.

See also

Uniqueness Types and In-Place Updates