📜  LISP 中的运算符

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

LISP 中的运算符

运算符是任何编程语言的基础。因此,如果不使用运算符,LISP 编程语言的功能是不完整的。我们可以将运算符定义为帮助我们对操作数执行特定数学和逻辑计算的符号。换句话说,我们可以说运算符对操作数进行操作。

LISP 中不同类型的运算符

  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 位运算符

算术运算符:

算术运算运算符用于执行数学运算,例如加法、减法、乘法和除法。

Operator Description Syntax
Addition Operator(+)Add the two numbers(+ num1 num2)
Subtraction Operator(-) Subtract the second number from the first number(- num1 num2)
Multiplication(*)Multiply two numbers(* num1 num2)
Division(/)Divide the two numbers(/ num1 num2)
Modulus(mod) Get the remainder of two numbers(MOD num1 num2)
Increment(incf)Increment number by given value(incf num1 num2)
Decrement(decf)Decrement number by given value(decf num1 num2)

示例:算术运算符

Lisp
;set value 1 to 300
; set value 2 to 600
(setq val1 300)
(setq val2 600)
  
;addition operation
(print (+ val1 val2))
  
;subtraction operation
(print (- val1 val2))
  
;multiplication operation
(print (* val1 val2))
  
;division operation
(print (/ val1 val2))
  
;modulus operation
(print (MOD val1 val2))
  
;increment a by 10
(print (incf val1 val2))
  
;decrement b by 20
(print (decf val1 val2))


Lisp
;set value 1 to 100
; set value 2 to 200
(setq val1 100)
(setq val2 200)
  
;check val1 is equal to val2 or not
(print (= val1 val2))
  
;check val1 is not equal to val2 or not
(print (/= val1 val2))
  
;check val1 is greater than val2 or not
(print (> val1 val2))
  
;check val1 is less than val2 or not
(print (< val1 val2))
  
;check val1 is greater than or equal to val2 or not
(print (>= val1 val2))
  
;check val1 is less than or equal to val2 or not
(print (<= val1 val2))
  
;get maximum number among val1 and val2
(print (max val1 val2))
  
;get minimum number among val1 and val2
(print (min val1 val2))


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 of variable val1 to 10
(setq val1 10)
;set value of variable val2 to 5
(setq val2 5)
  
;; logand operator
(print (logand val1 val2))
  
;; logior operator
(print (logior val1 val2))
  
;; logxor operator
(print (logxor val1 val2))
  
;; lognor operator
(print (lognor val1 val2))
  
;; logeqv operator
(print (logeqv val1 val2))
  
;; logcount operator
(print (logcount val2))
  
; arithmetic left shift
(print (ash val1 val2))   
  
; arithmetic right shift 
(print (ash val1 (- val2)))


输出 :

900  
-300  
180000  
1/2  
300  
900  
300 

比较运算符:

这些运算符用于通过取两个或多个操作数来比较数字。

Operator Description Syntax
=This operator checks if the values of the operands are all equal or not, if yes, will return T(True) otherwise NIL(= num1 num2)
/=This operator checks if the values of the operands are not equal, if yes, will return NIL otherwise T (True)(/= num1 num2)
>This operator checks if the values of the operand 1 are greater than operand 2, if yes then it returns True, otherwise NIL(> num1 num2)
<This operator checks if the values of the operand 1 are less than operand 2, if yes then it returns True, otherwise NIL(< num1 num2)
>=This operator checks if the values of the operand 1 are greater than or equal to  operand 2, if yes then it returns True, otherwise NIL(>= num1 num2)
<=This operator checks if the values of the operand 1 are less than or equal to  operand 2, if yes then it returns True, otherwise NIL(<= num1 num2)  
maxThis operator returns the maximum value.(max num1 num2) 
minThis operator returns the minimum value.(min num1 num2)

示例:比较运算符

语言

;set value 1 to 100
; set value 2 to 200
(setq val1 100)
(setq val2 200)
  
;check val1 is equal to val2 or not
(print (= val1 val2))
  
;check val1 is not equal to val2 or not
(print (/= val1 val2))
  
;check val1 is greater than val2 or not
(print (> val1 val2))
  
;check val1 is less than val2 or not
(print (< val1 val2))
  
;check val1 is greater than or equal to val2 or not
(print (>= val1 val2))
  
;check val1 is less than or equal to val2 or not
(print (<= val1 val2))
  
;get maximum number among val1 and val2
(print (max val1 val2))
  
;get minimum number among val1 and val2
(print (min val1 val2))

输出:

NIL  
T  
NIL  
T  
NIL  
T  
200  
100 

逻辑运算符:

Common LISP 在布尔值上支持 3 种类型的逻辑运算符

Operator Description Syntax 
and

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.

(and num1 num2)
orThis operator takes two numbers which are evaluated left to right. If any number evaluates to non-nil, then the value of the last number is returned. Otherwise, nil is returned.(or num1 num2)
notThis operator takes one number and returns T(true) if the argument evaluates to NIL(not num1)

逻辑运算符示例

语言

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

输出:

50  
50  
NIL  
NIL 

位运算符:

这些运算符用于执行数字的各个位的操作。

Operator Description Syntax
logandThe operator returns bitwise logical AND of two numbers (logand num1 num2)
logiorThe operator returns bitwise Inclusive OR of two numbers(logior num1 num2)
logxorThe operator returns bitwise Exclusive OR of two numbers(logxor num1 num2)
lognorThe operator returns bitwise NOT of two numbers(lognor num1 num2)
logeqvThe operator returns bitwise Exclusive NOR of two numbers(logeqv num1 num2)
logcountThe operator returns the number of ones in the binary representation of an integer(logcount num1)
ashShifts the bits to the left if the count is positive else to the right(ash num count)

示例:按位运算符

语言

;set value of variable val1 to 10
(setq val1 10)
;set value of variable val2 to 5
(setq val2 5)
  
;; logand operator
(print (logand val1 val2))
  
;; logior operator
(print (logior val1 val2))
  
;; logxor operator
(print (logxor val1 val2))
  
;; lognor operator
(print (lognor val1 val2))
  
;; logeqv operator
(print (logeqv val1 val2))
  
;; logcount operator
(print (logcount val2))
  
; arithmetic left shift
(print (ash val1 val2))   
  
; arithmetic right shift 
(print (ash val1 (- val2))) 

输出 :

0 
15 
15 
-16 
-16 
2 
320 
0