Calculemus


Set Module

Set represented as ordered lists and related operations.

Note

Try to change the functions that uses this module to use the standard SetModule.

This is a point where performance could possibly be greatly improved.

Functions and values

Function or value Description

allnonemptysubsets s

Full Usage: allnonemptysubsets s

Parameters:
    s : 'a list - The input 'set'.

Returns: 'a list list All nonempty subsets of the input 'set'.

Returns all nonempty subsets of the input 'set'.

s : 'a list

The input 'set'.

Returns: 'a list list

All nonempty subsets of the input 'set'.

Example

 allsubsets [1;2;3]
Evaluates to [[1]; [1; 2]; [1; 2; 3]; [1; 3]; [2]; [2; 3]; [3]].

allsets m l

Full Usage: allsets m l

Parameters:
    m : int - The size of the subsets to be returned.
    l : 'a list - The input 'set'.

Returns: 'a list list All the subsets of the given size.

Returns all subsets of the given size.

m : int

The size of the subsets to be returned.

l : 'a list

The input 'set'.

Returns: 'a list list

All the subsets of the given size.

Example

 allsets 2 [1;2;3]
Evaluates to [[1; 2]; [1; 3]; [2; 3]].

allsubsets s

Full Usage: allsubsets s

Parameters:
    s : 'a list - The input 'set'.

Returns: 'a list list All the subsets of the input 'set'.

Returns all the subsets of the input 'set'.

s : 'a list

The input 'set'.

Returns: 'a list list

All the subsets of the input 'set'.

Example

 allsubsets [1;2;3]
Evaluates to [[]; [1]; [1; 2]; [1; 2; 3]; [1; 3]; [2]; [2; 3]; [3]].

image f s

Full Usage: image f s

Parameters:
    f : 'a -> 'b - The function to transform elements of the input 'set'.
    s : 'a list - The input 'set'.

Returns: 'b list A 'set' containing the transformed elements.

The image of s under f.

Returns a new collection containing the results of applying the given function to each element of the input 'set'.

f : 'a -> 'b

The function to transform elements of the input 'set'.

s : 'a list

The input 'set'.

Returns: 'b list

A 'set' containing the transformed elements.

Example

 [1;2;3] |> image (fun x -> x * 2)
Evaluates to [2; 4; 6]

insert x l

Full Usage: insert x l

Parameters:
    x : 'a - The value to add.
    l : 'a list - The input 'set'.

Returns: 'a list A new 'set' containing x.

Returns a new set with an element added to the set. No exception is raised if the set already contains the given element.

x : 'a

The value to add.

l : 'a list

The input 'set'.

Returns: 'a list

A new 'set' containing x.

Note

Corresponds to the standard SetModule.Add.

Example

 insert 4 [2;3;3;5]
Evaluates to [2;3;4;5].

intersect l1 l2

Full Usage: intersect l1 l2

Parameters:
    l1 : 'a list - The first input list.
    l2 : 'a list - The second input list.

Returns: 'a list The intersection of the two lists.

Computes the intersection of two 'sets'.

intersect l1 l2 returns a list consisting of those elements of l1 that also appear in l1. If both sets are free of repetitions, this can be considered a set-theoretic intersection operation.

l1 : 'a list

The first input list.

l2 : 'a list

The second input list.

Returns: 'a list

The intersection of the two lists.

Note

Corresponds to the standard SetModule.Intersect.

Example

 intersect [1;2;3] [3;5;4;1]
Evaluates to [1;3].

Example

 intersect [1;2;4;1] [1;2;3;2]
Evaluates to [1;2].

mem value source

Full Usage: mem value source

Parameters:
    value : 'a - The value to locate in the input list.
    source : 'a list - The input list.

Returns: bool True if the input list contains the specified element; false otherwise.

Tests if the list contains the specified element.

value : 'a

The value to locate in the input list.

source : 'a list

The input list.

Returns: bool

True if the input list contains the specified element; false otherwise.

Note

It's just an alias for ListModule.Contains.

Example

 [1..9] |> mem 0
Evaluates to false.

Example

 [1..9] |> mem 3
Evaluates to true.

Example

 [1, "SpongeBob"; 2, "Patrick"; 3, "Squidward"; 4, "Mr. Krabs"] 
 |> mem (2, "Patrick")
Evaluates to true.

Example

 [1, "SpongeBob"; 2, "Patrick"; 3, "Squidward"; 4, "Mr. Krabs"] 
 |> mem (22, "Patrick")
Evaluates to false.

psubset l1 l2

Full Usage: psubset l1 l2

Parameters:
    l1 : 'b list - The potential subset.
    l2 : 'b list - The set to test against.

Returns: bool true if l1 is a proper subset of l2.

Evaluates to true if all elements of the first list are in the second, and at least one element of the second is not in the first.

l1 : 'b list

The potential subset.

l2 : 'b list

The set to test against.

Returns: bool

true if l1 is a proper subset of l2.

Note

Corresponds to the standard SetModule.IsProperSubset.

Example

 psubset [1;2;3] [1;4;3;2]
Evaluates to true.

Example

 psubset [1;2;3] [2;3;1]
Evaluates to false.

Example

 psubset [1;2;3;4] [2;3;1]
Evaluates to false.

set_eq l1 l2

Full Usage: set_eq l1 l2

Parameters:
    l1 : 'a list - The first list.
    l2 : 'a list - The second list.

Returns: bool true if l1 and l2 are equal seen as sets.

Tests two 'sets' for equality.

set_eq l1 l2 returns true if every element of l1 appears in l2 and every element of l2 appears in l1. Otherwise it returns false.

In other words, it tests if the lists are the same considered as sets, i.e. ignoring duplicates.

l1 : 'a list

The first list.

l2 : 'a list

The second list.

Returns: bool

true if l1 and l2 are equal seen as sets.

Example

 set_eq [1;2] [2;1;2]
Evaluates to true.

Example

 set_eq [1;2] [1;3]
Evaluates to false.

setify l

Full Usage: setify l

Parameters:
    l : 'a list - The input list.

Returns: 'a list The result list.

Removes repeated elements from a list, making a list into a 'set'.

Returns a sorted list that contains no duplicate entries according to generic hash and equality comparisons on the entries. If an element occurs multiple times in the list then the later occurrences are discarded.

l : 'a list

The input list.

Returns: 'a list

The result list.

Example

 setify [1;2;3;1;4;3]
Evaluates to [1;2;3;4].

subset l1 l2

Full Usage: subset l1 l2

Parameters:
    l1 : 'a list - The potential subset.
    l2 : 'a list - The set to test against.

Returns: bool true if l1 is a subset of l2.

Evaluates to true if all elements of the first list are in the second.

l1 : 'a list

The potential subset.

l2 : 'a list

The set to test against.

Returns: bool

true if l1 is a subset of l2.

Note

Corresponds to the standard SetModule.IsSubset.

Example

 subset [1;2;3] [1;4;3;2]
Evaluates to true.

Example

 subset [1;2;3] [2;3;1]
Evaluates to true.

Example

 subset [1;2;3;4] [2;3;1]
Evaluates to false.

subtract l1 l2

Full Usage: subtract l1 l2

Parameters:
    l1 : 'a list - The first input list.
    l2 : 'a list - The list whose elements will be removed from l1.

Returns: 'a list The list with the elements of l2 removed from l1.

Computes the set-theoretic difference of two 'sets'.

subtract l1 l2 returns a list consisting of those elements of l1 that do not appear in l2. If both lists are initially free of repetitions, this can be considered a set difference operation.

l1 : 'a list

The first input list.

l2 : 'a list

The list whose elements will be removed from l1.

Returns: 'a list

The list with the elements of l2 removed from l1.

Note

Corresponds to the standard SetModule.Difference.

Example

 subtract [1;2;3] [3;5;4;1]
Evaluates to [2].

Example

 subtract [1;2;4;1] [4;5]
Evaluates to [1;2].

union l1 l2

Full Usage: union l1 l2

Parameters:
    l1 : 'a list - The first input list.
    l2 : 'a list - The second input list.

Returns: 'a list The union of the two lists.

Computes the union of two `sets'.

union l1 l2 returns a list consisting of the elements of l1 not already in l2 concatenated with l2 . If l1 and l2 are initially free from duplicates, this gives a set-theoretic union operation.

l1 : 'a list

The first input list.

l2 : 'a list

The second input list.

Returns: 'a list

The union of the two lists.

Note

Corresponds to the standard SetModule.Union.

Example

 union [1;2;3] [1;5;4;3]
Evaluates to [1;2;3;4;5].

Example

 union [1;1;1] [1;2;3;2]
Evaluates to [1;2;3].

unions s

Full Usage: unions s

Parameters:
    s : 'a list list - The sequence of 'sets' to union.

Returns: 'a list The union of the input 'sets'.

Computes the union of a sequence of 'sets'.

s : 'a list list

The sequence of 'sets' to union.

Returns: 'a list

The union of the input 'sets'.

Note

Corresponds to the standard SetModule.UnionMany.

Example

 unions [[1;2;3]; [4;3;2;6;2]; [5;3;1;3]]
Evaluates to [1; 2; 3; 4; 5; 6].