Author Topic: just a question about lists  (Read 1683 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
just a question about lists
« on: April 23, 2014, 10:30:35 AM »
im not even sure how to ask this so ill just post my situation and hope its comprehensive enough to get some pro feedback.

i have 2 lists i have to compare against each other and if any item in list 1 matches any item in list 2, i have to make an alert.
so far, thats not the issue.

the issue is, list 2 is a static list with almost 350 items, while list 1 is created on the fly and can vary from 1 item to 60 or more items.

when comparing, what would be best for list 2?
should i put list 2 in an external text file, read and try to match from it
or making list 2 a variable in the code itself?

i hope this makes sense in what im after
and would appreciate a bit of direction


ymg

  • Guest
Re: just a question about lists
« Reply #1 on: April 23, 2014, 10:40:48 AM »
Would put it in the code, unless you are hard pressed for memory.

This way you you don't have the overhead of opening the file.

Here is a fuction by gile to get the common item to both list.

Code: [Select]
;; intersect
;; Retourne la liste des éléments communs à l1 et l2
;;
;; Arguments
;; l1 : une liste
;; l2 : une liste
(defun intersect (l1 l2)
  (if l1
    (if (member (car l1) l2)
      (cons (car l1) (intersect (cdr l1) l2))
      (intersect (cdr l1) l2)
    )
  )
)

ymg

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: just a question about lists
« Reply #2 on: April 23, 2014, 11:19:34 AM »
Sort of depends on how often the list 2 is updated, if it is something updated on a regular basis I'd use an external txt file.  If it is never updated then ymg's reply is good.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: just a question about lists
« Reply #3 on: April 23, 2014, 11:26:06 AM »
I think (vl-position is faster than member.
Also if any one hit is enough to cause the alert then no need to continue the comparison.

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.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: just a question about lists
« Reply #4 on: April 23, 2014, 01:38:02 PM »
I'm a big fan of separating code and data.  When data isn't in the code you can have a non-coder do the data entry while you work on something they can't.  You can keep the code in a locked down/read-only location and multiple data files in multiple folders which may or may not be read-only, and may have different contents varying by client or project.  Makes debugging code easier, as you don't have to jump through pages of data trying to find that particular spot in the code.  And on, and on, and on...
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: just a question about lists
« Reply #5 on: April 23, 2014, 06:02:45 PM »
I'm a big fan of separating code and data.  When data isn't in the code you can have a non-coder do the data entry while you work on something they can't.  You can keep the code in a locked down/read-only location and multiple data files in multiple folders which may or may not be read-only, and may have different contents varying by client or project.  Makes debugging code easier, as you don't have to jump through pages of data trying to find that particular spot in the code.  And on, and on, and on...

1+

As for the comparison, this should suffice:

Code - Auto/Visual Lisp: [Select]
  1. (defun common-p ( l1 l2 )
  2.     (and (vl-some '(lambda ( x ) (vl-position x l2)) l1))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (common-p '(1 2 3 4 5) '(9 8 7 6 5))
  2. T
  3. _$ (common-p '(1 2 3 4 5) '(9 8 7 6 0))
  4. nil

Or recursively (and slowly):
Code - Auto/Visual Lisp: [Select]
  1. (defun common-p ( l1 l2 )
  2.     (and l1 (or (member (car l1) l2) (common-p (cdr l1) l2)))
  3. )
« Last Edit: April 23, 2014, 06:37:49 PM by Lee Mac »

andrew_nao

  • Guest
Re: just a question about lists
« Reply #6 on: April 24, 2014, 02:14:35 PM »
appreciate everyones feedback