- LISP 教程
- LISP - 首页
- LISP - 概述
- LISP - 环境
- LISP - 程序结构
- LISP - 基本语法
- LISP - 数据类型
- LISP - 宏
- LISP - 变量
- LISP - 常量
- LISP - 运算符
- LISP - 决策
- LISP - 循环
- LISP - 函数
- LISP - 谓词
- LISP - 数字
- LISP - 字符
- LISP - 数组
- LISP - 字符串
- LISP - 序列
- LISP - 列表
- LISP - 符号
- LISP - 向量
- LISP - 集合
- LISP - 树
- LISP - 哈希表
- LISP - 输入与输出
- LISP - 文件 I/O
- LISP - 结构体
- LISP - 包
- LISP - 错误处理
- LISP - CLOS (Common Lisp 对象系统)
- LISP 有用资源
- Lisp - 快速指南
- Lisp - 有用资源
- Lisp - 讨论
Lisp - 逻辑运算符
Common 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。 |
示例 - and 运算符
创建一个名为 main.lisp 的新源代码文件,并在其中输入以下代码。这里我们使用and运算符来检查各种情况。
main.lisp
; set a as 10 (setq a 10) ; set b as 20 (setq b 20) ; perform and operation on a and b (format t "~% A and B is ~a" (and a b)) ; terminate printing (terpri) ; set a as nil (setq a nil) ; set b as 5 (setq b 5) ; perform and operation on a and b (format t "~% A and B is ~a" (and a b)) ; terminate printing (terpri) ; set a as nil (setq a nil) ; set b as 0 (setq b 0) ; perform and operation on a and b (format t "~% A and B is ~a" (and a b)) ; terminate printing (terpri) ; set values to variables (setq a 10) (setq b 0) (setq c 30) (setq d 40) ; print and operation on all four variables (format t "~% Result of and operation on 10, 0, 30, 40 is ~a" (and a b c d)) ; terminate printing (terpri) ; set values to variables (setq a 10) (setq b 20) (setq c nil) (setq d 40) ; print and operation on all four variables (format t "~% Result of and operation on 10, 20, nil, 40 is ~a" (and a b c d))
输出
单击“执行”按钮或键入 Ctrl+E 时,LISP 会立即执行它,并返回结果:
A and B is 20 A and B is NIL A and B is NIL Result of and operation on 10, 0, 30, 40 is 40 Result of and operation on 10, 20, nil, 40 is NIL
示例 - or 运算符
更新名为 main.lisp 的源代码文件,并在其中输入以下代码。这里我们使用or运算符来检查各种情况。
main.lisp
; set a as 10 (setq a 10) ; set b as 20 (setq b 20) ; print or operation on a and b (format t "~% A or B is ~a" (or a b)) ; terminate printing (terpri) ; set a as nil (setq a nil) ; set b as 5 (setq b 5) ; perform or operation on a and b (format t "~% A or B is ~a" (or a b)) ; terminate printing (terpri) ; set a as nil (setq a nil) ; set b as 0 (setq b 0) ; perform or operation on a and b (format t "~% A or B is ~a" (or a b)) ; terminate printing (terpri) ; set values of variables (setq a 10) (setq b 0) (setq c 30) (setq d 40) ; perform or operation on variables (format t "~% Result of and operation on 10, 0, 30, 40 is ~a" (or a b c d)) ; terminate printing (terpri) ; set values of variables (setq a 10) (setq b 20) (setq c nil) (setq d 40) ; perform or operation on variables (format t "~% Result of and operation on 10, 20, nil, 40 is ~a" (or a b c d))
输出
单击“执行”按钮或键入 Ctrl+E 时,LISP 会立即执行它,并返回结果:
A or B is 10 A or B is 5 A or B is 0 Result of and operation on 10, 0, 30, 40 is 10 Result of and operation on 10, 20, nil, 40 is 10
示例 - not 运算符
更新名为 main.lisp 的源代码文件,并在其中输入以下代码。这里我们使用not运算符来检查各种情况。
main.lisp
; set a as 10 (setq a 10) ; perform not operation on a (format t "~% not A is ~a" (not a)) ; terminate printing (terpri) ; set q as nil (setq a nil) ; perform not operation on a (format t "~% not A is ~a" (not a)) ; terminate printing (terpri) ; set q as 0 (setq a 0) ; perform not operation on a (format t "~% not A is ~a" (not a))
输出
单击“执行”按钮或键入 Ctrl+E 时,LISP 会立即执行它,并返回结果:
not A is NIL not A is T not A is NIL
请注意,逻辑运算作用于布尔值,其次,数值零和 NIL 不相同。
lisp_operators.htm
广告