Cond

Discussion in 'AutoCAD' started by Adesu, Apr 19, 2004.

  1. Adesu

    Adesu Guest

    Dear Alls,
    My question is easy and simple,but for me difficult to understand,how to
    interpreting "cond",can anybody give me "others sample" to add knowledge
    it,thanks for your helps.
    here is formula cond.
    (cond
    ((= s "Y") 1)
    ((= s "y") 1)
    ((= s "N") 0)
    ((= s "n") 0)
    (t nil)
    )
    Best regards
    Ade Suharna
     
    Adesu, Apr 19, 2004
    #1
  2. The "cond" function evaluates each item supplied until it returns a value
    that is not "nil".
    The function then evaluates the expressions that follow the test.
    If none of the items are true, then none of the expressions are evaluated.

    The 5th item in the test list is NOT necessary, unless you specifically want
    to return nil.
    Also, you can combine the 1st & 2nd item test by (= (strcase S) "Y") 1) or
    (member S (list "Y" "y")) 1)

    Using your program -
    (defun TestCond (S /)
    (cond
    ((= S "Y") 1)
    ((= S "y") 1)
    ((= S "N") 0)
    ((= S "n") 0)
    (t nil)
    )
    )

    (setq VAL "Y")
    (TestCond VAL) ;returns 1 from 1st item test

    (setq VAL "y")
    (TestCond VAL) ;returns 1 from 2nd item test

    (setq VAL "N")
    (TestCond VAL) ;returns 0 from 3rd item test

    (setq VAL "n")
    (TestCond VAL) ;returns 0 from 4th item test

    (setq VAL "anything except Y,y,N,n")
    (TestCond VAL) ;returns nil

    Another Example -
    (defun TestCond (S /)
    (cond
    ((strcase S "Y") (setq NewColor 1))
    ((member S (list "N" "n")) (setq NewColor 2))
    (T (setq NewColor 3))
    )
    )
     
    Alan Henderson, Apr 19, 2004
    #2
  3. Adesu

    Adesu Guest

    Thanks Alan,any others to add comment it ?
     
    Adesu, Apr 19, 2004
    #3
  4. Adesu

    Doug Broad Guest

    Adesu,
    For your example, which appears to be the answer to a yes
    or no question, the if function would be better:

    (if (member s '("Y" "y")) 1 0)


    or

    (if (= "Y" (strcase s)) 1 0)

    The cond statement is better for situations that require
    multiple tests or for testing the results at each step.

    The (t nil) is never necessary at the end of a cond but
    the last statement of a cond can be useful to trigger an
    error condition that will notify the user of an unanticipated
    situtation
    (cond .....
    (t (alert "\nUnanticipated condition in COND")(exit)))

    Of course this would be overkill in most instances but
    could be useful if you expect user i/o to change in the
    future and would like to be alerted in case you have
    forgotten a situation.

    Do a search on the group through google for the word
    cond to see many examples.
     
    Doug Broad, Apr 19, 2004
    #4
  5. In addition to the other responses, this whole test is likely unneeded, if
    you use better techniques (and better variable names!) to get the user's
    input.

    (initget "Yes No")
    (setq inp (cond ((getkword "\nIs there anybody out there? [Yes/No] <No>: "))
    ("No")))


    --
    R. Robert Bell


    Dear Alls,
    My question is easy and simple,but for me difficult to understand,how to
    interpreting "cond",can anybody give me "others sample" to add knowledge
    it,thanks for your helps.
    here is formula cond.
    (cond
    ((= s "Y") 1)
    ((= s "y") 1)
    ((= s "N") 0)
    ((= s "n") 0)
    (t nil)
    )
    Best regards
    Ade Suharna
     
    R. Robert Bell, Apr 19, 2004
    #5
  6. Adesu

    Tom Smith Guest

    Thanks Alan,any others to add comment it ?

    As others have said, this particular example is not a good use of cond, as
    there are simpler ways of doing the same thing.

    The "result" following a test condition is optional. If it's not given, the
    value of the test expression is returned. Example:

    (cond
    ((> x 5)
    (princ "\nGreater than 5"))
    ((= x 5)
    (princ "\nEqual to 5"))
    ((princ "\nMust be less than 5")))

    Since the last princ statement has a non-nil value, it will always be
    triggered if the neither of the others are.
     
    Tom Smith, Apr 19, 2004
    #6
  7. Adesu

    Adesu Guest

    Hi Tom Smith,thanks a lot for you,but this "sample" I think like
    (if ((> x 5)(princ "\nGreater than 5"))
    ((= x 5)(princ "\nEqual to 5")))
    It's true ?
     
    Adesu, Apr 20, 2004
    #7
  8. Adesu

    Adesu Guest

    Hi Doug,thanks a lot for your comment

     
    Adesu, Apr 20, 2004
    #8
  9. Adesu

    Adesu Guest

    Hi R.Robert B,thanks a lot for added comment
     
    Adesu, Apr 20, 2004
    #9
  10. Adesu

    Tom Smith Guest

    Hi Tom Smith,thanks a lot for you,but this "sample" I think like
    I don't understand your question. That is not a legal syntax for an if.

    (cond
    ((> x 5)(princ "\nGreater than 5"))
    ((= x 5)(princ "\nEqual to 5"))
    )

    Is legal, but it is not logically complete. It will generate a response for
    x>5 and x=5, but it has no provision for x<5 and will return nil for that
    condition.

    It is usually important with a cond to make sure you have tested all
    possible conditions.
     
    Tom Smith, Apr 20, 2004
    #10
  11. Adesu

    chris Guest

    I have always thought of (cond) as "CONDitional if", this is each implied if
    "(> x 5) and (= x 5)" is evaluated until one of the conditions is true and
    all other conditions that follow are ignored. In the case below, the last
    line is not an implied if. There is no condition that has to be true, only
    a direct command.

    (cond
    ((> x 5) (princ "\nGreater than 5"))
    ((= x 5) (princ "\nEqual to 5"))
    ((princ "\nMust be less than 5"))
    )



    chris
     
    chris, Apr 21, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.