LISP 中的逻辑运算符
Common LISP 支持 3 种类型的布尔值逻辑运算符。这些运算符的参数是有条件地计算的,因此它们也是 LISP 控制结构的一部分。
下表列出了常见的 LISP运算符:Operator Syntax Description and and number1 number2 This 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. or or number1 number2 This 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. not not number This 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