2

It seems like for a function with no arguments, apply and funcall should have the same behavior. However,

(let ((x (lambda nil (message "called"))))
  (message "x: %s" x)
  (funcall x)
  (apply x))

produces this output in *Messages*

x: (closure (t) nil (message called))
called
apply: Symbol’s function definition is void: closure

I get a similar result if I pass the lambda as an argument to a method, which (I think) gives me dynamic binding instead of lexical binding.

The documentation for lambda says (in part)

Regardless, the result is a function, i.e., it may be stored as the function value of a symbol, passed to ‘funcall’ or ‘mapcar’, etc.

So apply is not explicitly included there, but it seems like it should be part of etc.

Is there some reason why apply does not work here that I am missing?

For my current case, I can use funcall, but I would like to better understand what is happening.

1 Answer 1

4

You have a misunderstanding of how to use the apply function. Please read the documentation carefully:

Call FUNCTION with our remaining args, using our last arg as list of args.

So it should be

(apply x ())

instead of

(apply x)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.