[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: defining many similar functions using macros
From: |
Daniel Pittman |
Subject: |
Re: defining many similar functions using macros |
Date: |
Sun, 03 Oct 2004 16:49:22 +1000 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (gnu/linux) |
On 3 Oct 2004, Joe Corneli wrote:
> I have a lot of functions that are very similar:
>
> (defun tex-alpha ()
> (interactive)
> (insert "\\alpha"))
>
> (defun tex-beta ()
> (interactive)
> (insert "\\beta"))
>
> ...
>
> I would like to define them all in one go:
[...]
> This seems like a good chance to use a macro. My first experiment
> along these lines fails however, and I could use some help
> re-designing it.
>
> This macro works on single elements:
[...]
> triggers an error:
[... when run from `dolist' ...]
> Debugger entered--Lisp error: (wrong-type-argument sequencep elt)
> concat("tex-" elt)
> (intern (concat "tex-" name))
> (list (quote defun) (intern (concat "tex-" name)) nil (quote (interactive))
> (list (quote insert) "\\" name))
> ...
>
> There seem to be some subtleties associated with macro expansion
> that I'm missing here. Help would be appreciated.
Indeed. The solution suggested elsewhere in the thread makes the macro
expansion work as expected, but why bother with that hoop - just write
the code directly:
(dolist (name '("alpha" "beta"))
(fset (intern (concat "tex-" name))
`(lambda () (interactive) (insert "\\" ,name))))
Note the backquote is needed to use the value of name, rather than the
symbol, but that cuts out the macro expansion middle-man nicely.
Daniel
--
We live in a hallucination of our own devising.
-- Alan Kay