Author Topic: list comparison  (Read 10961 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
list comparison
« on: July 07, 2005, 03:28:23 PM »
I have some lists
(1 11)
(5 11 45)
I want to compare these lists to return t or nil if either number in the first list is = to any number in the second list. I know this can be done, but I'm frying my brain trying to remember how. :evil: Please help. I've tried different variations of member, mapcar, and foreach, but all attempts have resulted nil when it's obvious that there is a match.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
list comparison
« Reply #1 on: July 07, 2005, 03:33:52 PM »
Consider --

Code: [Select]
(mapcar    
   '(lambda (x) (and (member x '(5 11 45))))
   '(1 11)
)

Now rewrite it as a function that takes two arguments.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
list comparison
« Reply #2 on: July 07, 2005, 03:37:01 PM »
Wasnt this just mentioned? ...Yep! Heres my example from the post.  (I'll post my example code and then i'll go get the link for ya.)

Code: [Select]

(defun test (lst-c lst-m / inlist)
 
  (defun inlist (? lst)
    (and (member ? lst)) )
 
  (cond
    ((inlist (car lst-c) lst-m)
     (alert "Found a match")
     (test (cdr lst-c) lst-m))
    ( T (cond
          ((<= (length lst-c) 1)
           "End of list reached"
           )
          ( T (test (cdr lst-c) lst-m))))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
list comparison
« Reply #3 on: July 07, 2005, 03:37:02 PM »
which is exactly what I intend to do. Thank you. I'm still trying to get my mind around the various ways to use member, mapcar and lambda.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
list comparison
« Reply #4 on: July 07, 2005, 03:38:48 PM »
Holly cow. You guys move fast!

Well anyways, heres the link i promised.

EDIT: *sigh* (I forgot the link!??!)

http://www.theswamp.org/phpBB2/viewtopic.php?t=5656
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
list comparison
« Reply #5 on: July 07, 2005, 03:39:43 PM »
Quote from: daron
which is exactly what I intend to do. Thank you. I'm still trying to get my mind around the various ways to use member, mapcar and lambda.

My pleasure. Don't forget about the power in the apply, catch-all-apply, foreach etc. functions too.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

daron

  • Guest
list comparison
« Reply #6 on: July 07, 2005, 03:43:46 PM »
Here's the answer to your request:
Code: [Select]
(defun comparison (grlist complist)
     (mapcar
 '(lambda (x) (and (member x complist)))
 grlist
     )
)


John, funny you posted that code. I was searching before posting, but I think my mind was so wrapped up in how I was failing, that I didn't think it would apply. Thanks to both of you.

daron

  • Guest
list comparison
« Reply #7 on: July 07, 2005, 03:45:26 PM »
MP, I'll have to look at the apply's, but I was also trying foreach with no luck. End of the day stress I think and lack of coding for months.

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
list comparison
« Reply #8 on: July 07, 2005, 03:47:49 PM »
Here's one that uses foreach & member...
Code: [Select]

(defun inList? (l1 l2 / res)
  (foreach x l1
    (if (member x l2)
      (setq res t)
      )
    )
  res
  )

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
list comparison
« Reply #9 on: July 07, 2005, 05:06:27 PM »
Daron, *lol* Yeah I do that too.

But, that code I posted was just a stupid example. Its not very efficient, I was just trying to make a point with that code. The point being that the task can be solved about a million diff ways but if you write down what you want to do; you can code the solution almost the same way as you wrote it down.

For instance: Take what our friend "StarGazer" wrote in the request.

> What I want it to do is grab the first item in the just dump it into a
> list as the first element. Then reading the next line in I want to
> compare it to the list to see if it's the same, if it is then just
> skip it. If not append it to the list. Then go to the next line and
> compare that one to the list. Now that list can be just one or several
> elements that need to be compared.

Now take a look at my code. Im doing almost exactly what he said. (Basicy I translated his words into Autolisp code.)                                                                          

Okay, im done ranting/taching/preaching/etc for da-day.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

LE

  • Guest
Re: list comparison
« Reply #10 on: July 07, 2005, 05:18:28 PM »
This will return the position and the item, and stop evaluating once one item is found.

Code: [Select]

(setq lst1 '(1 11))
(setq lst2 '(5 11 45))

(vl-some '(lambda (i / pos)
   (if (setq pos (vl-position i lst1))
     (list pos i)))
lst2)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
list comparison
« Reply #11 on: July 07, 2005, 05:36:43 PM »
Wasn't Daron looking for a result that indicated the status of each member in the first list, like '(nil t)? Did I misunderstand (again)?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

LE

  • Guest
list comparison
« Reply #12 on: July 07, 2005, 05:46:39 PM »
Quote from: MP
Wasn't Daron looking for a result that indicated the status of each member in the first list, like '(nil t)? Did I misunderstand (again)?


Do not know Michael, to me he wants to find a match an item being on both lists.

Well, I'll wait for his reply.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
list comparison
« Reply #13 on: July 07, 2005, 05:55:23 PM »
Hey that's very cool..

Code: [Select]
_$ (setq lst1 '(1 11 3 6 7))
(setq lst2 '(5 21 18 7 11 45))
(1 11 3 6 7)
(5 21 18 7 11 45)
_$ (vl-some '(lambda (i) (member i lst1)) lst2)
(7)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
list comparison
« Reply #14 on: July 07, 2005, 05:57:04 PM »
Welll ... I went back and read his question and I think I am wrong, maybe he wants an overall result of nil or t ro reflect whether any item in the first list exists in the second.

Something like --

Code: [Select]
(vl-some
   '(lambda (item) (and (member item list2)))
    list1
)

BTW, does LE = Luis Esquivel?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst