2013-05-14 83 views
4

我有一个(可能)与Elisp愚蠢的问题。我想要一个函数返回tnil,具体取决于when的条件。这是代码:Elisp函数返回值

(defun tmr-active-timer-p 
    "Returns t or nil depending of if there's an active timer" 
    (progn 
    (if (not (file-exists-p tmr-file)) 
     nil 
     ; (... more code) 
    ) 
    ) 
) 

但是我有一个错误。我不知道如何使一个函数返回一个值...我读过一个函数返回最后一个表达式的结果值,但在这种情况下,我不想做一些像(PHP混乱警告):

// code 

if ($condition) { 
    return false; 
} 

// more code... 

也许我错过了这一点,功能编程不允许这种方法?

+0

好的问题实际上是在tmr-active-timer-p之前缺少括号。 – hhaamm 2013-05-14 16:19:30

+2

您需要接受答案。 – phils 2013-05-15 11:01:56

回答

13

第一个,你需要在tmr-active-timer-p之后的参数列表;该defun语法

(defun function-name (arg1 arg2 ...) code...) 

,你不需要包裹身体的progn

第三,返回值是评估的最后一种形式。如果你的情况,你可以只写

(defun tmr-active-timer-p() 
    "Returns t or nil depending of if there's an active timer." 
    (when (file-exists-p tmr-file) 
     ; (... more code) 
    )) 

那么它将返回nil如果文件不存在(因为(when foo bar)相同(if foo (progn bar) nil))。

最后,在lisp中挂起的括号被认为是糟糕的代码格式化风格。

PS。 Emacs Lisp没有return,但它的确有Nonlocal Exits。我敦促你避开它们,除非你真的知道你在做什么。

+0

那么,你的回答是正确的。基本上没有回归sintaxis。你不能轻易从Elisp函数的中间返回,并且由于功能范例可能不是一个好主意。 感谢您使用悬括括号的建议。 – hhaamm 2014-12-05 19:50:43