Operators in LISP

• Last Updated : 29 Oct, 2021

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.

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.

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

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.

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

My Personal Notes arrow_drop_up