Help with member

Help with member

DC-MWA
Collaborator Collaborator
917 Views
7 Replies
Message 1 of 8

Help with member

DC-MWA
Collaborator
Collaborator

I have this part of my code which works well.

(if (and (= TOC "5A")
(member OCC '("R-4" "R-2.1"))
)
(progn
(setq HTNS "50")
(setq HTS13D "50")
(setq HTS13R "50")
(setq HTS "50")
);end pr
);end if

 

I need to do something like this in another part of code...

 

(if (member OCC '("R-4" "R-2.1")
)
(progn
(alert "test successful!")
);end pr
);end if

Please help. Am I forced to (if (and ??

 

0 Likes
918 Views
7 Replies
Replies (7)
Message 2 of 8

dbhunia
Advisor
Advisor

Did not get it ....... both are working for me.......

 

If TOC =5A & OCC =R-4/R-2.1


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 3 of 8

ВeekeeCZ
Consultant
Consultant

The code is fine, no end needed. The MEMBER function works fine.

 

Though for such a situation would pro probably little faster to use the VL-POSITION function.

Also setq could be combined...

 

(if (and (= TOC "5A")
         (vl-position OCC '("R-4" "R-2.1"))
         )
  
  (setq HTNS "50"
        HTS13D "50"
        HTS13R "50"
        HTS "50"))


(if (vl-position OCC '("R-4" "R-2.1"))
  (alert "test successful!"))

 

But you can omit if entirely, and use just and. The result is the same. Some users prefers to build entire program that way.

 

(and (= TOC "5A")
     (vl-position OCC '("R-4" "R-2.1"))
     (setq HTNS "50"
           HTS13D "50"
           HTS13R "50"
           HTS "50"))

Or your second example may include the Error warning. The overall result would be still the same: T if OCC found, nil if not.

(or (vl-position OCC '("R-4" "R-2.1"))
    (alert (strcat "Error: Wrong input. " OCC " isn't allowed.")))

... all are just a minor tips. But all you can see quite often used in codes.

Message 4 of 8

martti.halminen
Collaborator
Collaborator

If you are doing tens of thousands of MEMBER calls, using VL-POSITION would be faster. If you are calling it just a few times, the difference is in milliseconds, so using MEMBER communicates the programmer intent more clearly to the next reader.

- This is essentially an Autodesk implementation failure: VL-POSITION does more work than MEMBER, so it shouldn't be able to be faster.

 

Regarding OR: in general Lisp programming you can often find usage like

(setq var (or (find-current-value...)

                       (find-default-value ...)

                       (ask-user ...)))

This won't work in AutoLISP.

 

Common Lisp: 

> (or nil 5 'A)
5

 

AutoLISP:

_$ (or nil 5 'A)
T

 

So, in AutoLISP you can only use OR if you are either not interested in the value returned, or T/NIL as the return value is sufficient.

- similar difference also in AND.

 

-- 

 

 

 

Message 5 of 8

doaiena
Collaborator
Collaborator

You can go around that limitation with something like this:

 

(setq var (vl-some 'eval (list nil 5 'A)))

0 Likes
Message 6 of 8

martti.halminen
Collaborator
Collaborator

@doaiena wrote:

You can go around that limitation with something like this:

 

(setq var (vl-some 'eval (list nil 5 'A)))


That is one way to do it. 

Another possibility: using COND without result value, so it returns the test value:

(setq var (cond ((find-current-value...))
                ((find-default-value ...))
                ((ask-user ...))))

- needs one layer of parentheses more than the other versions.

0 Likes
Message 7 of 8

DC-MWA
Collaborator
Collaborator

Thank you all for the input. I plan on testing the several different ways of handling this.

My schedule has forced me to put this task off to a later date.

I will let you all know what worked best for me.

THANK YOU SO MUCH FOR ASSISTING ME!!!!

0 Likes
Message 8 of 8

john.uhden
Mentor
Mentor

@ВeekeeCZ wrote:

" Or your second example may include the Error warning. The overall result would be still the same: T if OCC found, nil if not.

(or (vl-position OCC '("R-4" "R-2.1"))
    (alert (strcat "Error: Wrong input. " OCC " isn't allowed.")))

Be careful there in case OCC is nil.

John F. Uhden

0 Likes