LISP - 運算子


運算子是一個符號,它告訴編譯器執行特定的數學或邏輯操作。 LISP允許在眾多的資料業務,通過各種函式,巨集和其他結構的支援。

允許對資料的操作都可以歸類為:

  • 算術運算

  • 比較操作

  • 邏輯運算

  • 位運算

算術運算

下表列出了所有支援的LISP算術運算子。假設變數A=10和變數B=20則:

運算子 描述 Example
+ 增加了兩個運算元 (+ A B) = 30
- 從第一數減去第二個運算元 (- A B)= -10
* 乘兩個運算元 (* A B) = 200
/ 通過取消分子除以分子 (/ B A) = 2
mod,rem 模運算子和其餘整數除法後 (mod B A ) = 0
incf 遞增運算子,所指定的第二個引數增加整數值 (incf A 3) = 13
decf 遞減操作符,通過指定的第二個引數減小整數值 (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))

當您單擊Execute按鈕,或按下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,則:

Operator 描述 Example
= 檢查如果運算元的值都相等與否,如果是的話那麼條件為真。 (= A B)= true.
/= 檢查如果運算元的值都不同,或沒有,如果值不相等,則條件為真。 (/= A B) =true.
> 檢查如果運算元的值單調遞減。 (> A B) !=true.
< 檢查如果運算元的值單調遞增。 (< A B) = true.
>= 如有左運算元的值大於或等於下一個右運算元的值,如果是則條件檢查為真。 (>= A B) !=true.
<= 如有左運算元的值小於或等於其右運算元的值,如果是,則條件檢查為真。 (<= A B) = true.
max 它比較兩個或多個引數,並返回最大值。 (max A B) 返回20
min 它比較兩個或多個引數,並返回最小值。 (min A B) 返回 20

範例

建立一個名為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))

當您單擊Execute按鈕,或按下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

布林值邏輯操作

Common Lisp中提供了三種邏輯運算子:AND,OR,而不是運算子的布林值。假定A=nil,B=5,那麼

運算子 描述 範例
and 這需要任意數量的引數。該引數是從左向右計算。如果所有引數的計算結果為非零,那麼最後一個引數的值返回。否則就返回nil。 (and A B) = NIL.
or 這需要任意數量的引數。該引數是從左向右計算的,直到一個計算結果為非零,則此情況下返回引數值,否則返回nil。 (or A B) = 5.
not 它接受一個引數,並返回t,如果引數的計算結果為nil。 (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))

當您單擊Execute按鈕,或按下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

請注意,邏輯運算工作,布林值,其次,數位為零,NIL不是一樣的。

對數位運算

位運算子位工作並進行逐位元運算。對於按位元與,或,和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,則:

操作符 描述 Example
logand 這將返回位邏輯的引數和。如果沒有給出引數,則結果為-1,這是該操作的標識。 (logand a b)) = 12
logior 這將返回位邏輯包括它的引數或。如果沒有給出引數,那麼結果是零,這是該操作的標識。 (logior a b) = 61
logxor 這將返回其引數的按位元邏輯互斥或。如果沒有給出引數,那麼結果是零,這是該操作的標識。 (logxor a b) = 49
lognor 這不返回的逐位它的引數。如果沒有給出引數,則結果為-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))

當您單擊Execute按鈕,或按下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