Juraj
Juraj

Reputation: 870

Calling a function passed as parameter

Want: If a function or closure is passed, call it, otherwise return the input. I'm trying this:

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

and getting "undefined function: X". Why?

Upvotes: 1

Views: 106

Answers (1)

jkiiski
jkiiski

Reputation: 8421

The else-part of your if shouldn't be wrapped in parens. When you put something inside parens, it will be treated as a function call. To return the value, simply do

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

Upvotes: 5

Related Questions