emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* funny emacs-lisp macro behavior in org-babel related to lexical-binding
@ 2016-04-14 18:36 John Kitchin
  2016-04-15 20:49 ` Nicolas Goaziou
  0 siblings, 1 reply; 8+ messages in thread
From: John Kitchin @ 2016-04-14 18:36 UTC (permalink / raw)
  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’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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-04-17 23:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-14 18:36 funny emacs-lisp macro behavior in org-babel related to lexical-binding John Kitchin
2016-04-15 20:49 ` Nicolas Goaziou
2016-04-16 13:41   ` John Kitchin
2016-04-16 16:52     ` Nicolas Goaziou
2016-04-17  1:08       ` John Kitchin
2016-04-17  8:18         ` Thomas S. Dye
2016-04-17 16:11         ` Charles C. Berry
2016-04-17 23:03           ` John Kitchin

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).