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

if or cond statements

12 REPLIES 12
Reply
Message 1 of 13
Anonymous
864 Views, 12 Replies

if or cond statements

im needing to put an if statement, or cond (not sure which one or both) in and have no idea how to
make it work.
i tried this

(if ((< px1 qx1)(< py1 qy1)) sym1)) ;draw symbol up and left
(if ((> px1 qx1)(< py1 qy1)) sym2)) ; draw symbol up and right



(setq sym1 (command "_.pline" ti vert horz "")) ;draws slope symbol
(setq sym2 (command "_.pline" ti vert2 horz2 ""))

could someone show me how to actually set this up right. am I way of base?

dont laugh to hard and when I get this routine up and runnin I will share it

--
Dave C. Johnson
12 REPLIES 12
Message 2 of 13
Anonymous
in reply to: Anonymous


djohnson wrote:

im needing to put an if statement, or cond (not sure which one or both) in and have no idea how to 
make it work.
i tried this

(if ((< px1 qx1)(< py1 qy1)) sym1)) ;draw symbol up and left
(if ((> px1 qx1)(< py1 qy1)) sym2)) ; draw symbol up and right



(setq sym1 (command "_.pline" ti vert horz "")) ;draws slope symbol
(setq sym2 (command "_.pline" ti vert2 horz2 ""))

could someone show me how to actually set this up right. am I way of base?

dont laugh to hard and when I get this routine up and runnin I will share it





No laughing. We all started somewhere.



Since the (<py1 qy1) was the same in each IF statement, you don't
need to include it. If it does need to stay for some reason, use

(if (and (< px1
qx1) (< py1 qy1) )
...





(if

  (< px1 qx1)

  (command "_.pline" ti vert horz "")   ;draw symbol up and left

  (command "_.pline" ti vert2 horz2 "") ;draw symbol up and right

  )

 

========

or

========



(if

  (< px1 qx1)

  (sym1) ;draw symbol up and left

  (sym2) ;draw symbol up and right

  )

 

 

(defun sym1 (command "_.pline" ti vert horz ""))   ;;note DEFUN, not
SETQ

(defun sym2 (command "_.pline" ti vert2 horz2 ""))


Message 3 of 13
Anonymous
in reply to: Anonymous


As a general rule, if there is a binary yes/no choice, like "Line or
Polyline?" use IF.

If the result of one decision determines the next decision, use nested
IF's.

If there are multiple choices, like "Line, Polyline, Circle, or
Ellipse?"), use COND.



Your routine did not say what to do if px1 = qx1.

My IF solution treats that the same as (> px1 qx1)

COND could have been used to deal with this undefined condition on its
own, or the same as either of the other two.



(cond

  (
(< px1 qx1) (sym1))

  ( (> px1 qx1) (sym2))

  ( T           (sym3))  ;;this catch all is executed if no condition

                         ;;that precedes it is true.

  )

  


Message 4 of 13
Anonymous
in reply to: Anonymous


Oops.



(defun sym1 (command "_.pline" ti vert horz ""))   ;;note DEFUN, not
SETQ

(defun sym2 (command "_.pline" ti vert2 horz2 ""))



should be



(defun
sym1 () (command "_.pline" ti vert horz ""))   ;;note DEFUN, not
SETQ

(defun sym2 () (command "_.pline" ti vert2 horz2 ""))


Message 5 of 13
Anonymous
in reply to: Anonymous

thanks Bill. this will get me moving again. the if/cond are still confusing to me but I will come
around.



--
Dave C. Johnson
Message 6 of 13
Anonymous
in reply to: Anonymous

Hi,

The exemple I use when trying to explain the COND statement is about colors.
If in real life everything was black and white, one could use an IF
(if (= color "Black")
(do_black);;; true branch
(do_white);;; false branch, i.e. color = white
)

No, in real life we have more than black and white, so for more than one
possible truth, we could nest more IFs, but some smart people thought about
some other function, which in LISP is called COND.

(cond
( (= color "red");;; if this is true
(do_red)
)
( (= color "yellow");;; if this is true
(do_yellow)
)
( (= color "green");;; if this is true
(do_green)
)
( (= color "blue");;; if this is true
(do_blue)
)
(T (do_whatever_else_color);;; if eveything else is true
)

The cond function accepts any number of lists as arguments. It evaluates the
first item in each list (in the order supplied) until one of these items
returns a value other than nil. It then evaluates those expressions that
follow the test that succeeded. It is common to use T as the last (default)
test expression.

Return Values

The value of the last expression in the sublist. If there is only one
expression in the sublist (that is, if result is missing), the value of the
test expression is returned. If no arguments are supplied, cond returns nil.

HTH


"djohnson" a écrit dans le message de news:
6107244@discussion.autodesk.com...
thanks Bill. this will get me moving again. the if/cond are still confusing
to me but I will come
around.



--
Dave C. Johnson
Message 7 of 13
Anonymous
in reply to: Anonymous

To expand a little on what Some Buddy replied, and make more explicit something he didn't quite say
outright about one of (cond)'s benefits:

When you have more than one possibility to check, a big advantage of (cond) over (if) is that (cond)
*doesn't even bother to look at* any conditions after the first one that is satisfied. If you check
for multiple conditions with multiple (if) functions, like this:

{code}
(if (= color "red") (...do the red thing...))
(if (= color "yellow") (...do the yellow thing...))
(if (= color "green") (...do the greeb thing...))
(if (= color "blue") (...do the blue thing...))
{code}

it will work, but it will test for *all* of those possibilities, regardless of whether it finds any
of them satisfied. If the color is "red," it will do the red thing, but then it will also, and
unnecessarily, go on to check whether the color is any of those other possibilities. That will be a
complete waste, because none of them are going to be satisfied if the color is "red."

But if you use (cond), like this:

{code}
(cond
((= color "red") (...do the red thing...))
((= color "yellow") (...do the yellow thing...))
((= color "green") (...do the green thing...))
((= color "blue") (...do the blue thing...))
(T (...do the anything-else thing)); [with or without this]
); end cond
{code}

then it will only check *until it finds a true condition*, and ignore the rest. If the color is
"red," it will do the red thing, and then *go to the end* of the (cond) function, and continue with
whatever is next, without bothering itself to check for the other possibilities which won't be
satisfied.

Also, if you want an anything-else thing to be done if the color isn't any of those four, then it's
a lot more complicated with (if) functions. You would probably need an additional (if) function,
along with the ones above, something like:

{code}
(if
(not
(or
(= color "red")
(= color "yellow")
(= color "green")
(= color "blue")
); end or
); end not
(...do the anything-else thing)
)
{code}

or, a somewhat shorter way:

{code}
(if
(not
(member color '("red" "yellow" "green" "blue"))
)
(...do the anything-else thing)
)
{code}

Shorter, but still a lot more code than necessary if you use the (cond) approach instead.

--
Kent Cooper


"djohnson" wrote...
.... the if/cond are still confusing to me but I will come
around.
....
Message 8 of 13
Anonymous
in reply to: Anonymous


From my previous message:

 

"It evaluates the first item in each list (in the
order supplied) until one of these items
returns a value other than nil."

 

And anyway, I think that one who wants to
learn about LISP should read a little more by him self, than expecting
people here to write pages of explanations. What I think is that putting people
on the rails is enough, there is no need to push their train. But then again,
this is just me.

 

 

To expand a little on what Some
Buddy replied, and make more explicit something he didn't quite say
outright
about one of (cond)'s benefits:

When you have more than one possibility
to check, a big advantage of (cond) over (if) is that (cond)
*doesn't even
bother to look at* any conditions after the first one that is satisfied. 
If you check
for multiple conditions with multiple (if) functions, like
this:

{code}
(if (= color "red") (...do the red thing...))
(if (=
color "yellow") (...do the yellow thing...))
(if (= color "green") (...do the
greeb thing...))
(if (= color "blue") (...do the blue
thing...))
{code}

it will work, but it will test for *all* of those
possibilities, regardless of whether it finds any
of them satisfied. 
If the color is "red," it will do the red thing, but then it will also, and

unnecessarily, go on to check whether the color is any of those other
possibilities.  That will be a
complete waste, because none of them are
going to be satisfied if the color is "red."

But if you use (cond), like
this:

{code}
(cond
  ((= color "red") (...do the red
thing...))
  ((= color "yellow") (...do the yellow thing...))
 
((= color "green") (...do the green thing...))
  ((= color "blue")
(...do the blue thing...))
  (T (...do the anything-else thing)); [with
or without this]
); end cond
{code}

then it will only check *until
it finds a true condition*, and ignore the rest.  If the color is

"red," it will do the red thing, and then *go to the end* of the (cond)
function, and continue with
whatever is next, without bothering itself to
check for the other possibilities which won't be
satisfied.

Also, if
you want an anything-else thing to be done if the color isn't any of those four,
then it's
a lot more complicated with (if) functions.  You would
probably need an additional (if) function,
along with the ones above,
something like:

{code}
(if
  (not
   
(or
      (= color
"red")
      (= color
"yellow")
      (= color
"green")
      (= color
"blue")
    ); end or
  ); end not
  (...do
the anything-else thing)
)
{code}

or, a somewhat shorter
way:

{code}
(if
  (not
    (member color
'("red" "yellow" "green" "blue"))
  )
  (...do the anything-else
thing)
)
{code}

Shorter, but still a lot more code than necessary
if you use the (cond) approach instead.

--
Kent
Cooper


"djohnson" wrote...
.... the if/cond are still confusing to
me but I will come
around.
....
Message 9 of 13
Anonymous
in reply to: Anonymous

thanks guys that does help me and I really do appreciate it. I will keep hammering on this code and
post it when I get it done, which will be a while because I keeping running into stuff I had not
thought of. 2 steps forward 5 steps back 🙂



--
Dave C. Johnson
Message 10 of 13
Anonymous
in reply to: Anonymous

You're welcome Dave 🙂

"djohnson" a écrit dans le message de news:
6107406@discussion.autodesk.com...
thanks guys that does help me and I really do appreciate it. I will keep
hammering on this code and
post it when I get it done, which will be a while because I keeping running
into stuff I had not
thought of. 2 steps forward 5 steps back 🙂



--
Dave C. Johnson
Message 11 of 13
Anonymous
in reply to: Anonymous

djohnson wrote:
> im needing to put an if statement, or cond (not sure which one or both) in and have no idea how to
> make it work.
> i tried this
>
> (if ((< px1 qx1)(< py1 qy1)) sym1))


Others have already talked about IF and COND, I'd like to comment on two
beginner's mistakes your initial code is showing:

1. The parentheses must balance. Unless you are closing some larger
piece of program not shown here, you have one closing parenthesis too
much on each line.

2. Unlike many other programming languages, you may not add arbitrary
parentheses anywhere you like to combine clauses. You need to use some
special operators to connect stuff, for example AND, OR etc. PROGN is
also often useful, though meaningless in a test.

(if (and (< px qx1)(< py1 qy1)) sym1)

--
Message 12 of 13
Anonymous
in reply to: Anonymous

thanks Martti

I do seem to have trouble counting the parens, but I do know they need to equal.
Im still struggling with the special operators but can see how vital they are to getting things
accomplished in the routine.


--
Dave C. Johnson
Message 13 of 13
Anonymous
in reply to: Anonymous

djohnson wrote:
> thanks Martti
>
> I do seem to have trouble counting the parens, but I do know they need to equal.
> Im still struggling with the special operators but can see how vital they are to getting things
> accomplished in the routine.

The trick with counting parens is getting the computer to do that for
you: find an editor that can show the matching parens and can indent the
code according to syntax. Professionals read Lisp code based on the
indentation, not the parens.

The best editor for Lisp work I know is GNU Emacs, but even VLIDE is
better than nothing.

--

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

Post to forums  

Autodesk Design & Make Report

”Boost