1
0
Fork 0
cl-sites/novaspec.org/cl/f_remove-duplicates.html
2025-02-05 18:52:26 +01:00

601 lines
No EOL
12 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>remove-duplicates, delete-duplicates | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_remove-duplicates.html"
><LINK REL="next" HREF="18_Hash_Tables.html" TYPE="text/html" TITLE="18. Hash Tables"
><LINK REL="prev" HREF="f_remove.html" TYPE="text/html" TITLE="remove, remove-if, remove-if-not, delete, delete-if, delete-if-not"
><LINK REL="up" HREF="17_3_Sequences_Dictionary.html" TYPE="text/html" TITLE="17.3 Sequences Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="17_Sequences.html"
>17. Sequences</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="17_3_Sequences_Dictionary.html"
>17.3 Sequences Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_remove-duplicates.html"
>remove-duplicates, delete-duplicates</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_remove.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="18_Hash_Tables.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="remove-duplicates, delete-duplicates"
></SPAN
><B
>remove-duplicates, delete-duplicates</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><TABLE CELLPADDING="0" CELLSPACING="0"
><TR
><TD VALIGN="BASELINE" ALIGN="LEFT" NOWRAP="NOWRAP"
><B
>remove-duplicates</B
><SPAN CLASS="cmssi"
>&#160;</SPAN
><SPAN CLASS="cmssi"
>sequence</SPAN
><SPAN CLASS="cmtt"
>&#160;</SPAN
><SPAN CLASS="cmtt"
>&amp;key</SPAN
><SPAN CLASS="cmssi"
>&#160;</SPAN
></TD
><TD VALIGN="BASELINE" ALIGN="LEFT"
><SPAN CLASS="cmssi"
>from-end</SPAN
> <SPAN CLASS="cmssi"
>test</SPAN
> <SPAN CLASS="cmssi"
>test-not</SPAN
><BR
><SPAN CLASS="cmssi"
>start</SPAN
> <SPAN CLASS="cmssi"
>end</SPAN
> <SPAN CLASS="cmssi"
>key</SPAN
></TD
></TR
></TABLE
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmssi"
>result-sequence</SPAN
></DIV
><TABLE CELLPADDING="0" CELLSPACING="0"
><TR
><TD VALIGN="BASELINE" ALIGN="LEFT" NOWRAP="NOWRAP"
><B
>delete-duplicates</B
><SPAN CLASS="cmssi"
>&#160;</SPAN
><SPAN CLASS="cmssi"
>sequence</SPAN
><SPAN CLASS="cmtt"
>&#160;</SPAN
><SPAN CLASS="cmtt"
>&amp;key</SPAN
><SPAN CLASS="cmssi"
>&#160;</SPAN
></TD
><TD VALIGN="BASELINE" ALIGN="LEFT"
><SPAN CLASS="cmssi"
>from-end</SPAN
> <SPAN CLASS="cmssi"
>test</SPAN
> <SPAN CLASS="cmssi"
>test-not</SPAN
><BR
><SPAN CLASS="cmssi"
>start</SPAN
> <SPAN CLASS="cmssi"
>end</SPAN
> <SPAN CLASS="cmssi"
>key</SPAN
></TD
></TR
></TABLE
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmssi"
>result-sequence</SPAN
></DIV
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>sequence</VAR
> &#8212; a <A HREF="26_1_Glossary.html#proper_sequence"
><EM CLASS="term"
>proper sequence</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>from-end</VAR
> &#8212; a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. The default is <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>test</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> of two <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>arguments</EM
></A
> that returns a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>test-not</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> of two <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>arguments</EM
></A
> that returns a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>start</VAR
>, <VAR CLASS="param"
>end</VAR
> &#8212; <A HREF="26_1_Glossary.html#bounding_index_designator"
><EM CLASS="term"
>bounding index designators</EM
></A
> of <VAR CLASS="param"
>sequence</VAR
>. The defaults for <VAR CLASS="param"
>start</VAR
> and <VAR CLASS="param"
>end</VAR
> are <CODE CLASS="f"
>0</CODE
> and <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, respectively. </P
><P CLASS="j"
><VAR CLASS="param"
>key</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> of one argument, or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
><VAR CLASS="param"
>result-sequence</VAR
> &#8212; a <A HREF="26_1_Glossary.html#sequence"
><EM CLASS="term"
>sequence</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>remove-duplicates</B
></A
> returns a modified copy of <VAR CLASS="param"
>sequence</VAR
> from which any element that matches another element occurring in <VAR CLASS="param"
>sequence</VAR
> has been removed. </P
><P CLASS="j"
>If <VAR CLASS="param"
>sequence</VAR
> is a <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
>, the result is a <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
> that has the same <A HREF="26_1_Glossary.html#actual_array_element_type"
><EM CLASS="term"
>actual array element type</EM
></A
> as <VAR CLASS="param"
>sequence</VAR
>. If <VAR CLASS="param"
>sequence</VAR
> is a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>, the result is a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. </P
><P CLASS="j"
><A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>delete-duplicates</B
></A
> is like <A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>remove-duplicates</B
></A
>, but <A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>delete-duplicates</B
></A
> may modify <VAR CLASS="param"
>sequence</VAR
>. </P
><P CLASS="j"
>The elements of <VAR CLASS="param"
>sequence</VAR
> are compared <A HREF="26_1_Glossary.html#pairwise"
><EM CLASS="term"
>pairwise</EM
></A
>, and if any two match, then the one occurring earlier in <VAR CLASS="param"
>sequence</VAR
> is discarded, unless <VAR CLASS="param"
>from-end</VAR
> is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>, in which case the one later in <VAR CLASS="param"
>sequence</VAR
> is discarded. </P
><P CLASS="j"
><A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>remove-duplicates</B
></A
> and <A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>delete-duplicates</B
></A
> return a <A HREF="26_1_Glossary.html#sequence"
><EM CLASS="term"
>sequence</EM
></A
> of the same <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> as <VAR CLASS="param"
>sequence</VAR
> with enough elements removed so that no two of the remaining elements match. The order of the elements remaining in the result is the same as the order in which they appear in <VAR CLASS="param"
>sequence</VAR
>. </P
><P CLASS="j"
><A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>remove-duplicates</B
></A
> returns a <A HREF="26_1_Glossary.html#sequence"
><EM CLASS="term"
>sequence</EM
></A
> that may share with <VAR CLASS="param"
>sequence</VAR
> or may be <A HREF="26_1_Glossary.html#identical"
><EM CLASS="term"
>identical</EM
></A
> to <VAR CLASS="param"
>sequence</VAR
> if no elements need to be removed. </P
><P CLASS="j"
><A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>delete-duplicates</B
></A
>, when <VAR CLASS="param"
>sequence</VAR
> is a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>, is permitted to <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> any part, <A HREF="f_car.html" CLASS="funref"
><B
>car</B
></A
> or <A HREF="f_car.html" CLASS="funref"
><B
>cdr</B
></A
>, of the top-level list structure in that <VAR CLASS="param"
>sequence</VAR
>. When <VAR CLASS="param"
>sequence</VAR
> is a <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
>, <A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>delete-duplicates</B
></A
> is permitted to change the dimensions of the <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
> and to slide its elements into new positions without permuting them to produce the resulting <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(remove-duplicates "aBcDAbCd" :test #'char-equal :from-end t) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "aBcD"
(remove-duplicates '(a b c b d d e)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (A C B D E)
(remove-duplicates '(a b c b d d e) :from-end t) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (A B C D E)
(remove-duplicates '((foo #\a) (bar #\%) (baz #\A))
:test #'char-equal :key #'cadr) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((BAR #\%) (BAZ #\A))
(remove-duplicates '((foo #\a) (bar #\%) (baz #\A))
:test #'char-equal :key #'cadr :from-end t) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((FOO #\a) (BAR #\%))
(setq tester (list 0 1 2 3 4 5 6))
(delete-duplicates tester :key #'oddp :start 1 :end 6) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (0 4 5 6)</PRE
></DD
><DT
><B
>Side Effects</B
></DT
><DD
><P CLASS="j"
><A HREF="f_remove-duplicates.html" CLASS="funref"
><B
>delete-duplicates</B
></A
> might destructively modify <VAR CLASS="param"
>sequence</VAR
>.</P
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>Should signal an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> if <VAR CLASS="param"
>sequence</VAR
> is not a <A HREF="26_1_Glossary.html#proper_sequence"
><EM CLASS="term"
>proper sequence</EM
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="3_2_Compilation.html#sec_3_2_1" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.2.1</SPAN
> <SPAN CLASS="cmr"
>(Compiler</SPAN
> <SPAN CLASS="cmr"
>Terminology)</SPAN
></A
>, <A HREF="3_6_Traversal_Rules_and_Side_Effects.html#sec_3_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.6</SPAN
> <SPAN CLASS="cmr"
>(Traversal</SPAN
> <SPAN CLASS="cmr"
>Rules</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Side</SPAN
> <SPAN CLASS="cmr"
>Effects)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>If <VAR CLASS="param"
>sequence</VAR
> is a <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
>, the result might or might not be simple, and might or might not be <A HREF="26_1_Glossary.html#identical"
><EM CLASS="term"
>identical</EM
></A
> to <VAR CLASS="param"
>sequence</VAR
>. </P
><P CLASS="j"
>The <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:test-not</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>argument</EM
></A
> is deprecated. </P
><P CLASS="j"
>These functions are useful for converting <VAR CLASS="param"
>sequence</VAR
> into a canonical form suitable for representing a set.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_remove.html" CLASS="prev"
>&#8592;</A
><A HREF="18_Hash_Tables.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>