Jump to content

Section of List


Lee Mac

Recommended Posts

  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    10

  • uddfl

    10

  • CAB

    9

  • Se7en

    4

Top Posters In This Topic

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))
)

Link to comment
Share on other sites

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)))))
  )
)

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. (?)

Link to comment
Share on other sites

  • 1 month later...

(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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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)
     )
   )
 )

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

Oops, I had it backwards. :oops:

 

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...