Lisp-operators
提供:Dev Guides
LISP-オペレーター
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 LISPでは、さまざまな機能、マクロ、およびその他の構成要素によってサポートされる、データに対する多数の操作が可能です。
データで許可された操作は次のように分類できます-
- 算術演算
- 比較操作
- 論理演算
- ビット演算
算術演算
次の表は、LISPでサポートされているすべての算術演算子を示しています。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
*link:/lisp/lisp_arithmetic_operators [例を表示]*
Operator | Description | Example |
---|---|---|
+ | Adds two operands | (+A B) will give 30 |
- | Subtracts second operand from the first | (- A B) will give -10 |
* | Multiplies both operands | (* A B) will give 200 |
/ | Divides numerator by de-numerator | (/B A) will give 2 |
mod,rem | Modulus Operator and remainder of after an integer division | (mod B A )will give 0 |
incf | Increments operator increases integer value by the second argument specified | (incf A 3) will give 13 |
decf | Decrements operator decreases integer value by the second argument specified | (decf A 4) will give 9 |
比較操作
次の表は、数値を比較するLISPがサポートするすべての関係演算子を示しています。 ただし、他の言語の関係演算子とは異なり、LISP比較演算子は3つ以上のオペランドを取ることができ、数字のみで機能します。
変数 A が10を保持し、変数 B が20を保持すると仮定します-
*link:/lisp/lisp_comparison_operators [例を表示]*
Operator | Description | Example |
---|---|---|
= | Checks if the values of the operands are all equal or not, if yes then condition becomes true. | (= A B) is not true. |
/= | Checks if the values of the operands are all different or not, if values are not equal then condition becomes true. | (/= A B) is true. |
> | Checks if the values of the operands are monotonically decreasing. | (> A B) is not true. |
< | Checks if the values of the operands are monotonically increasing. | (< A B) is true. |
>= | Checks if the value of any left operand is greater than or equal to the value of next right operand, if yes then condition becomes true. | (>= A B) is not true. |
⇐ | Checks if the value of any left operand is less than or equal to the value of its right operand, if yes then condition becomes true. | (⇐ A B) is true. |
max | It compares two or more arguments and returns the maximum value. | (max A B) returns 20 |
min | It compares two or more arguments and returns the minimum value. | (min A B) returns 10 |
ブール値の論理演算
Common LISPは、ブール値を操作する* and、or、、 *not の3つの論理演算子を提供します。 A の値がnilで、 B の値が5であると仮定します-
*link:/lisp/lisp_logical_operators [例を表示]*
Operator | Description | Example |
---|---|---|
and | It takes any number of arguments. The arguments are evaluated left to right. If all arguments evaluate to non-nil, then the value of the last argument is returned. Otherwise nil is returned. | (and A B) will return NIL. |
or | It takes any number of arguments. The arguments are evaluated left to right until one evaluates to non-nil, in such case the argument value is returned, otherwise it returns nil. | (or A B) will return 5. |
not | It takes one argument and returns t *if the argument evaluates to nil.* | (not A) will return T. |
数値のビット演算
ビットごとの演算子はビットに対して機能し、ビットごとの操作を実行します。 ビットごとのAND、OR、およびXOR演算の真理値表は次のとおりです-
*link:/lisp/lisp_bitwise_operators [例を表示]*
p | q | p and q | p or q | p xor q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A and B = 0000 1100
A or B = 0011 1101
A xor B = 0011 0001
not A = 1100 0011
次の表に、LISPでサポートされているビット単位の演算子を示します。 変数 A が60を保持し、変数 B が13を保持すると仮定します-
Operator | Description | Example |
---|---|---|
logand | This returns the bit-wise logical AND of its arguments. If no argument is given, then the result is -1, which is an identity for this operation. | (logand a b)) will give 12 |
logior | This returns the bit-wise logical INCLUSIVE OR of its arguments. If no argument is given, then the result is zero, which is an identity for this operation. | (logior a b) will give 61 |
logxor | This returns the bit-wise logical EXCLUSIVE OR of its arguments. If no argument is given, then the result is zero, which is an identity for this operation. | (logxor a b) will give 49 |
lognor | This returns the bit-wise NOT of its arguments. If no argument is given, then the result is -1, which is an identity for this operation. | (lognor a b) will give -62, |
logeqv | This returns the bit-wise logical EQUIVALENCE (also known as exclusive nor) of its arguments. If no argument is given, then the result is -1, which is an identity for this operation. | (logeqv a b) will give -50 |