Author Topic: Compare 2 list  (Read 4614 times)

0 Members and 1 Guest are viewing this topic.

m4rdy

  • Newt
  • Posts: 62
Compare 2 list
« on: June 04, 2009, 09:30:01 AM »
I have 2 lists.
1. (nil nil T T T nil T)
2. (1 3 7 11 5 6 4)

What i want is :

list (1 3 6)

Thank you.
Autocad 2007, Windows XP

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Compare 2 list
« Reply #1 on: June 04, 2009, 09:48:50 AM »
Perhaps something like this ..
Code: [Select]
(setq L1 '(nil nil T T T nil T))
(setq L2 '(1 3 7 11 5 6 4))
(setq L3 '() )
;; assume the data lists are the same length
(setq  index 0)

(foreach item L1
   (if (not item)
       (setq L3 (cons  (nth index L2) L3))
   )
   (setq index (1+ index))
 )
(setq L3 (reverse L3))
(alert (VL-PRINC-TO-STRING L3))
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

m4rdy

  • Newt
  • Posts: 62
Re: Compare 2 list
« Reply #2 on: June 04, 2009, 09:56:53 AM »
Thank you for your reply Kerry, It works.
One more thing, what about if I want to use with mapcar or vl-remove (something like that).
Thank's again.

m4rdy
Autocad 2007, Windows XP

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Compare 2 list
« Reply #3 on: June 04, 2009, 10:08:59 AM »
Code: [Select]
(defun test (a b)
 (if a
  (if (null(car a))
   (cons (car b) (test (cdr a) (cdr b)))
   (test (cdr a) (cdr b))
  ) ;_  if
 ) ;_  if
)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Compare 2 list
« Reply #4 on: June 04, 2009, 10:19:31 AM »
Great Solution ElpanovEvgeniy  - I doubt that'll be beaten for simplicity  :-)

m4rdy

  • Newt
  • Posts: 62
Re: Compare 2 list
« Reply #5 on: June 04, 2009, 10:32:03 AM »
Thank you Evgeniy. It works also. I think this is enough for now. I will continue for learning Vlisp.
I think this is the best forum (for me) for studying & learning Vlisp.
Autocad 2007, Windows XP

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare 2 list
« Reply #6 on: June 04, 2009, 12:02:14 PM »
Code: [Select]
(setq l1 '(t nil nil t t nil)
      l2 '(1 2 3 4 5 6 7))
(defun test (l1 l2 / l3)
  (mapcar '(lambda(x y)(if x (setq l3 (cons y l3)))) l1 l2)
  (reverse l3)
)
(test l1 l2)

Note: if no matching t/nil assumes nil
« Last Edit: June 04, 2009, 02:14:21 PM by CAB »
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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Compare 2 list
« Reply #7 on: June 04, 2009, 12:28:51 PM »
Code: [Select]
(setq l1 '(t nil nil t t nil)
      l2 '(1 2 3 4 5 6 7)
)

(apply
  'append
  (mapcar
    '(lambda (x1 x2)
       (if (not x1)
(list x2)
       )
     )
    l1
    l2
  )
)
Speaking English as a French Frog

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Compare 2 list
« Reply #8 on: June 04, 2009, 12:55:10 PM »
Unimaginative but trying to avoid replicating previous entries ---

Code: [Select]
(defun _DeleteItems ( items predicates )
    (vl-remove-if 'null
        (mapcar
           '(lambda (item predicate) (if (null predicate) item))
            items
            predicates
        )   
    )
)

(_DeleteItems
   '(1 3 7 11 5 6 4)
   '(nil nil T T T nil T)
)

=> (1 3 6)

Flaw: Will remove perfectly legit nil items if in original list. Small comfort: Not only solution that suffers from said malady.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare 2 list
« Reply #9 on: June 04, 2009, 02:19:33 PM »
Mine will remove any item if the T/nil list is too short. It assumes nil. :-)
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.

uncoolperson

  • Guest
Re: Compare 2 list
« Reply #10 on: June 04, 2009, 03:42:35 PM »
ain't good, fast, or pretty... but they were fun

four was the funnest

Code: [Select]
(defun one ()
  (setq list1 '(nil nil T T T nil T))
  (setq list2 '(1 3 7 11 5 6 4))
  (mapcar '(lambda (item) (cdr item))
 (vl-remove-if
   '(lambda (item
    )
      (car item)
    )
   (mapcar '(lambda (i1 i2) (cons i1 i2)) list1 list2)
 )
  )
)


(defun two ()
  (setq list1 '(nil nil T T T nil T))
  (setq list2 '(1 3 7 11 5 6 4))
  (setq list4 nil)
  (setq list3 (mapcar '(lambda (i1 i2) (cons i1 i2)) list1 list2))
  (while (assoc nil list3)
    (setq list4 (cons (setq item (cdr (assoc nil list3))) list4))
    (setq list3 (subst (cons t item) (assoc nil list3) list3))
  )
  (reverse list4)
)



(defun three ()
  (setq list1 '(nil nil T T T nil T))
  (setq list2 '(1 3 7 11 5 6 4))
  (setq list3 nil)
  (setq len (length list1))
  (while (member nil list1)
    (setq list3
  (cons (nth (- (1- len)
(length (setq list1 (cdr (member nil list1))))
     )
     list2
)
list3
  )
    )
  )
  (reverse list3)
)


(defun four ()
  (setq list1 '(nil nil T T T nil T))
  (setq list2 '(1 3 7 11 5 6 4))
  (setq list3 (mapcar '(lambda (item1 item2) (cons item1 item2)) list1 list2))
  (mapcar 'cdr
 (vl-sort (vl-member-if-not
    '(lambda (item) (car item))
    (vl-sort list3 '(lambda (item1 item2) (car item1)))
  )
  '(lambda (item1 item2)
     (< (vl-position (cdr item1) list2)
(vl-position (cdr item2) list2)
     )
   )
 )
  )
)
« Last Edit: June 04, 2009, 06:13:58 PM by uncoolperson »

Spike Wilbury

  • Guest
Re: Compare 2 list
« Reply #11 on: June 04, 2009, 07:25:08 PM »
just want to participate....

Code: [Select]
(setq lst1 '(nil nil T T T nil T))
(setq lst2 '(1 3 7 11 5 6 4))
(setq lst nil)
(setq i 0)
(reverse (mapcar '(lambda (item)
    (if (not item)
      (setq lst (cons (nth i lst2) lst))
    )
    (setq i (1+ i))
  )
lst1
)
)
lst

_$
(nil nil T T T nil T)
(1 3 7 11 5 6 4)
nil
0
(7 6 5 4 3 2 1)
(6 3 1)  <<<======
_$

an ex lisper....

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare 2 list
« Reply #12 on: June 04, 2009, 09:37:20 PM »
Luis, Don't you miss LISP. :-)
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.

mkweaver

  • Bull Frog
  • Posts: 352
Re: Compare 2 list
« Reply #13 on: June 04, 2009, 11:35:38 PM »
Very similar to MP's

Code: [Select]
(setq l1 '(t nil nil t t nil)
      l2 '(1 2 3 4 5 6 7)
)

(vl-remove-if 'null
  (mapcar
    (function
      (lambda(a b)
(if a b)
)
      )
    l1 l2
    )
  )

Spike Wilbury

  • Guest
Re: Compare 2 list
« Reply #14 on: June 05, 2009, 10:00:03 AM »
Luis, Don't you miss LISP. :-)

some times :)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Compare 2 list
« Reply #15 on: June 05, 2009, 06:04:34 PM »
now that i read all the posts, i see that mine is virtually the same as alan's, but i thought i'd post what i did anyway.

Code: [Select]
(defun process (#ListCompare #ListValues / #NewList)
  (mapcar
    '(lambda (x y)
       (and (not x)
            (setq #NewList (cons y #NewList))
       ) ;_ and
     ) ;_ lambda
    #ListCompare
    #ListValues
  ) ;_ mapcar
  (reverse #NewList)
) ;_ defun
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox