Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; Public API entry point.
- ;;; This is just a convenient wrapper
- (defun duplicate-entries (l)
- (duplicate-entries-internal l l))
- ;;; For all each item in items, check and see if there are more than
- ;;; once instance of item in l.
- (defun duplicate-entries-internal (items l)
- ;; n - the # of instances of an item
- ;; item - the current item
- (let ((n 0) (item (car items)))
- ;; Define a function to count the # of instance of a.
- ;;
- ;; This is used as a predicate for member function to count the
- ;; number of items. We *only* return t if there n > 1. This makes
- ;; member function return a non-nil list iff there are more than one
- ;; instance of a.
- (defun more-than-one(a b)
- (if (equal a b)
- (if (> (incf n) 1)
- t)))
- ;; Make sure that we have something to count.
- (if (> (list-length items) 0)
- ;; Are there more than one instance of item in l?
- (if (equal (member item l :test #'more-than-one) nil)
- ;; If not, check other items.
- (duplicate-entries-internal (rest items) l)
- ;; Otherwise, we found an item with more than one instance.
- t))))
- ;; Test and print the results
- (defun test (in) (format t "~3S = ~S~%" (duplicate-entries in) in))
- ;; A sample test set
- (setf testset '((a b a c d) (a b (a) c d) ((a b) b c (a b))))
- ;; Run duplicate-entries over all test samples
- (mapcar #'test testset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement