VOOZH about

URL: https://www.geeksforgeeks.org/lisp/operators-in-lisp/

⇱ Operators in LISP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

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 

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)  
maxThis operator returns the maximum value.(max num1 num2) 
minThis operator returns the minimum value.(min num1 num2)

Example: Comparison Operators

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)
orThis 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)
notThis operator takes one number and returns T(true) if the argument evaluates to NIL(not num1)

Example of Logical Operators

Output:

50 
50 
NIL 
NIL 

Bitwise Operator:

These operators are used to perform the manipulation of individual bits of a number.

Operator Description Syntax
logandThe operator returns bitwise logical AND of two numbers (logand num1 num2)
logiorThe operator returns bitwise Inclusive OR of two numbers(logior num1 num2)
logxorThe operator returns bitwise Exclusive OR of two numbers(logxor num1 num2)
lognorThe operator returns bitwise NOT of two numbers(lognor num1 num2)
logeqvThe operator returns bitwise Exclusive NOR of two numbers(logeqv num1 num2)
logcountThe operator returns the number of ones in the binary representation of an integer(logcount num1)
ashShifts the bits to the left if the count is positive else to the right(ash num count)

Example: Bitwise Operators

Output :

0 
15 
15 
-16 
-16 
2 
320 
0 
Comment