all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: help-gnu-emacs@gnu.org
Subject: Re: obarray
Date: Tue, 17 Dec 2013 03:55:59 +0100	[thread overview]
Message-ID: <87haa8mbi8.fsf@web.de> (raw)
In-Reply-To: 87r49cp6o5.fsf@nl106-137-194.student.uu.se

Emanuel Berg <embe8573@student.uu.se> writes:

> Yes, this much I understood, only it was hard to
> visualize in practice. So: the macro accept code *as
> arguments* (code that isn't evaluated) and transforms
> that code?

Exactly.  Although, let's better say it accepts expressions (it must not
be valid code as such), and transforms them into code without evaluating
them before.  For example:

       (dolist (i '(1 2 3))
       		(message "%s" i))

The expressions (i '(1 2 3)) and (message "%s" i) are "arguments" to the
macro `dolist' in this call.  These are not evaluated, but transformed
into code.  You can see the transformed code with `macroexpand':

 (macroexpand '(dolist (i '(1 2 3))
		(message "%s" i)))

==>                

(cl--block-wrapper
 (catch '--cl-block-nil--
   (let
       ((--dolist-tail--
	 '(1 2 3))
	i)
     (while --dolist-tail--
       (setq i
	     (car --dolist-tail--))
       (message "%s" i)
       (setq --dolist-tail--
	     (cdr --dolist-tail--))))))

A macro call with the above arguments will first generate (expand) the
code to an expression like that, and will then execute it.

In the above case, the macro expanded to code that introduces another
variable, --cl-block-nil--.  In our case, it's an interned (!) symbol.
You can see why this isn't good when you try to evaluate

(dolist (--dolist-tail-- '(1 2 3))
  (message "%s" i))

Now you by coincidence use the symbol in the expression to be
transformed that the macro introduces as new variable.  They collide,
and

(dolist (--dolist-tail-- '(1 2 3))
  (message "%s" i))

doesn't work when evaluated.  So, it would be more correct to use an
uninterned symbol instead of '--dolist-tail--, then such a collision
could never happen.  They used a quite exotic name for the symbol so
such a collision is unlikely, but strictly speaking, the implementation
of dolist is not correct.

Defining `dolist' correctly is a good exercise for learning how to write
macros.


Regards,

Michael.




  reply	other threads:[~2013-12-17  2:55 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-15  0:54 obarray Emanuel Berg
2013-12-15  1:14 ` obarray Juanma Barranquero
     [not found] ` <mailman.9268.1387070101.10748.help-gnu-emacs@gnu.org>
2013-12-15  1:37   ` obarray Emanuel Berg
2013-12-15  1:56     ` obarray Michael Heerdegen
2013-12-15  1:59     ` obarray Juanma Barranquero
     [not found]     ` <mailman.9271.1387072648.10748.help-gnu-emacs@gnu.org>
2013-12-15  4:17       ` obarray Emanuel Berg
2013-12-15  4:17     ` obarray Barry Margolin
2013-12-15  4:47       ` obarray Michael Heerdegen
2013-12-15  4:55       ` obarray Emanuel Berg
2013-12-15  6:04         ` obarray Barry Margolin
2013-12-15 17:43           ` obarray Emanuel Berg
2013-12-16 17:44             ` obarray Barry Margolin
2013-12-17  1:47               ` obarray Michael Heerdegen
     [not found]               ` <mailman.9442.1387244871.10748.help-gnu-emacs@gnu.org>
2013-12-17  2:11                 ` obarray Emanuel Berg
2013-12-17  2:55                   ` Michael Heerdegen [this message]
     [not found]                   ` <mailman.9452.1387248989.10748.help-gnu-emacs@gnu.org>
2013-12-17  3:01                     ` obarray Emanuel Berg
2013-12-17 17:32                       ` obarray Barry Margolin
2013-12-17 17:42                         ` obarray Emanuel Berg
     [not found]       ` <mailman.9279.1387082898.10748.help-gnu-emacs@gnu.org>
2013-12-15  5:11         ` obarray Emanuel Berg
2013-12-15  5:36           ` obarray Emanuel Berg
2013-12-15  6:17             ` obarray Michael Heerdegen
     [not found]             ` <mailman.9283.1387088419.10748.help-gnu-emacs@gnu.org>
2013-12-15 17:51               ` obarray Emanuel Berg
2013-12-15  6:15           ` obarray Michael Heerdegen
     [not found]           ` <mailman.9282.1387088166.10748.help-gnu-emacs@gnu.org>
2013-12-15 17:47             ` obarray Emanuel Berg
2013-12-15  5:58         ` obarray Barry Margolin
2013-12-15 17:28           ` obarray Emanuel Berg
2013-12-17 14:38 ` obarray jack-mac

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

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

  git send-email \
    --in-reply-to=87haa8mbi8.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=help-gnu-emacs@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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.