Remove duplicate elements: Difference between revisions

Line 958:
 
=={{header|CafeOBJ}}==
The parametrized module NO-DUP-LIST(ELEMENTS :: TRIV) defines the signature of simple Haskellspace likeseparated 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:
<lang> red (1 2 3 4) (3 2 1 5) .
--> (4 5 1 2 3):Int
Line 969:
<lang CafeOBJ>
mod! NO-DUP-LIST(ELEMENTS :: TRIV) {
op _,___ : Elt Elt -> Elt { comm assoc idem assoc }
}
 
Line 976:
-- Test on lists of INT
open NO-DUP-LIST(INT) .
red 2 , 1 , 2 , 1 , 2 , 1 , 3 .
-- Gives (2 , 1 , 3):Int
open NO-DUP-LIST(INT) .
reduce 1 , 1 , 2 , 1 , 1 .
close
open NO-DUP-LIST(CHARACTER) .
reduce 'a' , 'b' , 'a' , 'a' .
close
open NO-DUP-LIST(STRING) .
reduce "abc" , "def" , "abc" , "abc" , "abc" .
close
</lang>
Line 994:
The evaluation automatically uses right associativity. So starting with
 
<lang> (1 , 1 , 2 , 1 , 1)</lang>
 
The system places appropriate brackets on the entire expression:
 
<lang> (1 , ((1 , 2) , (1 , 1))) </lang>
 
By idempotency applied at rightmost brackets:
We get rewrite ''(1 , 1) -> 1'' . The term is now:
 
<lang> (1 , ((1 , 2) , 1)) </lang>
 
Any further occurrence of 1 will be removed.
By idempotency applied at inner brackets ''(1 , 2) ''.
We get rewrite ''(1 , 2) -> 2 ''. Term is now:
 
<lang>(1 , (2 , 1))</lang>
 
By already established idempotency we finally get
 
<lang>(2 , 1)</lang>
 
=={{header|Ceylon}}==
101

edits