【发布时间】:2013-02-06 22:32:07
【问题描述】:
我是elisp 的新手,我正在尝试使用if 语句编写一个简单的程序。
重点是在学年阅读并返回 1、2、3、4 或 0 用于字符串 "freshman"、"sophomore"、"junior" , "senior" 分别(没有匹配时默认为0。我的代码如下:
(defun yearCode(name)
(if ( = name "freshman") 1
(if ( = name "sophomore") 2
(if ( = name "junior") 3
(if ( = name "senior") 4
(0))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; main function ;;;
;;; input: year ;;;
;;; output: code corresponding to year ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun main (year)
(interactive "sEnter your academic year: ") ; read year
(message "%d" (yearCode year))) ; display its code
;;; Tests...
(main "junior")
(yearCode "junior")
老实说,我对 elisp 一无所知,所以即使编译第一部分我也遇到了麻烦。谁能帮我正确地构造一个if 语句?
编辑:我在错误的缓冲区中测试 -_- 代码使用 if 语句工作。
【问题讨论】:
-
最后一次编辑完全改变了问题...当您传入一个不是四个预期字符串之一的字符串时,当前的
yearCode定义将失败(将=更改为@987654337 @) 因为它有一个表达式,其中第一个元素不是函数:(0)。
标签: emacs if-statement comparison elisp