TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on October 22, 2003, 09:54:35 AM

Title: Evaluate a list and return the nil
Post by: JohnK on October 22, 2003, 09:54:35 AM
I was thinking this morning and i have been trying to come up with a slick way to test a list and return the nil member of a list. I cant think of anything. (Am i missing anything?) What is the opposite of "cond"? Is there one?

The only thing i could come up with is: "(mapcar 'cond '(a b c d))" to evaluate each member of the list but all i get is something like "(T T T nil)" as a return. I was wondering if anyone knew of a way to return the item that is nil in a list?

Am i nuts?! ...I must be nuts.
Title: Evaluate a list and return the nil
Post by: Mark on October 22, 2003, 10:33:29 AM
do what..............
I think you've been taking to much cold medicine. :D
Title: Evaluate a list and return the nil
Post by: hendie on October 22, 2003, 10:53:10 AM
funnily enough I was trying to do exactly the same thing two days ago.


I gave up    :roll:
Title: Evaluate a list and return the nil
Post by: JohnK on October 22, 2003, 10:56:14 AM
:? Smartass! Funny, cause I need to go get more. (I'm out)

Ok, lets say I have a list of stuff.
(setq a 1 b 2 c 3 d nil)
and I want to run thru the list to see what member dose not have a value.

I can use a "(mapcar 'cond "mylist")" to evaluate each member of my list but that dosent tell me which one in the list is nil.
Title: Evaluate a list and return the nil
Post by: JohnK on October 22, 2003, 11:09:07 AM
Well it can be done. Here is "a way" but it aint smoooth (like me)

Run each of these lines in your vlide and watch the return values.
Code: [Select]
(setq a 1 b 2 c 3 d nil)
  (setq x (list a b c d))
  (setq y '(a b c d))
  (setq n (- (length x) 1))
  (setq nu
        (1+ (while
              ;; test each
              (equal (nth n x) nil)
              ;; drop the n variable down one increment
              (setq n (1- n))
              ))
        )
 ;; return the nil variable.
  (nth nu y)
Title: Evaluate a list and return the nil
Post by: SomeCallMeDave on October 22, 2003, 11:10:03 AM
If you are sure that you only will have one nil, you could use vl-position

Command: (setq aa (list "A" "b" nil "c"))
("A" "b" nil "c")
Command: (vl-position nil aa)
2
Title: Evaluate a list and return the nil
Post by: JohnK on October 22, 2003, 11:11:38 AM
Quote from: SomeCallMeDave
If you are sure that you only will have one nil, you could use vl-position

Command: (setq aa (list "A" "b" nil "c"))
("A" "b" nil "c")
Command: (vl-position nil aa)
2


Oh crap! im sorry, i forgot to mention that i wanted a way without VL. But nice.

Edit: Im actualy just playing around with the lisp language; looking for cool stuff that i overlooked.  Sorry, i prolly should have mentioned that huh?!
Title: Evaluate a list and return the nil
Post by: Mark on October 22, 2003, 11:25:48 AM
Quote
Ok, lets say I have a list of stuff.
(setq a 1 b 2 c 3 d nil)
and I want to run thru the list to see what member dose not have a value.

OK..........now you're startin to make sense. :D

Check out he eval function.
Code: [Select]
(if (not (eval x))(prompt "is nil.."))
Title: Evaluate a list and return the nil
Post by: CAB on October 23, 2003, 07:32:37 AM
Se7en,
Putting the nil in the middle of the list resulted in error.
I had to move the 1+ for the index var and don't see why eval is needed.
Still just a variation on your routine..


Code: [Select]
 (setq a 1 b 2 c nil d 3)
  (setq x (list a b c d))
  (setq y '(a b c d))
  (setq n (- (length x) 1))
  (setq nu (while
    (nth n x)
    (setq n (1- n))
  )
  )
  ;; return the nil variable.
  (nth (1+ nu) y)
Title: Evaluate a list and return the nil
Post by: CAB on October 23, 2003, 07:49:35 AM
This appears to work as well.

Code: [Select]
 (setq a 1 b nil c 3 d 4)
  (setq x (list a b c d))
  (setq y '(a b c d))
  (setq nu (-(length x)(length (member nil x))))
  (nth nu y);; return the nil variable. If no nil returns nil
Title: Evaluate a list and return the nil
Post by: Mark on October 23, 2003, 08:07:32 AM
Code: [Select]
(setq a 1 b 2 c nil d 3)
 (setq syms '(a b c d))
   
  (foreach item syms
    (if (not (eval item))
      (princ item)
      )
    )
Title: Evaluate a list and return the nil
Post by: Kerry on October 23, 2003, 12:34:17 PM
am I missing something ??

 (setq xxxx (list 1 2 3 nil 5 6))
==>> (1 2 3 nil 5 6)

(member nil xxxx)
==>> (nil 5 6)

(length xxxx) ==>> 6
(length nil) ==>> 0
(length (member nil xxxx)) ==>> 3
Title: Evaluate a list and return the nil
Post by: Mark on October 23, 2003, 12:39:13 PM
Quote from: Kerry B
am I missing something ??

 (setq xxxx (list 1 2 3 nil 5 6))
==>> (1 2 3 nil 5 6)

(member nil xxxx)
==>> (nil 5 6)

member function
Quote
Searches a list for an occurrence of an expression and returns the remainder of the list, starting with the first occurrence of the expression
Title: Evaluate a list and return the nil
Post by: Kerry on October 23, 2003, 12:42:23 PM
yep

.. if the return from (member nil xxxx) is not nil

(- (length xxxx) (length (member nil xxxx))) will be the position of the nil.

it's early here, so I may be incorrect :) need coffee
Title: Evaluate a list and return the nil
Post by: JohnK on October 23, 2003, 12:52:54 PM
Mark, That is what i was attmepting to do. Damn! I cant find my "foreach" test. (I must have destroyed it.) But it was craaaazy.  

Look at this one:
"(mapcar 'if (not (cond (eval x))) (setq x (princ "this one")) '(a b c d))"
Dahell is that?!!!

Kids, do you see what cold med's do to your mind. They make you go nuts!
Title: Evaluate a list and return the nil
Post by: Mark on October 23, 2003, 12:53:18 PM
looks correct to me, but se7en while have to make the final say-so since he started this mess........:D
Title: Evaluate a list and return the nil
Post by: Kerry on October 23, 2003, 12:54:34 PM
if you are after the index, this may suit :
Code: [Select]

(defun test (theList / tmp)
  (if (setq tmp (member nil theList))
    (1+ (- (length theList) (length tmp)))
    nil
  )
)

Title: Evaluate a list and return the nil
Post by: JohnK on October 23, 2003, 01:03:01 PM
Oh i dont care. I was origionaly looking for the opposite of "cond" like it was the fountian of youth or something but anyof those will do.  Thanx alot guys.
Title: Evaluate a list and return the nil
Post by: JohnK on October 23, 2003, 01:07:11 PM
If i had to pick one i would have to say marks because its cleaner.  But their all cool.
Title: Evaluate a list and return the nil
Post by: Kerry on October 23, 2003, 01:15:16 PM
Quote

< snip > and I want to run thru the list to see what member dose not have a value.

I can use a "(mapcar 'cond "mylist")" to evaluate each member of my list but that dosent  tell me which one in the list is nil.


ahh ok ..