Hi all, I was playing around with elisp macros in an org-file and came across a behavior I didn't expect that is due to lexical binding.
Here is the code: #+BEGIN_SRC emacs-lisp :results output :prologue ;; -*- lexical-binding: t -*- ;; you need this to make the binding. (setq lexical-binding t) ;; Graham's alambda (defmacro alambda (parms &rest body) `(cl-labels ((self ,parms ,@body)) #'self)) (setq N (alambda (n) (if (> n 0) (cons n (self (- n 1)))))) (funcall N 3) #+END_SRC The last line should return (3 2 1), and it does if I use C-x C-e on each form. If I try to run the block though, I get an error: cons: Symbol’s value as variable is void: --cl-self-- I can see where that comes from. The macro expands to: (lambda (n) (if (> n 0) (cons n (funcall --cl-self-- (- n 1))))) This doesn't work because babel simply evals the body of the code. It turns out you can use eval with lexical scoping: (eval FORM &optional LEXICAL) Evaluate FORM and return its value. If LEXICAL is t, evaluate using lexical scoping. So, I would like to propose adding the third argument to the eval statement that reads (assoc :lexical params) to turn on lexical eval if you want it. What do you think? -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu