Satish Lele
satish.lele@gmail.com


Conditional Functions
These allows program to make decisions, by testing whether a condition is true or false.

if: Conditionally evaluates expressions.
(if testexpr thenexpr [elseexpr])
If testexpr is not nil, it evaluates thenexpr. Otherwise it evaluates elseexpr. if function returns the value of the selected expression. If elseexpr is missing and testexpr is nil, then the if function returns nil.
(if (= 1 3) "YES!!" "no.") returns "no."
(if (= 2 (+ 1 1)) "YES!!") returns "YES!!"
(if (= 2 (+ 3 4)) "YES!!") returns nil.

progn: (progn [expr] [expr] [expr] ...)
You can use progn to evaluate several expressions where only one expression is expected as in case of condition of if.
(if (progn (= a b) (= c d)) (princ "\nA = B and C = D") (setq a (+ a 10) b (- b 10))

cond: Serves as the primary conditional function for AutoLISP.
(cond
(test1 expression1 expression2 expression3 ...)
(test2 expression1 expression2 expression3 ...)
(test3 expression1 expression2 expression3 ...)
...)
It is actually the primary conditional function. If function is offered as an alternative to cond, since it is similar to conditional functions in other programming languages. (cond is similar to case used in c language).
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, and returns 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.
The following example uses cond to perform an absolute value calculation:
(cond
((minusp a) (- a))
(t a)
)
If the variable a is set to the value -10, this returns 10.
As shown, cond can be used as a case type function. It is common to use T as the last (default) test expression. Here's another simple example. Given a user response string in the variable s, this function tests the response and returns 1 if it is Y or y, 0 if it is N or n, and nil otherwise. It can be used anywhere you can use if.

< (less than): Returns T if each argument is numerically less than the argument to its right, and returns nil otherwise
(< numstr [numstr] ...)
Each numstr argument can be a number or a string.
(< 10 20) ;returns T
(< "b" "c") ;returns T
(< 357 33.2) ;returns nil
(< 2 3 88) ;returns T
(< 2 3 4 4) ;returns nil

<= (less than or equal to): Returns T if each argument is numerically less than or equal to the argument to its right, and returns nil otherwise
(<= numstr [numstr] ...)
Each numstr argument can be a number or a string.
(<= 10 20) ;returns T
(<= "b" "b") ;returns T
(<= "b" "a") ;returns nil
(<= 357 33.2) ;returns nil
(<= 2 9 9) ;returns T
(<= 2 9 4 5) ;returns nil

> (greater than): Returns T if each argument is numerically greater than the argument to its right, and returns nil otherwise
(> numstr [numstr] ...)
Each numstr argument can be a number or a string.
(> 120 17) ;returns T
(> "c" "b") ;returns T
(> 3.5 1792) ;returns nil
(> 77 4 2) ;returns T
(> 77 4 4) ;returns nil

>= (greater than or equal to): Returns T if each argument is numerically greater than or equal to the argument to its right, and returns nil otherwise
(>= numstr [numstr] ...)
Each numstr argument can be a number or a string.
(>= 120 17) ;returns T
(>= "c" "c") ;returns T
(>= 3.5 1792) ;returns nil
(>= 77 4 4) ;returns T
(>= 77 4 9) ;returns nil

= (equal to): Returns T if all arguments are numerically equal, and returns nil otherwise
(= numstr [numstr] ...)
Each numstr argument can be a number or a string.
(= 4 4.0) ;;returns T
(= 20 388) ;;returns nil
(= 2.4 2.4 2.4) ;;returns T
(= 499 499 500) ;returns nil
(= "me" "me") ;returns T
(= "me" "you") ;returns nil

/= (not equal to): Returns T if the arguments are not numerically equal, and nil if the arguments are numerically equal
(/= numstr [numstr] ...)
Each numstr argument can be a number or a string.
(/= 10 20) ;returns T
(/= "you" "you") returns nil
(/= 5.43 5.44) ;returns T
(/= 10 20) ;returns T

eq: Determines whether two expressions are identical. (eq expr1 expr2)
The eq function determines whether expr1 and expr2 are bound to the same object (by setq, for example). Returns T if the two expressions are identical, and returns nil otherwise. You can use this function to determine whether two lists are the same. For example, given the assignments
(setq f1 '(a b c))
(setq f2 '(a b c))
(setq f3 f2)
then
(eq f1 f3) returns nil, f1 and f3 are not the same list
(eq f3 f2) returns T, f3 and f2 are exactly the same list

equal: Determines whether two expressions are equal
(equal expr1 expr2 [fuzz])
The equal function determines whether expr1 and expr2 evaluate to the same thing. When comparing two real numbers (or two lists of real numbers, as in points), the two identical numbers can differ slightly if different methods are used to calculate them. Therefore, you can use an optional numeric argument, fuzz, to specify the maximum amount by which expr1 and expr2 can differ and still be considered equal.
For example, given the assignments
(setq f1 '(a b c))
(setq f2 '(a b c))
(setq f3 f2)
(setq a 1.123456)
(setq b 1.123457)
then
(equal f1 f3) returns T
(equal f3 f2) returns T
(equal a b) returns nil
(equal a b 0.000001) returns T
Although two lists that the equal function finds the same, might not be found so using the eq function, atoms that are found to be the same using the equal function are always found to be the same, if you use the eq function. However, if the eq function finds that the list or atoms are the same, the equal function also finds them to be the same.

Boundp: Verifies if a value is bound to a symbol
(boundp sym)
Returns T if sym has a value bound to it. If no value is bound to sym (or if it has been bound to nil), boundp returns nil. If sym is an undefined symbol, it is automatically created and is bound to nil. For example, given the assignments
(setq a 2 b nil)
then
(boundp 'a) returns T
(boundp 'b) returns nil
The atoms-family function provides an alternate method of determining the existence of a symbol without automatically creating the symbol.

listp: Verifies if an object is a list.
minusp: Verifies if a numeric value is negative.
numberp: Verifies if an object is a number, real or integer.
zerop: an object evaluates to zero.

Logical Operators:
and: Returns the logical AND of a list of expressions (and expr ...)
If any of the expressions evaluate to nil, this function ceases further evaluation and returns nil; otherwise it returns T.
(setq a 103 b nil c "string")
then
(and 1.4 a c) ;returns T
(and 1.4 a b c) ;returns nil

not: Verifies that an item evaluates to nil. (not item)
Returns T if item evaluates to nil, and returns nil otherwise.
(setq a 123 b "string" c nil)
(not a) returns nil
(not b) returns nil
(not c) returns T
(not '()) returns T
Typically, the null function is used for lists, and not is used for other data types along with some type of control function.

null: Verifies that an item is bound to nil. (null item)
Returns T if item is bound to nil, and returns nil otherwise.

or: Returns the logical OR of a list of expressions (or expr...)
The or function evaluates the expressions from left to right, looking for a non-nil expression. If one is found, or ceases further evaluation and returns T. If all of the expressions are nil, or returns nil.
(or nil 45 '()) returns T
(or nil '()) returns nil

Repeating Part of Program:
While / Repeat: It requires a pre-condition to determine whether to evaluate its arguments. Repeat uses an integer value to determine the number of times to perform the evaluation.
( while condition
exp1 exp2 exp3
)
condition checks if counter is less than or greater than highest value
Last exp increases or reduces counter value
(repeat n
exp1 exp2 exp3
)
backBack top