Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.
Different types of Operators in LISP
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Arithmetic Operator:
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.
Operator | Description | Syntax |
---|
Addition Operator(+) | Add the two numbers | (+ num1 num2) |
---|
Subtraction Operator(-) | Subtract the second number from the first number | (- num1 num2) |
---|
Multiplication(*) | Multiply two numbers | (* num1 num2) |
---|
Division(/) | Divide the two numbers | (/ num1 num2) |
---|
Modulus(mod) | Get the remainder of two numbers | (MOD num1 num2) |
---|
Increment(incf) | Increment number by given value | (incf num1 num2) |
---|
Decrement(decf) | Decrement number by given value | (decf num1 num2) |
---|
Example: Arithmetic Operators
Lisp
;set value 1 to 300
; set value 2 to 600
(setq val1 300)
(setq val2 600)
;addition operation
(print (+ val1 val2))
;subtraction operation
(print (- val1 val2))
;multiplication operation
(print (* val1 val2))
;division operation
(print (/ val1 val2))
;modulus operation
(print (MOD val1 val2))
;increment a by 10
(print (incf val1 val2))
;decrement b by 20
(print (decf val1 val2))
Output :
900
-300
180000
1/2
300
900
300
Comparison Operator:
These operators are used to compare numbers by taking two or more operands.
Operator | Description | Syntax |
---|
= | This operator checks if the values of the operands are all equal or not, if yes, will return T(True) otherwise NIL | (= num1 num2) |
---|
/= | This operator checks if the values of the operands are not equal, if yes, will return NIL otherwise T (True) | (/= num1 num2) |
---|
> | This operator checks if the values of the operand 1 are greater than operand 2, if yes then it returns True, otherwise NIL | (> num1 num2) |
---|
< | This operator checks if the values of the operand 1 are less than operand 2, if yes then it returns True, otherwise NIL | (< num1 num2) |
---|
>= | This operator checks if the values of the operand 1 are greater than or equal to operand 2, if yes then it returns True, otherwise NIL | (>= num1 num2) |
---|
<= | This operator checks if the values of the operand 1 are less than or equal to operand 2, if yes then it returns True, otherwise NIL | (<= num1 num2) |
---|
max | This operator returns the maximum value. | (max num1 num2) |
---|
min | This operator returns the minimum value. | (min num1 num2) |
---|
Example: Comparison Operators
Lisp
;set value 1 to 100
; set value 2 to 200
(setq val1 100)
(setq val2 200)
;check val1 is equal to val2 or not
(print (= val1 val2))
;check val1 is not equal to val2 or not
(print (/= val1 val2))
;check val1 is greater than val2 or not
(print (> val1 val2))
;check val1 is less than val2 or not
(print (< val1 val2))
;check val1 is greater than or equal to val2 or not
(print (>= val1 val2))
;check val1 is less than or equal to val2 or not
(print (<= val1 val2))
;get maximum number among val1 and val2
(print (max val1 val2))
;get minimum number among val1 and val2
(print (min val1 val2))
Output:
NIL
T
NIL
T
NIL
T
200
100
Logical Operator:
Common LISP supports 3 types of logical operators on Boolean Values
Operator | Description | Syntax |
---|
and |
This operator takes two numbers which are evaluated left to right. If all numbers evaluate to non-nil, then the value of the last number is returned.
Otherwise, nil is returned.
| (and num1 num2) |
---|
or | This operator takes two numbers which are evaluated left to right. If any number evaluates to non-nil, then the value of the last number is returned. Otherwise, nil is returned. | (or num1 num2) |
---|
not | This operator takes one number and returns T(true) if the argument evaluates to NIL | (not num1) |
---|
Example of Logical Operators
Lisp
;set value 1 to 50
; set value 2 to 50
(setq val1 50)
(setq val2 50)
;and operator
(print (and val1 val2))
;or operator
(print (or val1 val2))
;not operator with value1
(print (not val1))
;not operator with value2
(print (not val2))
Output:
50
50
NIL
NIL
Bitwise Operator:
These operators are used to perform the manipulation of individual bits of a number.
Operator | Description | Syntax |
---|
logand | The operator returns bitwise logical AND of two numbers | (logand num1 num2) |
---|
logior | The operator returns bitwise Inclusive OR of two numbers | (logior num1 num2) |
---|
logxor | The operator returns bitwise Exclusive OR of two numbers | (logxor num1 num2) |
---|
lognor | The operator returns bitwise NOT of two numbers | (lognor num1 num2) |
---|
logeqv | The operator returns bitwise Exclusive NOR of two numbers | (logeqv num1 num2) |
---|
logcount | The operator returns the number of ones in the binary representation of an integer | (logcount num1) |
---|
ash | Shifts the bits to the left if the count is positive else to the right | (ash num count) |
---|
Example: Bitwise Operators
Lisp
;set value of variable val1 to 10
(setq val1 10)
;set value of variable val2 to 5
(setq val2 5)
;; logand operator
(print (logand val1 val2))
;; logior operator
(print (logior val1 val2))
;; logxor operator
(print (logxor val1 val2))
;; lognor operator
(print (lognor val1 val2))
;; logeqv operator
(print (logeqv val1 val2))
;; logcount operator
(print (logcount val2))
; arithmetic left shift
(print (ash val1 val2))
; arithmetic right shift
(print (ash val1 (- val2)))
Output :
0
15
15
-16
-16
2
320
0
Similar Reads
Bitwise Operators in LISP In this article, we will discuss the Bitwise operators in LISP. These operators are used to perform the manipulation of individual bits of a number. The truth table for bitwise AND, NAND, OR, XOR, NOR, & XNOR: aba and b a nand ba or b a xor b a nor b a xnor b00010011010111001110100110011100 Dif
4 min read
Vectors in LISP In this article, we will discuss Vectors in LISP. Vectors in LISP are one-dimensional arrays which are also known as sequences. We can create a vector using vector function and # symbol Syntax: variable_name(vector element1 element2 ... element n) or variable_name #(element1 element2 ... element n)
2 min read
Arithmetic Operators in LISP Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. There are 7 arithmetic operators in LISP that are listed in the below table: OperatorSyntaxDescriptionAddition Operator(+)+ num1 num2Add the two numbersSubtraction Operator(-)-
2 min read
Comparison Operators in LISP In this article, we will discuss the comparison operators in LISP. These operators are used to compare numbers by taking two or more operands. Note: This will work only on numbers, Different comparison operators are:OperatorSyntaxNameDescription== operand1 operand2equal toThis operator checks if the
4 min read
Predicates in LISP In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T. Types of predicates: Below is a list of major Predicates with
5 min read
Numbers in LISP LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. LISP Math Function:floor:Â floor returns the nearest smalles
2 min read
Strings in LISP A string is a set of characters. String  are enclosed in double-quotes. Example: "hello geek","java","python" etc Example: LISP program to display strings Lisp ;edisplay hello geek (write-line "Hello Geek") ;display (write-line "Welcome to java") Output: Hello Geek Welcome to javaString Comparison
4 min read
Operators in Objective-C The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 min read
Bitwise Operators in C++ In C+, Bitwise Operators are the operators that are used to perform bit-level operations on the integers. While performing these operations, integers are considered as sequences of binary digits. These operators are useful for low-level programming, system programming, and optimizing performance.C++
6 min read
Assignment Operators in C++ In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in
6 min read