- 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
廣告