3.8 If-then-else表达式

3.8 If-then-else表达式

一个if表达式可能有一个可选的参数,称为else-part,当true-or-false-test返回false时。

     (if true-or-false-test
         action-to-carry-out-if-the-test-returns-true
       action-to-carry-out-if-the-test-returns-false)
例如,

     (if (> 4 5)                               ; if-part
         (message "4 falsely greater than 5!") ; then-part
       (message "4 is not greater than 5!"))   ; else-part
一个函数例子,

     (defun type-of-animal (characteristic)  ; Second version.
       "Print message in echo area depending on CHARACTERISTIC.
     If the CHARACTERISTIC is the string \"fierce\",
     then warn of a tiger; else say it is not fierce."
       (if (equal characteristic "fierce")
           (message "It is a tiger!")
         (message "It is not fierce!")))
安装上面的函数type-of-animal,测试:

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

第二个测试,回显区显示:"It is not fierce!"

你可能感兴趣的:(emacs)