Remove duplicate elements: Difference between revisions

m
Line 959:
=={{header|CafeOBJ}}==
The parametrized module NO-DUP-LIST(ELEMENTS :: TRIV) defines the signature of a space separated list structure. The removal of duplicates is handled by the equational properties listed after the signature in brackets {}. There is no user written code in module NO-DUP-LIST. The binary operation (_ _) is associative, commutative, and idempotent, meaning (x , x) = x. This list structure does not permit duplicates, they are removed during evaluation (called reduction in CafeOBJ). Using this module we can perform set union, just by evaluating two lists e.g:
<syntaxhighlight lang="$CafeOBJ">
<lang> red (1 2 3 4) (3 2 1 5) .
--> (4 5 1 2 3):Int
</syntaxhighlight>
</lang>
 
 
 
<langsyntaxhighlight lang="$CafeOBJ">
mod! NO-DUP-LIST(ELEMENTS :: TRIV) {
op __ : Elt Elt -> Elt { comm assoc idem assoc }
Line 985 ⟶ 986:
reduce "abc" "def" "abc" "abc" "abc" .
close
</syntaxhighlight>
</lang>
 
'''How does work?'''
Line 992 ⟶ 993:
The evaluation automatically uses right associativity. So starting with:
 
<langsyntaxhighlight> (1 1 2 1 1)</langsyntaxhighlight>
 
The system places appropriate brackets on the entire expression:
 
<langsyntaxhighlight> (1 ((1 2) (1 1))) </langsyntaxhighlight>
 
Idempotency is applied at rightmost inner brackets:
We get rewrite ''(1 1) -> 1''. The term is now:
<langsyntaxhighlight> (1 ((1 2) 1)) </langsyntaxhighlight>
 
Any further occurrence of 1 will be removed.
Line 1,008 ⟶ 1,009:
We get rewrite ''(1 2) -> 2 ''. The term is now:
 
<langsyntaxhighlight>(1 (2 1))</langsyntaxhighlight>
 
By already established idempotency we finally get
 
<langsyntaxhighlight>(2 1)</langsyntaxhighlight>
 
=={{header|Ceylon}}==
101

edits