TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on February 25, 2013, 10:20:47 AM

Title: using cond instead of if..
Post 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
Code: [Select]
(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...
Code: [Select]
  (SETQ var
(COND
   ((WCMATCH var "A-*") "A")
   ((WCMATCH var "B-*") "B")
   ((WCMATCH var "C-*") "C")

)
  )

Title: Re: using cond instead of if..
Post by: Lee Mac on February 25, 2013, 10:30:52 AM
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:

Code - Auto/Visual Lisp: [Select]
  1. (if <test-expression-1>
  2.     <then-expression-1>
  3.     (if <test-expression-2>
  4.         <then-expression-2>
  5.         (if <test-expression-3>
  6.             <then-expression-3>
  7.             ...
  8.         )
  9.     )
  10. )

The above is then equivalent to:

Code - Auto/Visual Lisp: [Select]
  1.     (   <test-expression-1>
  2.         <then-expression-1>
  3.     )
  4.     (   <test-expression-2>
  5.         <then-expression-2>
  6.     )
  7.     (   <test-expression-3>
  8.         <then-expression-3>
  9.     )
  10.     (   ...   )
  11. )

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:

Code - Auto/Visual Lisp: [Select]
  1. (if (= var "a")
  2.     (progn
  3.         (do my thing)
  4.     )
  5.     (if (= var "b")
  6.         (progn
  7.             (do my thing)
  8.         )
  9.         (if (= var "c")
  10.             (progn
  11.                 (do my thing)
  12.             )
  13.         )
  14.     )
  15. )

Which indicates that cond could be used instead:

Code - Auto/Visual Lisp: [Select]
  1.     (   (= var "a")
  2.         (do my thing)
  3.     )
  4.     (   (= var "b")
  5.         (do my thing)
  6.     )
  7.     (   (= var "c")
  8.         (do my thing)
  9.     )
  10. )
Title: Re: using cond instead of if..
Post by: andrew_nao on February 25, 2013, 10:40:18 AM
excellent explaination Lee, as always.

thanks for the help :)
Title: Re: using cond instead of if..
Post by: CAB on February 25, 2013, 10:50:42 AM
And if you are interested?
http://www.theswamp.org/index.php?topic=13046.msg158557#msg158557
Title: Re: using cond instead of if..
Post by: andrew_nao on February 25, 2013, 11:50:14 AM
excellent read.. thanks CAB
Title: Re: using cond instead of if..
Post by: Lee Mac on February 25, 2013, 12:04:15 PM
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-)
Title: Re: using cond instead of if..
Post by: CAB on February 25, 2013, 12:37:52 PM
Thanks fellas.  8-)