📜  LISP 中的逻辑运算符

📅  最后修改于: 2022-05-13 01:54:45.952000             🧑  作者: Mango

LISP 中的逻辑运算符

Common LISP 支持 3 种类型的布尔值逻辑运算符。这些运算符的参数是有条件地计算的,因此它们也是 LISP 控制结构的一部分。

下表列出了常见的 LISP运算符:

OperatorSyntaxDescription
andand number1 number2This operator takes two numbers which are evaluated left to right. If all numbers evaluate to non-nil, then the value of the last number is returned. Otherwise, nil is returned.
oror number1 number2This operator takes two numbers which are evaluated left to right. If all numbers evaluate to non-nil, then the value of the last number is returned. Otherwise, nil is returned.
notnot numberThis operator takes one number and returns T(true) if the argument evaluates to NIL

示例:演示数字逻辑运算符的LISP 程序

Lisp
;set value 1 to 50
; set value 2 to 50
(setq val1 50)
(setq val2 50)
 
;and operator
(print (and val1 val2))
 
;or operator
(print (or val1 val2))
 
;not operator with value1
(print (not val1))
 
;not operator with value2
(print (not val2))


Lisp
;set value 1 to T
; set value 2 to NIL
(setq val1 T)
(setq val2 NIL)
 
;and operator
(print (and val1 val2))
 
;or operator
(print (or val1 val2))
 
;not operator with value1
(print (not val1))
 
;not operator with value2
(print (not val2))


输出:



50  
50  
NIL  
NIL 

示例 2:用于演示布尔值的逻辑运算符的LISP 程序

Lisp

;set value 1 to T
; set value 2 to NIL
(setq val1 T)
(setq val2 NIL)
 
;and operator
(print (and val1 val2))
 
;or operator
(print (or val1 val2))
 
;not operator with value1
(print (not val1))
 
;not operator with value2
(print (not val2))

输出:

NIL  
T  
NIL  
T