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

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

Re: Difficult macro question: Doing custom `let'


From: lawrence mitchell
Subject: Re: Difficult macro question: Doing custom `let'
Date: Mon, 18 Aug 2003 19:20:07 +0100
User-agent: Gnus/5.090011 (Oort Gnus v0.11) Emacs/20.4 (sparc-sun-solaris2.6)

Jari Aalto+mail.emacs wrote:

[...] given list => '(a b c), get (let (a b c) ...)

> Now the problem is, How Do I make a macro that does exactly that above?
> The macro would be called inside function body:

>     (defun my-test ()
>       (my-let-transform list
>          (message "It worked.")))

> Which should be after macroexpand:

>     (defun my-test ()
>         (let (a
>               b
>               b)
>           (message "It worked.")))

If you do (require 'cl), you can try something like this:

(defmacro* my-transform-list ((&rest vars) &body body)
  ;; If VARS is a variable, assume we wanted its value.
  ;; otherwise, we just take it as a literal list.
  ;; This means that both (my-transform-list (a b) ...)
  ;; and (my-transform-list foo ...) work (assuming foo is boundp).
  (ignore-errors 
    (setq vars (symbol-value vars)))
  `(let ,vars
      ,@body))

(macroexpand '(my-transform-list (a b c) (message "It works.")))
  => (let (a b c)
       (message "It works."))

but also:
(setf list '(a b c))

(macroexpand '(my-transform-list list (message "It works.")))
  => (let (a b c)
       (message "It works."))

Note that there is no need to quote the list of variables if you
give them literally.

-- 
lawrence mitchell <wence@gmx.li>


reply via email to

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