Author Topic: Evaluate a list and return the nil  (Read 13938 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10569
Evaluate a list and return the nil
« 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Evaluate a list and return the nil
« Reply #1 on: October 22, 2003, 10:33:29 AM »
do what..............
I think you've been taking to much cold medicine. :D
TheSwamp.org  (serving the CAD community since 2003)

hendie

  • Guest
Evaluate a list and return the nil
« Reply #2 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:

JohnK

  • Administrator
  • Seagull
  • Posts: 10569
Evaluate a list and return the nil
« Reply #3 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10569
Evaluate a list and return the nil
« Reply #4 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)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SomeCallMeDave

  • Guest
Evaluate a list and return the nil
« Reply #5 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

JohnK

  • Administrator
  • Seagull
  • Posts: 10569
Evaluate a list and return the nil
« Reply #6 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?!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Evaluate a list and return the nil
« Reply #7 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.."))
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Evaluate a list and return the nil
« Reply #8 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)
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Evaluate a list and return the nil
« Reply #9 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
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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Evaluate a list and return the nil
« Reply #10 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)
      )
    )
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Evaluate a list and return the nil
« Reply #11 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
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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Evaluate a list and return the nil
« Reply #12 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
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Evaluate a list and return the nil
« Reply #13 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
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10569
Evaluate a list and return the nil
« Reply #14 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!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org