an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
clojure has the following types of operators −
- arithmetic operators
- relational operators
- logical operators
- bitwise operators
note − in clojure, operators and operands work in the following syntax manner.
syntax
(operator operand1 operand2 operandn)
for example,
example
(+ 1 2)
the above example does an arithmetic operation on the numbers 1 and 2.
arithmetic operators
clojure language supports the normal arithmetic operators as any language. following are the arithmetic operators available in clojure.
| operator | description | example |
|---|---|---|
| + | addition of two operands | (+ 1 2) will give 3 |
| − | subtracts second operand from the first | (- 2 1) will give 1 |
| * | multiplication of both operands | (* 2 2) will give 4 |
| / | division of numerator by denominator | (float (/ 3 2)) will give 1.5 |
| inc | incremental operators used to increment the value of an operand by 1 | inc 5 will give 6 |
| dec | incremental operators used to decrement the value of an operand by 1 | dec 5 will give 4 |
| max | returns the largest of its arguments | max 1 2 3 will return 3 |
| min | returns the smallest of its arguments | min 1 2 3 will return 1 |
| rem | remainder of dividing the first number by the second | rem 3 2 will give 1 |
relational operators
relational operators allow comparison of objects. following are the relational operators available in clojure.
| operator | description | example |
|---|---|---|
| = | tests the equality between two objects | (= 2 2) will give true |
| not= | tests the difference between two objects | (not = 3 2) will give true |
| < | checks to see if the left object is less than the right operand | (< 2 3) will give true |
| <= | checks to see if the left object is less than or equal to the right operand | (<= 2 3) will give true |
| > | checks to see if the left object is greater than the right operand | (> 3 2) will give true |
| >= | checks to see if the left object is greater than or equal to the right operand | (>= 3 2) will give true |
logical operators
logical operators are used to evaluate boolean expressions. following are the logical operators available in groovy.
| operator | description | example |
|---|---|---|
| and | this is the logical “and” operator | (or true true) will give true |
| or | this is the logical “or” operator | (and true false) will give false |
| not | this is the logical “not” operator | (not false) will give true |
the following code snippet shows how the various operators can be used.
bitwise operators
clojure provides four bitwise operators. following are the bitwise operators available in clojure.
| sr.no. | operator & description |
|---|---|
| 1 |
bit-and this is the bitwise “and” operator |
| 2 |
bit-or this is the bitwise “or” operator |
| 3 |
bit-xor this is the bitwise “xor” or exclusive ‘or’ operator |
| 4 |
bit-not this is the bitwise negation operator |
following is the truth table showcasing these operators.
| p | q | p&q | p | q | p ^ q |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
operator precedence
as is the case with lisps in general, there is no need to worry about operator precedence. this is one of the benefits of s-expressions and prefix notation. all functions evaluate left to right and inside out. the operators in clojure are just functions, and everything is fully parenthesized.