emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: funny emacs-lisp macro behavior in org-babel related to lexical-binding
Date: Thu, 14 Apr 2016 14:36:45 -0400	[thread overview]
Message-ID: <m2y48gavgi.fsf@Johns-MacBook-Air.local> (raw)

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

             reply	other threads:[~2016-04-14 18:36 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-14 18:36 John Kitchin [this message]
2016-04-15 20:49 ` funny emacs-lisp macro behavior in org-babel related to lexical-binding 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2y48gavgi.fsf@Johns-MacBook-Air.local \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).