hha*_*amm 5 emacs elisp return function
我对Elisp有一个(可能)愚蠢的问题.我希望函数返回t
或nil
根据when
条件.这是代码:
(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)
)
)
)
Run Code Online (Sandbox Code Playgroud)
但我有一个错误.我不确定如何使函数返回一个值...我已经读取了一个函数返回最后一个表达式结果值,但在这种情况下,我不想做类似的事情(PHP混乱警告):
// code
if ($condition) {
return false;
}
// more code...
Run Code Online (Sandbox Code Playgroud)
也许我错过了这一点,功能编程不允许这种方法?
sds*_*sds 16
首先,你需要一个参数列表tmr-active-timer-p
; 该defun
语法
(defun function-name (arg1 arg2 ...) code...)
Run Code Online (Sandbox Code Playgroud)
其次,你不需要将身体包裹起来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)
))
Run Code Online (Sandbox Code Playgroud)
nil
如果文件不存在,它将返回(因为(when foo bar)
它是相同的(if foo (progn bar) nil)
).
最后,挂起的括号在lisp中被认为是一种糟糕的代码格式化风格.
PS.Emacs Lisp没有return
,但它确实有Nonlocal Exits.除非你真的知道自己在做什么,否则我建议你避开它们.