help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

hypothetical question about macros


From: Alan
Subject: hypothetical question about macros
Date: Tue, 24 May 2011 20:00:56 -0000
User-agent: G2/1.0

Here is a hypothetical question about macros.  Since a macro can
result in code that gets interpreted, suppose one wanted to have a
macro insert the following code:

(insert "#1\n")
(insert "#2\n")
(insert "#3\n")
(insert "#4\n")

This is a silly example, since in real life one is rather unlikely to
do such a thing.  However, it is an exercise of interest for
understanding macros.

Reading the documentation and making some trials, I don't see how to
do this.  For example

(defmacro aw-test ()
  '(insert "#1\n")
  '(insert "#2\n")
  '(insert "#3\n")
  '(insert "#4\n"))

and then using "eval-print-last-sexp" in the scratch buffer on

(aw-test)

results in

#4
nil

Similarly:

(macroexpand '(aw-test))

results in

(insert "#4
")

The following is a possibility:

(defmacro aw-test ()
  '(progn (insert "#1\n")
  (insert "#2\n")
  (insert "#3\n")
  (insert "#4\n")))

In the scratch buffer, using "eval-print-last-sexp" on

(aw-test)

then gives

#1
#2
#3
#4
nil

and

(macroexpand '(aw-test))

results in:

(progn (insert "#1
") (insert "#2
") (insert "#3
") (insert "#4
"))

But I had to resort to using "progn" in this case.

If there were some reason not to have to resort to using "progn" I
don't see how to do so.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]