位置:首頁 > 高級語言 > Lisp教學 > LISP - 運算符

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