运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 LISP允许对数据进行大量操作,并由各种函数,宏和其他构造支持。
允许对数据进行的操作可以归类为-
- 算术运算
- 比较操作
- 逻辑运算
- 按位运行
算术运算
下表显示了LISP支持的所有算术运算符。假设变量 A=10,变量 B=20,然后-
运算符 | 描述 | 示例 |
---|---|---|
+ | 添加两个操作数 | (+ A B)=30 |
- | 从第一个中减去第二个操作数 | (-A B)=-10 |
* | 将两个操作数相乘 | (* A B)=200 |
/ | 按分子除分子 | (/B A)=2 |
mod,rem | 模运算符和整数除后的余数 | (mod B A)=0 |
incf | Increments运算符通过指定的第二个参数来增加整数值 | (incf A 3)=13 |
decf | Decrements运算符通过指定的第二个参数减小整数值 | (decf A 4)=9 |
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(setq a 10) (setq b 20) (format t "~% A + B=~d" (+ a b)) (format t "~% A - B=~d" (- a b)) (format t "~% A x B=~d" (* a b)) (format t "~% B/A=~d" (/ b a)) (format t "~% Increment A by 3=~d" (incf a 3)) (format t "~% Decrement A by 4=~d" (decf a 4))
当您单击执行按钮或键入Ctrl + E时,LISP立即执行它,返回的结果是-
A + B=30 A - B=-10 A x B=200 B/A=2 Increment A by 3=13 Decrement A by 4=9
比较操作
下表显示了LISP支持的所有关系运算符,它们在数字之间进行比较。但是,与其他语言中的关系运算符不同,LISP比较运算符可以采用两个以上的操作数,并且它们仅对数字起作用。
假设变量 A=10,变量 B=20,则-
运算符 | 描述 | 示例 |
---|---|---|
= | 检查操作数的值是否全部相等 |
(= A B) is not true. |
/= | 检查操作数的值是否全部不同 | (/= A B) is true. |
> | 检查操作数的值是否单调递减。 | (> A B) is not true. |
< | 检查操作数的值是否单调递增。 | (< A B) is true. |
>= | 检查任何左操作数的值是否大于或等于下一个右操作数的值,如果是,则条件为true。 |
(>= A B) is not true. |
<= | 检查任何左操作数的值是否小于或等于其右操作数的值,如果是,则条件为true。 | (<= A B) is true. |
max | 它比较两个或多个参数并返回最大值。 | (max A B) returns 20 |
min | 它比较两个或多个参数并返回最小值。 | (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
逻辑运算
通用LISP提供了三个逻辑运算符: and,or,和 not ,它们对布尔值进行运算。假设 A 的值为nil,而 B 的值为5,则-
运算符 | 描述 | 示例 |
---|---|---|
and | 从左到右判断参数。如果所有参数的计算输出均为非nil,则返回最后一个参数的值。否则返回nil。 | (and A B)=NIL。 |
or | 自变量从左到右求值,直到一个值取非nil,在这种情况下,返回参数值,否则返回 nil 。 | (or A B)=5。 |
not | 它接受一个参数,如果该参数的值为 nil,则返回 t 。 | (not A)=T。 |
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(setq a 10) (setq b 20) (format t "~% A and B is ~a" (and a b)) (format t "~% A or B is ~a" (or a b)) (format t "~% not A is ~a" (not a)) (terpri) (setq a nil) (setq b 5) (format t "~% A and B is ~a" (and a b)) (format t "~% A or B is ~a" (or a b)) (format t "~% not A is ~a" (not a)) (terpri) (setq a nil) (setq b 0) (format t "~% A and B is ~a" (and a b)) (format t "~% A or B is ~a" (or a b)) (format t "~% not A is ~a" (not a)) (terpri) (setq a 10) (setq b 0) (setq c 30) (setq d 40) (format t "~% Result of and operation on 10, 0, 30, 40 is ~a" (and a b c d)) (format t "~% Result of and operation on 10, 0, 30, 40 is ~a" (or a b c d)) (terpri) (setq a 10) (setq b 20) (setq c nil) (setq d 40) (format t "~% Result of and operation on 10, 20, nil, 40 is ~a" (and a b c d)) (format t "~% Result of and operation on 10, 20, nil, 40 is ~a" (or a b c d))
当您单击执行按钮或键入Ctrl + E时,LISP立即执行它,返回的结果是-
A and B is 20 A or B is 10 not A is NIL A and B is NIL A or B is 5 not A is T A and B is NIL A or B is 0 not A is T Result of and operation on 10, 0, 30, 40 is 40 Result of and operation on 10, 0, 30, 40 is 10 Result of and operation on 10, 20, nil, 40 is NIL Result of and operation on 10, 20, nil, 40 is 10
按位运算
按位运算符对位进行运算并执行逐位操作。按位,or,and和xor运算的真值表如下-
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,则-
运算符 | 描述 | 示例 |
---|---|---|
logand | 这将返回其参数的按位逻辑与。如果未提供任何参数,则输出为-1,这是此操作的标识。 | (logand a b))=12 |
logor | 这将返回其参数的按位逻辑INCLUSIVE OR。如果未提供任何参数,则输出为零,这是此操作的标识。 | (logior a b)=61 |
logxor | 这将返回其参数的按位逻辑异或。如果未提供任何参数,则输出为零,这是此操作的标识。 | (logxor a b)=49 |
lognor | 这将返回其参数的按位NOT。如果未提供任何参数,则输出为-1,这是此操作的标识。 | (lognor a b)=-62, |
logeqv | 这将返回其参数的按位逻辑等效(也称为"异或")。如果未提供任何参数,则输出为-1,这是此操作的标识。 | (logeqv a b)=-50 |
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(setq a 60) (setq b 13) (format t "~% BITWISE AND of a and b is ~a" (logand a b)) (format t "~% BITWISE INCLUSIVE OR of a and b is ~a" (logior a b)) (format t "~% BITWISE EXCLUSIVE OR of a and b is ~a" (logxor a b)) (format t "~% A NOT B is ~a" (lognor a b)) (format t "~% A EQUIVALANCE B is ~a" (logeqv a b)) (terpri) (terpri) (setq a 10) (setq b 0) (setq c 30) (setq d 40) (format t "~% Result of bitwise and operation on 10, 0, 30, 40 is ~a" (logand a b c d)) (format t "~% Result of bitwise or operation on 10, 0, 30, 40 is ~a" (logior a b c d)) (format t "~% Result of bitwise xor operation on 10, 0, 30, 40 is ~a" (logxor a b c d)) (format t "~% Result of bitwise eqivalance operation on 10, 0, 30, 40 is ~a" (logeqv a b c d))
当您单击执行按钮或键入Ctrl + E时,LISP立即执行它,返回的结果是-
BITWISE AND of a and b is 12 BITWISE INCLUSIVE OR of a and b is 61 BITWISE EXCLUSIVE OR of a and b is 49 A NOT B is -62 A EQUIVALANCE B is -50 Result of bitwise and operation on 10, 0, 30, 40 is 0 Result of bitwise or operation on 10, 0, 30, 40 is 62 Result of bitwise xor operation on 10, 0, 30, 40 is 60 Result of bitwise eqivalance operation on 10, 0, 30, 40 is -61
参考链接
https://www.learnfk.com/lisp/lisp-operators.html
标签:10,format,LISP,无涯,40,运算符,setq,operation From: https://blog.51cto.com/u_14033984/9233221