调用作为参数传递的函数

Calling a function passed as parameter

想要:如果传递了函数或闭包,则调用它,否则return 输入。我正在尝试这个:

(defun ifcall (x) (if (typep x 'FUNCTION) (funcall x) (x)))

并获得 "undefined function: X"。为什么?

您的 if 的其他部分不应包含在括号中。当您将某些东西放在括号内时,它将被视为函数调用。要 return 值,只需执行

(defun ifcall (x) (if (typep x 'FUNCTION) (funcall x) x))