From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: funny emacs-lisp macro behavior in org-babel related to lexical-binding Date: Thu, 14 Apr 2016 14:36:45 -0400 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:38129) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aqm8K-0002I0-Ra for emacs-orgmode@gnu.org; Thu, 14 Apr 2016 14:36:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aqm8H-0007Wa-LQ for emacs-orgmode@gnu.org; Thu, 14 Apr 2016 14:36:52 -0400 Received: from mail-qk0-x233.google.com ([2607:f8b0:400d:c09::233]:35329) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aqm8H-0007WT-HH for emacs-orgmode@gnu.org; Thu, 14 Apr 2016 14:36:49 -0400 Received: by mail-qk0-x233.google.com with SMTP id o6so31913144qkc.2 for ; Thu, 14 Apr 2016 11:36:49 -0700 (PDT) Received: from Johns-MacBook-Air.local (KITCHIN-TIMEMACHINE.CHEME.CMU.EDU. [128.2.54.215]) by smtp.gmail.com with ESMTPSA id l12sm18773345qki.19.2016.04.14.11.36.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Apr 2016 11:36:46 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode 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=E2=80=99s 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=20 (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