Lessons

Comparisons

Welcome to the lesson on comparisons in AutoLISP! In any programming language, the ability to compare values is fundamental. This capability enables us to make decisions, filter data, and control program flow. In AutoLISP, the primary comparison operators we will be covering are < (less than), <= (less than or equal to), > (greater than), and /= (not equal to). By the end of this lesson, you’ll have a comprehensive understanding of how to use these operators effectively.


Learning Outcomes

By the end of this lesson on comparisons in AutoLISP, students will be able to:

  • Identify and Understand Comparison Operators: Recognize the significance of the <, <=, >, and /= operators and differentiate between their functionalities.
  • Apply Comparison Operators: Utilize the comparison operators in various programming scenarios, ensuring accurate and logical outcomes.
  • Interpret Results: Analyze the results of comparison operations, distinguishing between true (T) and false (nil) returns.
  • Integrate into Larger Codebases: Seamlessly incorporate these comparison operations into larger scripts, enhancing decision-making, data filtering, and loop controls.
  • Develop Logical Reasoning: Strengthen logical reasoning skills, crucial for problem-solving in programming, by mastering these foundational comparison concepts.

Understanding Comparison Operators

Comparison operators, as the name suggests, allow us to compare two values. In AutoLISP, these operators return a T (true) or nil (false), depending on the result of the comparison.

1. The < (Less Than) Operator

The < operator checks if the value on the left is less than the value on the right.

Syntax:

(< value1 value2)

Example 1:

(< 3 5) ; This will return T, because 3 is less than 5

In this example, we’re comparing the numbers 3 and 5. Since 3 is indeed less than 5, the function returns T, indicating a true comparison.

Example 2:

(< 10 5) ; This will return nil, because 10 is not less than 5

Here, we’re comparing 10 to 5. Since 10 is not less than 5, the result is nil, indicating the statement is false.


2. The <= (Less Than or Equal To) Operator

The <= operator checks if the value on the left is either less than or equal to the value on the right.

Syntax:

(<= value1 value2)

Example 1:

(<= 4 5) ; This will return T, because 4 is less than 5

In this scenario, we’re comparing 4 to 5. Since 4 is less than 5, the function returns T.

Example 2:

(<= 5 5) ; This will return T, because 5 is equal to 5

Here, we compare the number 5 to itself. The <= operator checks for both “less than” and “equal to” conditions. Since 5 equals 5, the result is T.


The > (Greater Than) Operator

The > operator checks if the value on the left is greater than the value on the right.

Syntax:

(> value1 value2)

Example 1:

(> 5 3) ; This will return T, because 5 is greater than 3

In this example, we’re comparing the numbers 5 and 3. Since 5 is indeed greater than 3, the function returns T, indicating a true comparison.

Example 2:

(> 4 10) ; This will return nil, because 4 is not greater than 10

Here, we’re comparing 4 to 10. Since 4 is not greater than 10, the result is nil, indicating the statement is false.


The /= (Not Equal To) Operator

The /= operator checks if the two values are not equal.

Syntax:

(/= value1 value2)

Example 1:

(/= 5 4) ; This will return T, because 5 is not equal to 4

In this example, the number 5 is being compared to 4. Since they are not equal, the function returns T, indicating the values are indeed different.

Example 2:

(/= 7 7) ; This will return nil, because both values are equal

Here, we’re comparing the number 7 to itself. Since both values are equal, the result is nil, indicating no difference between the two values.


Practical Applications of Comparison Operators

Comparison operators are not just for mathematical operations. They play a pivotal role in many aspects of programming:

  1. Conditional Expressions: Using if, cond, and other conditional functions, comparisons guide program decisions.
  2. Loop Control: In iterative loops, comparisons can determine whether a loop should continue or terminate.
  3. Data Filtering: When processing lists or arrays, comparisons help in filtering and sorting data.

Practical Examples

To deepen your understanding, let’s explore some real-world applications of these operators:

1. Determining the Smallest Number:

(defun smallest (num1 num2)(if (< num1 num2)
num1
num2
)
)(smallest 7 10) ; This will return 7

In this function, we use the < operator to compare two numbers. If num1 is smaller, it returns num1; otherwise, it returns num2.

2. Determining the Largest Number:

(defun largest (num1 num2)
(if (> num1 num2)
num1
num2
)
)
(largest 7 10) ; This will return 10

Here, we employ the > operator to determine the larger of two numbers. If num1 is greater, it returns num1; otherwise, it returns num2.

3. Checking Eligibility for a Senior Discount:

(defun seniorDiscount (age)
(if (<= age 65)
“Eligible for senior discount”
“Not eligible for senior discount”
)
)
(seniorDiscount 64) ; This will return “Eligible for senior discount”

Here, we use the <= operator to check if someone’s age qualifies them for a senior discount. If their age is 65 or older, they are eligible.

4. Youth Discount Function:

(defun youthDiscount (age)
(if (/= age 18)
“Eligible for youth discount”
“Not eligible for youth discount”
)
)
(youthDiscount 17) ; This will return “Eligible for youth discount”

For this function, the /= operator checks if someone’s age is different from 18. If their age is not 18, they qualify for a youth discount.


These practical examples showcase the versatility and importance of understanding and correctly applying the comparison operators in AutoLISP. Whether you’re designing tools for CAD operations or automating tasks, these operators will frequently be at the heart of your logic and decision-making processes.


Summary

In this lesson, we delved into the key comparison operators in AutoLISP:

  • < (Less Than): Compares if the left value is strictly smaller than the right.
  • <= (Less Than or Equal To): Checks if the left value is either smaller or equal to the right.
  • > (Greater Than): Assesses if the left value is strictly larger than the right.
  • /= (Not Equal To): Determines if the two values are different.

These operators are quintessential tools in your AutoLISP toolkit. Gaining proficiency in these comparisons establishes a robust foundation for delving into more intricate logical and conditional operations as you advance in your programming journey.

It’s pivotal to practice using these operators consistently and to explore varied scenarios. The more hands-on experience you gain, the more second-nature these concepts will become. As you continue on your AutoLISP educational journey, you’ll unearth the extensive applications of these seemingly straightforward, yet immensely potent tools.