Function Elisp函数返回值
我(可能)对Elisp有个愚蠢的问题。我希望函数根据Function Elisp函数返回值,function,emacs,return,elisp,Function,Emacs,Return,Elisp,我(可能)对Elisp有个愚蠢的问题。我希望函数根据when条件返回t或nil。代码如下: (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) ) ) ) 但我有个错误。我不知道如何使函数
when
条件返回t
或nil
。代码如下:
(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混乱警告)的事情:
也许我没有抓住要点,函数式编程不允许这种方法?首先,在
tmr-active-timer-p
之后需要一个参数列表;语法是
(defun function-name (arg1 arg2 ...) code...)
第二步,您不需要将身体包裹起来
第三个,返回值是最后计算的表单。如果你的案子你可以写下来
(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
(因为(当foo-bar时)
与(如果foo(progn-bar)nil)
)相同)
最后,在lisp中,挂括号被认为是一种糟糕的代码格式样式
PS.Emacs Lisp没有,但它确实有。除非你真的知道自己在做什么,否则我建议你避免使用它们。好的,问题实际上是在tmr-active-timer-p之前缺少括号。你需要接受答案。嗯,你的答案是正确的。基本上没有返回轴。在Elisp中,您不能轻易地从函数的中间返回,而且由于函数范式的原因,这可能不是一个好主意。谢谢你带括号的建议。
(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)
))