Ernest A
Ernest A

Reputation: 7839

Unquote symbol in lambda expression?

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.

Upvotes: 0

Views: 762

Answers (2)

Stefan
Stefan

Reputation: 28571

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).

Upvotes: 1

Drew
Drew

Reputation: 30708

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.

Upvotes: 1

Related Questions