Advertisement
Guest User

Untitled

a guest
Mar 9th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.40 KB | None | 0 0
  1. ;;; Public API entry point.
  2. ;;; This is just a convenient wrapper
  3. (defun duplicate-entries (l)
  4.   (duplicate-entries-internal l l))
  5.  
  6. ;;; For all each item in items, check and see if there are more than
  7. ;;; once instance of item in l.
  8. (defun duplicate-entries-internal (items l)
  9.   ;; n - the # of instances of an item
  10.   ;; item - the current item
  11.   (let ((n 0) (item (car items)))
  12.  
  13.     ;; Define a function to count the # of instance of a.
  14.     ;;
  15.     ;; This is used as a predicate for member function to count the
  16.     ;; number of items. We *only* return t if there n > 1. This makes
  17.     ;; member function return a non-nil list iff there are more than one
  18.     ;; instance of a.
  19.     (defun more-than-one(a b)
  20.       (if (equal a b)
  21.         (if (> (incf n) 1)
  22.           t)))
  23.  
  24.     ;; Make sure that we have something to count.
  25.     (if (> (list-length items) 0)
  26.       ;; Are there more than one instance of item in l?
  27.       (if (equal (member item l :test #'more-than-one) nil)
  28.         ;; If not, check other items.
  29.         (duplicate-entries-internal (rest items) l)
  30.         ;; Otherwise, we found an item with more than one instance.
  31.         t))))
  32.  
  33. ;; Test and print the results
  34. (defun test (in) (format t "~3S = ~S~%" (duplicate-entries in) in))
  35. ;; A sample test set
  36. (setf testset '((a b a c d) (a b (a) c d) ((a b) b c (a b))))
  37. ;; Run duplicate-entries over all test samples
  38. (mapcar #'test testset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement