• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Recursion-and-induction
      • Hons-and-memoization
      • Events
      • Parallelism
      • History
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Mutual-recursion
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Defmacro
        • Loop$-primer
        • Fast-alists
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Efficiency
        • Irrelevant-formals
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
          • Std/strings
          • String-listp
          • Stringp
          • Length
          • Search
          • Remove-duplicates
            • Nat-list-remove-duplicates
            • Hons-remove-duplicates
            • Std/lists/remove-duplicates-equal
          • Position
          • Coerce
          • Concatenate
          • Reverse
          • String
          • Subseq
          • Substitute
          • String-upcase
          • String-downcase
          • Count
          • Char
          • String<
          • String-equal
          • String-utilities
          • String-append
          • String>=
          • String<=
          • String>
          • Hex-digit-char-theorems
          • String-downcase-gen
          • String-upcase-gen
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Oracle-eval
        • Defmacro-untouchable
        • <<
        • Primitive
        • Revert-world
        • Unmemoize
        • Set-duplicate-keys-action
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Fake-oracle-eval
        • Defopen
        • Sleep
      • Operational-semantics
      • Real
      • Start-here
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Math
    • Testing-utilities
  • 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.