Checking equality on multiple elements

Checking equality on multiple elements

davidkoh007
Contributor Contributor
1,165 Views
7 Replies
Message 1 of 8

Checking equality on multiple elements

davidkoh007
Contributor
Contributor

Hi, how can I check, if any elements from list are equal to each other ?

 

(setq 
   c0 cv_RP 
   c1 cv_RPH
   c2 cv_RPD
   c3 cv_RPN
   c4 cv_HB 
   c5 cv_HB
)
; I want to be able to do smth like
(equal c0 c1 c2 c3 c4 c5)
T

 

Thanks!

0 Likes
Accepted solutions (2)
1,166 Views
7 Replies
Replies (7)
Message 2 of 8

ВeekeeCZ
Consultant
Consultant

It depends what you're about to compare.

But the '= function allows you to add multiple arguments.

(= c0 c1 c2 ...)

0 Likes
Message 3 of 8

davidkoh007
Contributor
Contributor

Thanks for the reply,  the cv_xxx represent already predefined strings.

 

	(setq 
		cv_RP "surface edge"
		cv_RPH "terrain edge"
		cv_RPD "road"
		cv_RPN "sidewalk"
		cv_HB "building"
etc...)

I want to check for duplicates basically, if there is any, just return true,

0 Likes
Message 4 of 8

ВeekeeCZ
Consultant
Consultant
Accepted solution

Not really sure what you want.

I would use the member or vl-position functions.

Or maybe THIS Lee's functions can help you with duplicates...

0 Likes
Message 5 of 8

Moshe-A
Mentor
Mentor

@davidkoh007  hi,

 

check this one:

 

usage:

(equality c4 (list cv_rp cv_rph cv_rpd cv_rpn cv_hb cv_hb))

returns:

("cv_HB" "cv_HB")

 

enjoy

moshe

 

 

 

 

(defun initialize (vars^ vals^)
 (mapcar
  '(lambda (var val)
    (set var val)
   )
   vars^ vals^
 )
); initialize 

(initialize (list 'cv_RP 'cv_RPH 'cv_RPD 'cv_RPN 'cv_HB)
(list "cv_RP" "cv_RPH" "cv_RPD" "cv_RPN" "cv_HB")
) (initialize (list 'c0 'c1 'c2 'c3 'c4 'c5)
(list cv_RP cv_RPH cv_RPD cv_RPN cv_HB cv_HB) )
(defun equality (item lst) (vl-remove-if-not '(lambda (x) (eq x item) ) lst ) ); equality

 

 

 

 

0 Likes
Message 6 of 8

davidkoh007
Contributor
Contributor

Used this method in the end, thanks a bunch! 

 

(defun LM:Unique-p ( l )
    (vl-every (function (lambda ( x ) (not (member x (setq l (cdr l)))))) l)
)
0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@davidkoh007 wrote:

....

I want to check for duplicates basically, if there is any, just return true,


 

If that's really all you want to know, and you don't care which one(s) is/are duplicated, nor how many of them are, nor how many duplicates there are of any of them, here's another way:

 

(defun AnyDups (lst / dup)
  (foreach item lst
    (if (/= (length (vl-remove item lst)) (1- (length lst)))
      (setq dup T)
    ); if
  ); foreach
  dup
); defun

Usage -- function in parentheses with list as argument:  (AnyDups YourList)

 

Returns T if there are any duplicates, nil if not.  Works with strings and/or numbers.

Kent Cooper, AIA
0 Likes
Message 8 of 8

Moshe-A
Mentor
Mentor

@davidkoh007 hi,

 

check this (duplicate) function i made

it will return nil if no duplicate items found but if found? it will return a list where the first element is the item in question along with items index found - very cool

 

usage:

(duplicate c4 (list c0 c1 c2 c3 c4 c5))

("building" 4 5)

 

enjoy

moshe

 

 

 

(defun initialize (vars^ vals^)
 (mapcar
  '(lambda (var val)
    (set var val)
   )
   vars^ vals^
 )
); init  


(initialize (list 'cv_RP 'cv_RPH 'cv_RPD 'cv_RPN 'cv_HB) (list "surface edge" "terrain edge" "road" "sidewalk" "building"))
(initialize (list 'c0 'c1 'c2 'c3 'c4 'c5) (list cv_RP cv_RPH cv_RPD cv_RPN cv_HB cv_HB))

 

 

 

(defun upto-elem (item lst / i p tmp^)
 (setq tmp^ '() i 0 p (vl-position item lst)) 
 (while (<= i p)
  (setq tmp^ (cons (nth i lst) tmp^) i (1+ i))
 )
 (reverse tmp^) 
); upto-elem


(defun subst-p (n-item e-item lst)
 (append (reverse (cdr (reverse (upto-elem e-item lst)))) (list n-item) (cdr (member e-item lst))) 
); subst-p


(defun duplicate (item lst / l)
 (setq l (vl-remove-if
          '(lambda (x)
            (not x)
           )
           (mapcar 
            '(lambda (x / tmp^) 
              (if (eq x item)
               (progn  
	        (setq tmp^ lst lst (subst-p nil x lst))
                (vl-position x tmp^)
               ); progn
              ); if
            ); lambda
            lst
           ); mapcar
         ); vl-remove-if
 ); setq

 (if (> (length l) 1)
  (cons item l)
 )
); duplicate

 

 

0 Likes