3.7if

来源:互联网 发布:淘宝千人千面原理 编辑:程序博客网 时间:2024/06/16 21:51

3.7if

除了defun和let以外,第三种特殊形式是条件if。

用Lisp编写的if表达式不使用单词“then”;例如,

     (if (> 5 4)                             ; if-part         (message "5 is greater than 4!"))   ; then-part
另一个例子:

     (defun type-of-animal (characteristic)       "Print message in echo area depending on CHARACTERISTIC.     If the CHARACTERISTIC is the string \"fierce\",     then warn of a tiger."       (if (equal characteristic "fierce")           (message "It is a tiger!")))
安装type-of-animal函数后,测试一下:

     (type-of-animal "fierce")          (type-of-animal "striped")
第一个测试,会在回显区显示:"It is a tiger!"

第二个测试,在回显区显示:nil

在Lisp中,equal是一个函数,它决定了它的第一个参数是否等于它的第二个参数。第二个参数是字符串“fierce”,第一个参数是符号特征的值 - 换句话说,传递给这个函数的参数。

原创粉丝点击