help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lists composed of equal number and kind of elements


From: Pascal J. Bourguignon
Subject: Re: Lists composed of equal number and kind of elements
Date: Tue, 28 Jul 2015 19:10:30 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> is there a handy way to check if two lists --results of (window-list)
> precisely-- are equal WRT to the kind and number elements?
>
> The order of elements should be ignored.

(require 'cl)
(setf lexical-binding t)

(defun* set-equal (a b &key (test (function eql)))
  (and (subsetp a b :test test)
       (subsetp b a :test test)))

(defun equivalence-classes (set equalf)
  (loop with classes = (quote ())
     for item in set
     for class = (car (member* item classes
                               :test equalf  :key (function second)))
     do (if class
            (push item (cdr class))
            (push (list :class item) classes))
     finally (return (mapcar (function cdr) classes))))

(defun* equal-wrt-kind-and-number-of-elements
    (list1 list2 &key (kind 'type-of) (test 'equal))
  (flet ((reduce-to-classes (list)
           (mapcar (lambda (class) (list (first class) (length class)))
                   (equivalence-classes (mapcar kind list) test))))
    (and (= (length list1) (length list2))
         (set-equal (reduce-to-classes list1)
                    (reduce-to-classes list2)
                    :test (lambda (c1 c2)
                            (and (= (second c1) (second c2))
                                 (funcall test (first c1) (first c2))))))))



(equal-wrt-kind-and-number-of-elements '("a" 1 2 b c d)
                                       '(x 3 y 4 z "zz"))
t

(equal-wrt-kind-and-number-of-elements '("a" 1 2 b c d)
                                       '(x 3 y 4 z xx))
nil

(equal-wrt-kind-and-number-of-elements '("a" 1 2 b c d)
                                       '(x 3 y 4 z "a" "b"))
nil



-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]