• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
      • History
      • Parallelism
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Memoize
        • Mbe
        • Io
        • Apply$
        • Defpkg
        • Mutual-recursion
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Loop$-primer
        • Fast-alists
        • Defmacro
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Irrelevant-formals
        • Efficiency
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
          • Member
          • Append
          • Nth
          • List
          • Len
          • True-listp
          • Symbol-listp
          • String-listp
          • Nat-listp
          • Character-listp
          • True-list-listp
          • Length
          • Search
          • Intersection$
          • Union$
          • Remove-duplicates
            • Nat-list-remove-duplicates
            • Hons-remove-duplicates
            • Std/lists/remove-duplicates-equal
          • Position
          • Take
          • Update-nth
          • Set-difference$
          • Subsetp
          • No-duplicatesp
          • Concatenate
          • Remove
          • Nthcdr
          • Remove1
          • Intersectp
          • Endp
          • Keyword-value-listp
          • Reverse
          • List-utilities
          • Add-to-set
          • Set-size
          • Integer-listp
          • Revappend
          • Subseq
          • Make-list
          • Lists-light
          • Butlast
          • Pairlis$
          • Substitute
          • Count
          • Boolean-listp
          • List*
          • Keyword-listp
          • Eqlable-listp
          • Last
          • Integer-range-listp
          • Pos-listp
          • Rational-listp
          • Evens
          • Atom-listp
          • ACL2-number-listp
          • Good-atom-listp
          • Typed-list-utilities
          • Listp
          • Odds
          • Standard-char-listp
          • Last-cdr
          • Pairlis
          • Proper-consp
          • Improper-consp
          • Pairlis-x2
          • Pairlis-x1
          • Merge-sort-lexorder
          • Fix-true-list
          • Real-listp
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Defmacro-untouchable
        • Primitive
        • <<
        • Revert-world
        • Set-duplicate-keys-action
        • Unmemoize
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Defopen
        • Sleep
      • Start-here
      • Real
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • Lists
  • Strings
  • ACL2-built-ins

Remove-duplicates

Remove duplicates from a string or a list

General Forms:
(remove-duplicates x)
(remove-duplicates x :test 'eql)   ; same as above (eql as equality test)
(remove-duplicates x :test 'eq)    ; same, but eq is equality test
(remove-duplicates x :test 'equal) ; same, but equal is equality test

(Remove-duplicates x) returns the result of deleting duplicate elements from the beginning of the list or string x. For example, (remove-duplicates '(1 2 3 2 4)) is equal to '(1 3 2 4). The optional keyword, :TEST, has no effect logically, but provides the test (default eql) used for comparing x with successive elements of lst.

The guard for a call of remove-duplicates depends on the test. In all cases, the argument must satisfy stringp or true-listp. If the test is eql, then the argument must satisfy either stringp or eqlable-listp. If the test is eq, then the argument must satisfy symbol-listp.

The relation between remove-duplicates and its variants is related to the usual pattern for equality variants; see equality-variants. However, the possibility of a string argument changes the usual pattern a bit. As one might expect:

(remove-duplicates-eq lst) is equivalent to (remove-duplicates lst :test 'eq).

However, remove-duplicates-equal is defined without consideration of strings, for backward compatibility with versions of ACL2 through Version_4.2. The macro remove-duplicates-logic has been introduced to model the behavior of remove-duplicates even on strings; use :pe if you wish to see its definition. So we can say the following.

(remove-duplicates-logic lst) is equivalent to (remove-duplicates lst :test 'equal); and

(remove-duplicates-logic lst) is equal to (remove-duplicates-equal lst) when lst is not a string.

In particular, when the argument is not a string, reasoning about any of these primitives reduces to reasoning about the function remove-duplicates-equal.

Function: remove-duplicates-equal

(defun remove-duplicates-equal (l)
       (declare (xargs :guard (true-listp l)))
       (cond ((endp l) nil)
             ((member-equal (car l) (cdr l))
              (remove-duplicates-equal (cdr l)))
             (t (cons-with-hint (car l)
                                (remove-duplicates-equal (cdr l))
                                l))))

Remove-duplicates is defined by Common Lisp. See any Common Lisp documentation for more information.

Subtopics

Nat-list-remove-duplicates
High-performance duplicate-removal function for nat-listps.
Hons-remove-duplicates
(hons-remove-duplicates l) removes duplicate elements from a list, and is implemented using fast-alists.
Std/lists/remove-duplicates-equal
Lemmas about remove-duplicates-equal available in the std/lists library.