如果不用 @tobin 建议的 Common Lisp 扩展,就得检查有没有提供可选参数。要留意,这里其实不需要 let。我觉得下面这样更符合习惯。
(defun perform (entry &optional extra)
(or extra (setq extra fallback))
(perform-body entry extra))
评论里有人认为,unless 可能比 or 更合适。
(defun perform (entry &optional extra)
(unless extra (setq extra fallback))
(perform-body entry extra))
评论里还有一个意见,更纯粹的函数式写法可以像问题里那样使用 let,但不需要起两个不同的名字。
(defun my-perform (entry &optional extra)
(let ((extra (or extra fallback)))
(perform-body entry extra)))
当然,如果可选参数只用一次,直接这样写就行。
(defun my-perform (entry &optional extra)
(perform-body entry (or extra fallback)))
从英语翻译·查看原文Without the Common Lisp additions @tobin suggested, you have to test whether an optional argument was provided. Note that a let isn't actually necessary in this situation. I'd find this version more idiomatic:
(defun perform (entry &optional extra)
(or extra (setq extra fallback))
(perform-body entry extra))
The comments suggest that unless could be a better choice than or:
(defun perform (entry &optional extra)
(unless extra (setq extra fallback))
(perform-body entry extra))
Another point from the comments: a more purely functional approach uses let, like the question does, without needing two different names:
(defun my-perform (entry &optional extra)
(let ((extra (or extra fallback)))
(perform-body entry extra)))
Naturally, when the optional argument occurs only once, you can simply write:
(defun my-perform (entry &optional extra)
(perform-body entry (or extra fallback)))