Can anyone see where I'm going wrong?
Hi, Lets clear some things up -
The
if function in LISP wraps up both, the
then and
else evaluation blocks:
(evaluate_if_true)
(evaluate_if_false) ; this is optional
); if
In languages like C# it would look like this:
if (condition)
{
// evaluate_if_true
}
else // this is optional block
{
// evaluate_if_false
}
But in there you can also include multiple
else if blocks
(which is different in terms of evaluation, than copying multiple single if-else statements) :
if (condition1)
{
// evaluate_for_condition1
}
else if (condition2) // this is optional block, aswell the below ones
{
// evaluate_for_condition2
}
else if (condition3)
{
// evaluate_for_condition3
}
else
{
// evaluate_if_no_condtion_is_matched
}
The above C# syntax example will check for the first condition that matches and won't bother checking the rest.
Problem is when you copy single if-else statements multiple times (just like you did),
the evaluation inside the function
doesn't stop at the first matching condition, but rather
continues checking for the rest conditions and seek to evaluate whatever if matches true.
Unfortunately in LISP there are no multiple 'else if' built-in function, but theres
cond -
which works similarly to the 2nd C# syntax I've mentioned - the one with multiple
else if blocks.
( (condition1) ; this is an 'if' block
; evaluate_for_condition1
)
( (condition2) ; this is an 'else if' block [optional]
; evaluate_for_condition2
)
( (condition3) ; this is an 'else if' block [optional]
; evaluate_for_condition3
)
(t ; this is an 'else' block [optional]
; evaluate_if_no_condtion_is_matched
)
); cond
I'm sure that after reading the above you still won't understand what I said (nothing personal), so I'll provide an comparsion example
for you to observe whats the difference between evaluating multiple copied if statements and multiple else-if statements (
cond)
used for overlapping conditions -
(defun Multiple_If_Statements
( / x
)
(alert "if_Block1: Yes, x is a number.") ; (alert "if_Block1: No, x is not a number.")
)
(alert "if_Block2: Yes, x is a number, and is below or equal to five.") ; (alert "if_Block2: No, x is not a number or its not below or equal to five.")
)
(alert "if_Block3: Yes, x is a number, and is above or equal to zero.") ; (alert "if_Block3: No, x is not a number or its not above or equal to zero")
)
)
(Multiple_If_Statements)
(defun Multiple_Else_If_Statements
( / x
)
(alert "if_Block1: Yes, x is a number.") )
(alert "else_if_Block2: Yes, x is a number, and is below or equal to five.") )
(alert "else_if_Block3: Yes, x is a number, and is above or equal to zero.") )
); cond
); defun
(Multiple_Else_If_Statements)
When you run the first function (Multiple_If_Statements) you should get the 3 alert messages,
and when you run the second function (Multiple_Else_If_Statements) you should get only 1 alert message.
Maybe you still won't get whats the big deal about this, but imagine that
every condition in both examples were substituted with yours:
(= NEWTILE "rb48")
and also:
(and
(= NEWTILE "rb48")
(= sel "pipe_")
) ;end and
and each alert message was substituted with yours:
(DRAW_PIPE_SHAPE)
(SET_BLOCK)
meaning that if you prompt for a point inside the call-function, your checking-function would x3 times that prompt.
So that makes two completely different programs.
Understanding how conditionals work is essential for any programming language,
so I suggest for you to play/experiment with small parts of your program, some obvious conditionals, along with alert or print functions
In order to understand how it works and whats the logic behind it.
ASIDE TALK:
BTW theres a related, common programming question about on how to stop the evaluation after the first matching occurence of an iterative datastructure in a 'x' language.
So far in my exploration I've collected these:
LISP:
(vl-some '(lambda) (list))
C#:
<Array class>.FirstOrDefault()
<List class>.Any()
JavaScript:
<array>.some()
Not to mention the commonly known -
for(i = 0; i < arr.length ; i++)
{
if (condition_for arr[i])
{
// do stuff...
break;
}
}