LISP 中的 if函数

首页 / LISP入门教程 / LISP 中的 if函数

if宏后面是一个TEST子句,其计算结果为t或nil。

if-的语法

(if (test-clause) (action1) (action2))

if - 示例1

创建一个名为main.lisp的新源代码文件,并在其中键入以下代码。

(setq a 10)
(if (> a 20)
   (format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)

单击执行按钮或键入Ctrl+E时,LISP会立即执行它,并且返回的结果为-

链接:https://www.learnfk.comhttps://www.learnfk.com/lisp/lisp-if-construct.html

来源:LearnFk无涯教程网

value of a is 10

if - 示例2

if子句后面可以跟一个可选的then子句。

创建一个名为main.lisp的新源代码文件,并在其中键入以下代码。

(setq a 10)
(if (> a 20)
   then (format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)

单击执行按钮或键入Ctrl+E时,LISP会立即执行它,并且返回的结果为-

链接:https://www.learnfk.comhttps://www.learnfk.com/lisp/lisp-if-construct.html

来源:LearnFk无涯教程网

a is less than 20
value of a is 10 

if - 示例3

还可以使用If子句创建If-Then-Else类型语句。

创建一个名为main.lisp的新源代码文件,并在其中键入以下代码。

(setq a 100)
(if (> a 20)
   (format t "~% a is greater than 20") 
   (format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)

单击执行按钮或键入Ctrl+E时,LISP会立即执行它,并且返回的结果为-

链接:https://www.learnfk.comhttps://www.learnfk.com/lisp/lisp-if-construct.html

来源:LearnFk无涯教程网

a is greater than 20
value of a is 100  

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入拆解Java虚拟机 -〔郑雨迪〕

玩转Git三剑客 -〔苏玲〕

OpenResty从入门到实战 -〔温铭〕

Node.js开发实战 -〔杨浩〕

安全攻防技能30讲 -〔何为舟〕

打造爆款短视频 -〔周维〕

说透区块链 -〔自游〕

B端产品经理入门课 -〔董小圣〕

结构执行力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)