首页 > 其他分享 >无涯教程-LISP - 运算符

无涯教程-LISP - 运算符

时间:2024-01-13 20:31:36浏览次数:28  
标签:10 format LISP 无涯 40 运算符 setq operation

运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 LISP允许对数据进行大量操作,并由各种函数,宏和其他构造支持。

允许对数据进行的操作可以归类为-

  • 算术运算
  • 比较操作
  • 逻辑运算
  • 按位运行

算术运算

下表显示了LISP支持的所有算术运算符。假设变量 A=10,变量 B=20,然后-

运算符 描述 示例
+ 添加两个操作数 (+ A B)=30
- 从第一个中减去第二个操作数 (-A B)=-10
* 将两个操作数相乘 (* A B)=200
/ 按分子除分子 (/B A)=2
mod,rem 模运算符和整数除后的余数 (mod B A)=0
incf Increments运算符通过指定的第二个参数来增加整数值 (incf A 3)=13
decf Decrements运算符通过指定的第二个参数减小整数值 (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))

当您单击执行按钮或键入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,则-

运算符 描述 示例
= 检查操作数的值是否全部相等
(= A B) is not true.
/= 检查操作数的值是否全部不同 (/= A B) is true.
> 检查操作数的值是否单调递减。 (> A B) is not true.
< 检查操作数的值是否单调递增。 (< A B) is true.
>= 检查任何左操作数的值是否大于或等于下一个右操作数的值,如果是,则条件为true。
(>= A B) is not true.
<= 检查任何左操作数的值是否小于或等于其右操作数的值,如果是,则条件为true。 (<= A B) is true.
max 它比较两个或多个参数并返回最大值。 (max A B) returns 20
min 它比较两个或多个参数并返回最小值。 (min A B) returns 10

创建一个名为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))

当您单击执行按钮或键入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

逻辑运算

通用LISP提供了三个逻辑运算符: and,or,和 not ,它们对布尔值进行运算。假设 A 的值为nil,而 B 的值为5,则-

运算符 描述 示例
and 从左到右判断参数。如果所有参数的计算输出均为非nil,则返回最后一个参数的值。否则返回nil。 (and A B)=NIL。
or 自变量从左到右求值,直到一个值取非nil,在这种情况下,返回参数值,否则返回 nil 。 (or A B)=5。
not 它接受一个参数,如果该参数的值为 nil,则返回 t 。 (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))

当您单击执行按钮或键入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

按位运算

按位运算符对位进行运算并执行逐位操作。按位,or,and和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,则-

运算符 描述 示例
logand 这将返回其参数的按位逻辑与。如果未提供任何参数,则输出为-1,这是此操作的标识。 (logand a b))=12
logor 这将返回其参数的按位逻辑INCLUSIVE OR。如果未提供任何参数,则输出为零,这是此操作的标识。 (logior a b)=61
logxor 这将返回其参数的按位逻辑异或。如果未提供任何参数,则输出为零,这是此操作的标识。 (logxor a b)=49
lognor 这将返回其参数的按位NOT。如果未提供任何参数,则输出为-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))

当您单击执行按钮或键入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

参考链接

https://www.learnfk.com/lisp/lisp-operators.html

标签:10,format,LISP,无涯,40,运算符,setq,operation
From: https://blog.51cto.com/u_14033984/9233221

相关文章

  • 无涯教程-LISP - 常量声明
    在LISP中,常量是在程序执行期间永不更改其值的变量,常量使用defconstant构造进行声明。以下示例显示了声明全局常量PI并随后在名为area-circle的函数中使用此值来计算圆的面积的情况。defun构造用于定义一个函数,我们将在Function一章中对其进行研究。创建一个名为main.l......
  • 无涯教程-LISP - 宏(Macros)
    宏允许您扩展标准LISP的语法。定义宏在LISP中,使用另一个名为defmacro的宏定义了一个命名宏,定义宏的语法为-(defmacromacro-name(parameter-list))"Optionaldocumentationstring."body-form宏定义由宏的名称,参数列表,可选的文档字符串和Lisp表达式主体组成。让我们编写......
  • 无涯教程-LISP - 环境变量
    Lisp执行器CLISP是用于在Windows中设置LISP的GNU通用LISP多体系结构编译器,Windows版本使用Windows下的MingW模拟Unix环境,安装程序会处理此问题,并自动将clisp添加到WindowsPATH变量。您可以从此处获取最新的WindowsCLISP-https://sourceforge.net/projects/clisp/files/late......
  • 无涯教程-LISP - 简介
    Lisp(历史上拼写为LISP)是具有悠久历史的计算机编程语言家族,有独特和完全括号的前缀符号表示法。起源于公元1958年,是现今第二悠久而仍广泛使用的高端编程语言。只有FORTRAN编程语言比它更早一年。Lisp编程语族已经演变出许多种方言。现代最著名的通用编程语种是Clojure、CommonLis......
  • 无涯教程-Maven - 快照
    SNAPSHOT是一个特殊版本,指示当前的开发副本,与常规版本不同,Maven为每个构建都在远程存储库中检查新的SNAPSHOT版本。现在,数据服务团队将每次将SNAPSHOT更新的代码发布到存储库中,例如数据服务:1.0-SNAPSHOT,将替换旧的SNAPSHOTjar。快照与版本对于Version,如果Maven一旦下载了提......
  • 无涯教程-Maven - 构建和测试项目
    无涯教程在"CreateingProject"创建项目一章中学到的是如何使用Maven创建Java应用程序。现在将看到如何构建和测试应用程序。转到创建Java应用程序的C:/MVN目录。打开consumerBanking文件夹。您将看到POM.xml文件,其中包含以下内容。<projectxmlns="http://maven.apache.......
  • 无涯教程-Maven - 存储库(Repositories)
    用Maven术语来说,存储库是一个目录,所有项目jar,库jar,插件或任何其他项目特定的工件都存储在该目录中,并且Maven可以轻松使用它们。Maven存储库有三种类型。下图说明了这三种类型。localcentralremote本地存储库Maven本地存储库是计算机上的文件夹位置。首次运行任何maven命令......
  • 无涯教程-JUnit - 扩展类
    以下是JUnit扩展-CactusJWebUnitXMLUnitCactusCactus是用于对服务器端Java代码(Servlet,EJB,TagLib,Filters)进行单元测试的简单测试框架。Cactus的目的是降低编写服务器端代码测试的成本。它使用JUnit并将其扩展。由几个部分组成-CactusFramework是Cactus的核心,它是提供......
  • 无涯教程-Maven - 环境设置
    Maven是一个基于Java的工具,因此最首要的要求是在您的计算机上安装JDK。第1步-验证Java安装打开控制台并执行以下java命令。OSTaskCommandWindowsOpenCommandConsolec:\>java-versionLinuxOpenCommandTerminal$java-versionMacOpenTerminalmachine:~jo......
  • 无涯教程-Maven - 配置文件
    构建配置文件是一组配置值,可用于设置或覆盖Maven构建的默认值。文件类型构建配置文件主要分为三种类型。Type定义的地方PerProject在项目POM文件pom.xml中定义PerUser在Maven设置xml文件(%USER_HOME%/.m2/settings.xml)中定义Global在Maven全局设置xml文件(%M2_HOME%/co......