gpt4 book ai didi

function - Elisp 函数返回值

转载 作者:行者123 更新时间:2023-12-04 13:19:46 24 4
gpt4 key购买 nike

我在 Elisp 上遇到了(可能)愚蠢的问题。我想要一个函数返回 tnil取决于 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)
)
)
)

但我有一个错误。我不知道如何让函数返回一个值......我读过一个函数返回最后一个表达式结果值,但在这种情况下,我不想做类似(PHP困惑警告)的事情:
// code

if ($condition) {
return false;
}

// more code...

也许我没有捕获重点,函数式编程不允许这种方法?

最佳答案

第一 , 在 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 中,挂括号被认为是一种糟糕的代码格式样式。

附注。 Emacs Lisp 没有 return ,但它确实有 Nonlocal Exits .我敦促您避免使用它们,除非您真的知道自己在做什么。

关于function - Elisp 函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16547908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com