Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Please help with nested COND functions

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
2385 Views, 3 Replies

Please help with nested COND functions

Hi All,

Please help me with the following nested "cond" function in AutoLisp.
I would like to be able to set various variables based on conditions within
conditions.

Please review the following lines and tell me what I am doing wrong.

(defun ITEMWIDTH2()
(cond
((= xitmq 2)
(and (setq itmha itm2ah itmhb itm2bh itmhc itm2ch)
(setq itmde itm1ew)(setq itmwe itm2ew)
(cond((= drbhs2 1)(setq drhs 1))((= drbhs2
2)(setq drhs 2)))
(cond((= drwhs2 1)(setq drhs 1))((= drwhs2
2)(setq drhs 2)))
);and
);cond

((= xitmq 3)
(and (setq itmha itm3ah itmhb itm3bh itmhc itm3ch)
(setq itmde (+ itm1ew itm2ew))(setq itmwe itm3ew)
(cond((= drbhs3 1)(setq drhs 1))((= drbhs3
2)(setq drhs 2)))
(cond((= drwhs3 1)(setq drhs 1))((= drwhs3
2)(setq drhs 2)))
);and
)
);cond

);defun

Thank you in advance,
Al
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

Hi All,

Please review the following and tell me why it is not working:

I am trying to create a conditional function within a conditional function.

(defun ITEMWIDTH2()
(cond
((= xitmq 2)
(and (setq itmha itm2ah itmhb itm2bh itmhc itm2ch)
(setq itmde itm1ew)(setq itmwe itm2ew)
(cond((= drbhs2 1)(setq drhs 1))((= drbhs2
2)(setq drhs 2)))
(cond((= drwhs2 1)(setq drhs 1))((= drwhs2
2)(setq drhs 2)))
);and
)

((= xitmq 3)
(and (setq itmha itm3ah itmhb itm3bh itmhc itm3ch)
(setq itmde (+ itm1ew itm2ew))(setq itmwe itm3ew)
(cond((= drbhs3 1)(setq drhs 1))((= drbhs3
2)(setq drhs 2)))
(cond((= drwhs3 1)(setq drhs 1))((= drwhs3
2)(setq drhs 2)))
);and
)
);cond

);defun

Thank you,
Al
Message 3 of 4
charmingladyfool
in reply to: Anonymous

COND is an unusual function which may take any arbitrary number of arguments. Each argument is called a clause, and consists of a list of exactly two S-expressions. We will call the first S-expression in a clause a condition, and the second S-expression a result. Thus, a call to COND looks like this:

 

(COND

(condition1 result1 )

(condition2 result2 )

. . .

(T    resultN ) )

The value returned by COND is computed as follows: if condition1 is true (not NIL), then return result1; else if condition2 is true then return result2; else if ...; else return resultN. In most LISP systems, it is an error if none of the conditions are true, and the result of the COND is undefined. For this reason, T is usually used as the final condition.

___________________________________________________________________________________________________________________

Copyright © 1995, 2000 by David Matuszek
All rights reserved.
Last updated October 27, 2000

Message 4 of 4

I do wonder whether johnal is still waiting for a reply after more than 12 years, but anyway....

 

I don't think there's anything about their code that shouldn't work, and the first question I would have asked [but that was before I became a user of these Forums] would have been, "What about it doesn't work?  Does it do nothing?  Something, but not what you expect?  Is there any error message?"  Maybe some variable or other doesn't have a value set in it, or is spelled wrong, or something.  It is constructed a little oddly, though, in connection with which I would dispute what charmingladyfool posted from David Matuszek.  There can be more than one result expression evaluated/performed when a given condition returns a non-nil result, that is, the "exactly two" is incorrect:

 

(condition1 result1a result1b result1c ...)

(condition2 result2a result2b result2c ...)

....

 

The mis-belief that there must be only one result for each condition may be a confusion of the way (cond) works with the way (if) works.  In (if), the 'then' expression and the 'else' expression must each be a single item, and if more than one thing needs to be done, they must be wrapped into a single item using something such as (progn), though I guess (and) could be used to accomplish the same.  I suspect that's the reason for the (and) functions in johnal's code, but they are not needed.

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost