Lisp-comparison-operators
提供:Dev Guides
LISP-比較演算子
次の表は、数値を比較するLISPがサポートするすべての関係演算子を示しています。 ただし、他の言語の関係演算子とは異なり、LISP比較演算子は3つ以上のオペランドを取ることができ、数字のみで機能します。
変数 A が10を保持し、変数 B が20を保持すると仮定します-
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 |
例
main.lispという名前の新しいソースコードファイルを作成し、次のコードを入力します。
(setq a 10)
(setq b 20)
(format t "~% A = B is ~a" (= a b))
(format t "~% A/= B is ~a" (/= a b))
(format t "~% A > B is ~a" (> a b))
(format t "~% A < B is ~a" (< a b))
(format t "~% A >= B is ~a" (>= a b))
(format t "~% A <= B is ~a" (<= a b))
(format t "~% Max of A and B is ~d" (max a b))
(format t "~% Min of A and B is ~d" (min a b))
実行ボタンをクリックするか、Ctrl + Eを入力すると、LISPはすぐに実行し、返される結果は-
A = B is NIL
A/= B is T
A > B is NIL
A < B is T
A >= B is NIL
A <= B is T
Max of A and B is 20
Min of A and B is 10