CAB Posted March 10, 2009 Posted March 10, 2009 and another: _$ (vl-remove-if '(lambda(x) (not (equal 3 x))) lst) (3 3) Quote
uddfl Posted March 10, 2009 Posted March 10, 2009 Got it. For what I need, which is to cross-compare all elements and retrieve the value of the repeated element, I am still using a repeat loop. But please critique and let me know if there is a simpler way. (setq a 10 b 20 b 20 c 30) (setq alist (list a b b c)) (setq list-index 0 listlength (length alist)) ;;; determine if two elements of the points list are equal (repeat listlength (setq element (nth list-index alist)) (setq list-index 0) (mapcar '(lambda (x) (if (eq x element) (setq repeated_item x) ) ) alist) (setq list-index (1+ list-index)) ) Quote
uddfl Posted March 10, 2009 Posted March 10, 2009 and another: _$ (vl-remove-if '(lambda(x) (not (equal 3 x))) lst) (3 3) Boy, that is a shortcut Quote
CAB Posted March 10, 2009 Posted March 10, 2009 If you just want to see if that value is contained in the list you can use these: (vl-some '(lambda(x) (equal 3 x)) lst) T _$ (vl-position 3 lst) 2 _$ (member 3 lst) (3 3 4 5) as long as the return valus is not nil you have a valid test (if (member 3 lst) (progn (princ "\nYes there is a 3 in the list) (princ (strcat "\nYou can find it at position #" (itoa (vl-position 3 lst))))) ) ) Quote
CAB Posted March 10, 2009 Posted March 10, 2009 (setq lst1 '(1 2 3 4 5)) (setq lst2 '(5 9 3 2 0 ) --------------------------------------------- **>> Note: ignores extra items in lst2 --------------------------------------------- (defun Return_Unique_Items (lst1 lst2) (vl-remove-if '(lambda (x) (member x lst2)) lst1) ) ;Returns: (1 4) (defun Return_Same_Items (lst1 lst2) (vl-remove-if-not '(lambda (x) (member x lst2)) lst1) ) ;Returns: (2 3 5) --------------------------------------------- >>---> This addresses the problem ******* --------------------------------------------- (defun Return_Unique_Items2 (lst1 lst2) (append (vl-remove-if '(lambda (x) (member x lst1)) lst2) (vl-remove-if '(lambda (x) (member x lst2)) lst1)) ) (defun Return_Unique_Items2 (lst1 lst2) (mapcar 'Return_Unique_Items (list lst1 lst2) (list lst2 lst1)) ) ;Returns: ((1 4) (9 0 ) Here is my source: http://www.theswamp.org/index.php?topic=5119.0 Quote
uddfl Posted March 10, 2009 Posted March 10, 2009 Lambda is just an anonymous function. Where "(defun name ( / )" is a named function, replace all that with "(lambda ( / )" and you got yourself a function without a name. If you need a function several time throughout a program you give it a name right? (defun name ( / )... Well if you need a function only once you can use use (lambda ( / ) in it place. get it? Yes, I think I understand now. So if i did happen to have a defined function for what I'm doing inside of mapcar, I would call that function and its arguments inside of mapcar, but if the function is only within the mapcar function, lambda serves as an anonymous, or 'temporary' function. Amirite? If you just want to see if that value is contained in the list you can use these: (vl-some '(lambda(x) (equal 3 x)) lst) T _$ (vl-position 3 lst) 2 _$ (member 3 lst) (3 3 4 5) as long as the return valus is not nil you have a valid test (if (member 3 lst) (progn (princ "\nYes there is a 3 in the list) (princ (strcat "\nYou can find it at position #" (itoa (vl-position 3 lst))))) ) ) (setq lst1 '(1 2 3 4 5)) (setq lst2 '(5 9 3 2 0 ) --------------------------------------------- **>> Note: ignores extra items in lst2 --------------------------------------------- (defun Return_Unique_Items (lst1 lst2) (vl-remove-if '(lambda (x) (member x lst2)) lst1) ) ;Returns: (1 4) (defun Return_Same_Items (lst1 lst2) (vl-remove-if-not '(lambda (x) (member x lst2)) lst1) ) ;Returns: (2 3 5) --------------------------------------------- >>---> This addresses the problem ******* --------------------------------------------- (defun Return_Unique_Items2 (lst1 lst2) (append (vl-remove-if '(lambda (x) (member x lst1)) lst2) (vl-remove-if '(lambda (x) (member x lst2)) lst1)) ) (defun Return_Unique_Items2 (lst1 lst2) (mapcar 'Return_Unique_Items (list lst1 lst2) (list lst2 lst1)) ) ;Returns: ((1 4) (9 0 ) Here is my source: http://www.theswamp.org/index.php?topic=5119.0 I will have to take some time and study this. I am not very good with vl- functions, just not-so-illiterate in regular AutoLISP. Thank you, CAB. BTW I registered at your site there^ like a week ago and I still can't login... do you know why? Quote
Se7en Posted March 10, 2009 Posted March 10, 2009 Yes, I think I understand now. So if i did happen to have a defined function for what I'm doing inside of mapcar, I would call that function and its arguments inside of mapcar, but if the function is only within the mapcar function, lambda serves as an anonymous, or 'temporary' function. Amirite? Shall we find out? (setq alist '(a b b c) inx 0) (defun equal? ( x ) (if (eq x 'b) (setq inx (1+ inx))) ) (mapcar 'equal? alist) Yep, it works. (setq alist '(a b b c) inx 0) (defun equal? ( x y ) (if (eq x y) (setq inx (1+ inx))) ) (mapcar 'equal? 'b alist) Nope! Can you tell me why the last one doesn't work? Quote
uddfl Posted March 10, 2009 Posted March 10, 2009 Shall we find out? (setq alist '(a b b c) inx 0) (defun equal? ( x ) (if (eq x 'b) (setq inx (1+ inx))) ) (mapcar 'equal? alist) Yep, it works. (setq alist '(a b b c) inx 0) (defun equal? ( x y ) (if (eq x y) (setq inx (1+ inx))) ) (mapcar 'equal? 'b alist) Nope! Can you tell me why the last one doesn't work? I just had a flashback of when I was in 3rd grade... The only reason I know the second one doesn't work is because 'b is not a list, and mapcar requires the quoted function to be applied directly to a list or lists. (?) Quote
CAB Posted March 10, 2009 Posted March 10, 2009 uddfl Try again making sure you provide the correct email info and let me you when they sign up again. Thanks CAB Quote
uddfl Posted April 14, 2009 Posted April 14, 2009 (setq a 10 b 20 b 20 c 30) (setq alist (list a b b c)) (setq list-index 0 listlength (length alist)) ;;; determine if two elements of the points list are equal (repeat listlength (setq element (nth list-index alist)) (setq list-index 0) (mapcar '(lambda (x) (if (eq x element) (setq repeated_item x) ) ) alist) (setq list-index (1+ list-index)) ) Something isn't right. I am trying to retrieve the value of list elements which may be equal. For instance, the example above should return "20". It does not. Sorry to bump this, but I am still not doing this right and I need help. Quote
Lee Mac Posted April 14, 2009 Author Posted April 14, 2009 I would just uses something like this: (defun Return_Duplicates (alist / dLst lst2) (foreach x alist (if (member x lst2) (setq dLst (cons x dLst)) (setq lst2 (cons x lst2)))) dLst) (Return_Duplicates '(1 2 2 3)) (2) (Return_Duplicates '(1 2 2 3 4 5 4)) (4 2) Quote
uddfl Posted April 14, 2009 Posted April 14, 2009 I would just uses something like this: (defun Return_Duplicates (alist / dLst lst2) (foreach x alist (if (member x lst2) (setq dLst (cons x dLst)) (setq lst2 (cons x lst2)))) dLst) (Return_Duplicates '(1 2 2 3)) (2) (Return_Duplicates '(1 2 2 3 4 5 4)) (4 2) Perfect, Lee. Thank you very much. Quote
Lee Mac Posted April 14, 2009 Author Posted April 14, 2009 Perfect, Lee. Thank you very much. No Probs, happy to help dude Quote
CAB Posted April 14, 2009 Posted April 14, 2009 Another version. ;; CAB 12/02/05 (defun unique (lst / result) (while (setq itm (car lst)) (setq lst (vl-remove itm lst) result (cons itm result) ) ) ) Quote
CarlB Posted April 14, 2009 Posted April 14, 2009 I tried Lee's; if there are 3 duplicates, it is in the duplicate list twice. i tried CAB's; his doesn't return duplicates, rather a list with duplicates removed. Mine? I got nothing, just a critic for now Quote
CAB Posted April 15, 2009 Posted April 15, 2009 Oops, I had it backwards. Try this: (defun doups (lst / result) (while (setq x (car lst)) (if (vl-position x (setq lst (cdr lst))) (setq result (cons x result) lst (vl-remove x lst)) ) ) result ) Quote
Lee Mac Posted April 15, 2009 Author Posted April 15, 2009 Nice one CAB - I hadn't accounted for more than two occurrences Quote
uddfl Posted April 15, 2009 Posted April 15, 2009 For the particular application I am working on, Lee's function worked OK because I always have only one value that is repeated. But thank you all for your help. Quote
Lee Mac Posted April 15, 2009 Author Posted April 15, 2009 Just out of interest CAB, am I right in saying the code would perform identically with "member" instead of "vl-position"? [b][color=BLACK]([/color][/b]defun doups [b][color=FUCHSIA]([/color][/b]lst / result[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq x [b][color=MAROON]([/color][/b]car lst[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]if [b][color=MAROON]([/color][/b]member x [b][color=GREEN]([/color][/b]setq lst [b][color=BLUE]([/color][/b]cdr lst[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]setq result [b][color=GREEN]([/color][/b]cons x result[b][color=GREEN])[/color][/b] lst [b][color=GREEN]([/color][/b]vl-remove x lst[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] result[b][color=BLACK])[/color][/b] Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.