TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: m4rdy 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.
-
Perhaps something like this ..
(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))
-
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
-
(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
)
-
Great Solution ElpanovEvgeniy - I doubt that'll be beaten for simplicity :-)
-
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.
-
(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
-
(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
)
)
-
Unimaginative but trying to avoid replicating previous entries ---
(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.
-
Mine will remove any item if the T/nil list is too short. It assumes nil. :-)
-
ain't good, fast, or pretty... but they were fun
four was the funnest
(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)
)
)
)
)
)
-
just want to participate....
(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....
-
Luis, Don't you miss LISP. :-)
-
Very similar to MP's
(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
)
)
-
Luis, Don't you miss LISP. :-)
some times :)
-
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.
(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