---
id: set-reference
title: Set
description: Set operations
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
Sets are unordered collections of unique values of the same type.
function empty : set('value)
val empty : 'value set
let empty: set('value)
Create an empty set.
```pascaligo group=sets
const my_set : set (int) = Set.empty
```
Alternative syntax:
```pascaligo group=sets
const my_set : set (int) = set []
```
```cameligo group=sets
let my_set : int set = Set.empty
```
```reasonligo group=sets
let my_set : set (int) = Set.empty;
```
function literal : list('value) -> set('value)
val literal : 'value list -> 'value set
let literal: list('value) => set('value)
Create a non-empty set.
```pascaligo group=sets
const my_set : set (int) = Set.literal (list [3; 2; 2; 1])
```
Or use the following syntax sugar:
```pascaligo group=sets
const my_set : set (int) = set [3; 2; 2; 1]
```
```cameligo group=sets
let my_set : int set =
Set.literal [3; 2; 2; 1]
```
```reasonligo group=sets
let my_set : set (int) =
Set.literal ([3, 2, 2, 1]);
```
function mem : 'value -> set('value) -> 'bool
val mem : 'value -> 'value set -> bool
let mem: ('value, set('value)) => bool
Checks if a value exists in the set.
```pascaligo group=sets
const contains_3 : bool = Set.mem(3, my_set)
```
Or:
```pascaligo group=sets
const contains_3_alt : bool = my_set contains 3
```
```cameligo group=sets
let contains_3 : bool = Set.mem 3 my_set
```
```reasonligo group=sets
let contains_3 : bool = Set.mem (3, my_set);
```
function cardinal : set('value) -> nat
val cardinal : 'value set -> nat
let cardinal: set('value) => nat
Number of elements in a set.
```pascaligo group=sets
const cardinal : nat = Set.size (my_set)
```
> Note that `size` is *deprecated*. Please use `Set.size`
```cameligo group=sets
let cardinal : nat = Set.size my_set
```
```reasonligo group=sets
let cardinal : nat = Set.size (my_set);
```
function add : 'value -> set('value) -> set('value)
val add : 'value -> 'value set -> 'value set
let add: ('value, set('value)) => set('value)
Add a value to a set.
function remove : 'value -> set('value) -> set('value)
val remove : 'value -> 'value set -> 'value set
let remove: ('value, set('value)) => set('value)
Remove a value from a set.
function iter : ('a -> unit) -> set('a) -> unit
val iter : ('a -> unit) -> 'a set -> unit
let iter: (('a => unit), set('a)) => unit
Iterate over values in a set.
```pascaligo group=sets
function iter_op (const s : set (int)) : unit is
block {
function iterated (const i : int) : unit is
if i > 2 then Unit else (failwith ("Below range.") : unit)
} with Set.iter (iterated, s)
```
> Note that `set_iter` is *deprecated*.
```cameligo group=sets
let iter_op (s : int set) : unit =
let predicate = fun (i : int) -> assert (i > 3)
in Set.iter predicate s
```
```reasonligo group=sets
let iter_op = (s : set (int)) : unit => {
let predicate = (i : int) => assert (i > 3);
Set.iter (predicate, s);
};
```
function fold : (('accumulator -> 'item -> 'accumulator) -> set ('item) -> 'accumulator) -> 'accumulator
val fold : ('accumulator -> 'item -> 'accumulator) -> 'set list -> 'accumulator -> 'accumulator
let fold: ((('accumulator, 'item) => 'accumulator), set('item), 'accumulator) => 'accumulator
[Fold over values in a set](../language-basics/sets-lists-tuples#folded-operation)
```pascaligo group=sets
function sum (const acc : int; const i : int): int is acc + i
const sum_of_elements : int = Set.fold (sum, my_set, 0)
```
> Note that `set_fold` is *deprecated*.
```cameligo group=sets
let sum (acc, i : int * int) : int = acc + i
let sum_of_elements : int = Set.fold sum my_set 0
```
```reasonligo group=sets
let sum = ((acc, i) : (int, int)) : int => acc + i;
let sum_of_elements : int = Set.fold (sum, my_set, 0);
```