Skip to content
Related Articles

Related Articles

Operators in LISP

Improve Article
Save Article
  • Last Updated : 29 Oct, 2021
Improve Article
Save Article

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 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!