📅  最后修改于: 2020-11-05 03:58:37             🧑  作者: Mango
运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。
Clojure具有以下类型的运算符-
注–在Clojure中,运算符和操作数以以下语法方式工作。
(operator operand1 operand2 operandn)
例如,
(+ 1 2)
上面的示例对数字1和2进行算术运算。
Clojure语言支持普通算术运算运算符,就像任何语言一样。以下是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 |
关系运算符允许比较对象。以下是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 |
逻辑运算符用于评估布尔表达式。以下是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 |
以下代码段显示了如何使用各种运算符。
Clojure提供了四个按位运算运算符。以下是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 |
以下是展示这些运算符的真值表。
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 |
通常,与LISP一样,无需担心运算符优先级。这是S表达式和前缀表示法的好处之一。所有功能从左到右,由内而外求值。 Clojure中的运算符只是函数,并且所有内容都已完全括在括号中。