Author Topic: using cond instead of if..  (Read 2388 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
using cond instead of if..
« 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")

)
  )


Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: using cond instead of if..
« Reply #1 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. )

andrew_nao

  • Guest
Re: using cond instead of if..
« Reply #2 on: February 25, 2013, 10:40:18 AM »
excellent explaination Lee, as always.

thanks for the help :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: using cond instead of if..
« Reply #3 on: February 25, 2013, 10:50:42 AM »
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.

andrew_nao

  • Guest
Re: using cond instead of if..
« Reply #4 on: February 25, 2013, 11:50:14 AM »
excellent read.. thanks CAB

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: using cond instead of if..
« Reply #5 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-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: using cond instead of if..
« Reply #6 on: February 25, 2013, 12:37:52 PM »
Thanks fellas.  8-)
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.