Lisp - Logical Operators



Common LISP provides three logical operators: and, or, and not that operates on Boolean values. Assume A has value nil and B has value 5, then −

Operator Description Example
and It takes any number of arguments. The arguments are evaluated left to right. If all arguments evaluate to non-nil, then the value of the last argument is returned. Otherwise nil is returned. (and A B) will return NIL.
or It takes any number of arguments. The arguments are evaluated left to right until one evaluates to non-nil, in such case the argument value is returned, otherwise it returns nil. (or A B) will return 5.
not It takes one argument and returns t if the argument evaluates to nil. (not A) will return T.

Example - and Operator

Create a new source code file named main.lisp and type the following code in it. Here we're using and operator to check various scenarios.

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))

Output

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

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

Example - or Operator

Update the source code file named main.lisp and type the following code in it. Here we're using or operator to check various scenarios.

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))

Output

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

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

Example - not Operator

Update the source code file named main.lisp and type the following code in it. Here we're using not operator to check various scenarios.

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))

Output

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

not A is NIL

 not A is T

 not A is NIL

Please note that the logical operations work on Boolean values and secondly, numeric zero and NIL are not same.

lisp_operators.htm
Advertisements