Unquote symbol in lambda expression?

657 views Asked by At

In an anonymous function such as

(lambda () x)

how can I replace the symbol x with its value in the current scope?

The only thing I can think of is

(eval `(lambda () ,x))

but I wonder if there's another way.

2

There are 2 answers

2
Drew On BEST ANSWER

Remove the eval. Just `(lambda () ,x).

That returns the list (lambda () VAL-X), where VAL-X is the value of variable x. And a lambda list is interpreted by Emacs as a function.

2
Stefan On

The better solution is to add

;; -*- lexical-binding:t -*-

at the beginning of your file. Once you've done that, writing (lambda () x) is all it takes, since Emacs will then take care of replacing that x with the value from the scope surrounding that lambda (i.e. will create a proper closure).