无涯教程-LISP - if函数

62 阅读1分钟

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会立即执行它,并且返回的结果为-

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会立即执行它,并且返回的结果为-

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会立即执行它,并且返回的结果为-

a is greater than 20
value of a is 100  

参考链接

www.learnfk.com/lisp/lisp-i…