I’m now studying Emacs Lisp from the reference manual and Common Lisp from a LISP Book.
from the Common Lisp book
>> (setf power-of-two
(let ((previous-power-of-two 1))
#'(lambda ()
(setf previous-power-of-two
(* previous-power-of-two 2)))))
>> (funcall power-of-two)
2
>> (funcall power-of-two)
4
>> (funcall power-of-two)
8
The function won’t work in Emacs Lisp because of its dynamic binding behavior.
I wonder if it is possible to implement the same function in Emacs Lisp without introducing a global variable ?
Update:
By now, Emacs 24 has been officially released, and it supports lexical binding without using
lexical-let, when the buffer-local variablelexical-bindingis non-nil. See alsoM-: (info "(elisp) using lexical binding")and pokita’s answer.You can use
lexical-letfrom the Common Lisp Extensions (the “CL package”):I’ve also heard about a lexbind branch of GNU Emacs.