TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on February 25, 2013, 10:20:47 AM
-
how would i set up the cond function to use like the example instead of multiple if's?
example
(if (= var "a")
(progn
(do my thing)
)
)
(if (= var "b")
(progn
(do my thing)
)
)
(if (= var "c")
(progn
(do my thing)
)
)
or is the cond function only used when setting variable?
like so...
(SETQ var
(COND
((WCMATCH var "A-*") "A")
((WCMATCH var "B-*") "B")
((WCMATCH var "C-*") "C")
)
)
-
In general, it would depend upon the behaviour you are looking to achieve, since cond ceases evaluation of subsequent test expressions upon a test expression for a condition returning a non-nil value.
Hence in general, the question of whether cond may be used is equivalent to asking whether your if statements could be written:
<then-expression-1>
<then-expression-2>
<then-expression-3>
...
)
)
)
The above is then equivalent to:
( <test-expression-1>
<then-expression-1>
)
( <test-expression-2>
<then-expression-2>
)
( <test-expression-3>
<then-expression-3>
)
( ... )
)
However, this question is obvious for your scenario, since the variable 'var' can only hold one value, so this value must be either "a", "b" or "c". Therefore, the set of consecutive if statements could be written as a nested structure:
(do my thing)
)
(do my thing)
)
(do my thing)
)
)
)
)
Which indicates that cond could be used instead:
( (= var "a")
(do my thing)
)
( (= var "b")
(do my thing)
)
( (= var "c")
(do my thing)
)
)
-
excellent explaination Lee, as always.
thanks for the help :)
-
And if you are interested?
http://www.theswamp.org/index.php?topic=13046.msg158557#msg158557
-
excellent read.. thanks CAB
-
excellent explaination Lee, as always.
thanks for the help :)
You're welcome Andrew.
@CAB, a thorough explanation - I've referred members to it many a time. 8-)
-
Thanks fellas. 8-)