[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: emacs lisp - unable to write a function maker
From: |
Drew Adams |
Subject: |
RE: emacs lisp - unable to write a function maker |
Date: |
Fri, 16 Sep 2011 07:06:13 -0700 |
> if you're using eval on something you don't want eval'd
> until the function is run, you should backquote it.
>
> (defun functionmaker (name form)
> (eval `(defun ,name () ,form)))
>
> I used to associate backquoting exclusively with macros, but I think
> that was wrong thinking.
Right.
But if you're evaluating the entire constructed form once at the top level like
that, then you likely do want to use a macro. That's just what a Lisp macro is:
a function that returns code that then gets evaluated.
(defmacro functionmaker (name form)
`(defun ,name () ,form))
Or, for the original question:
(defmacro makeAbrevFun (name val)
`(defun ,name () (insert ,val)))
(makeAbrevFun aa "aaaaaaaaaaaaaa")
Well, not quite - the OP quoted the function symbol, indicating that s?he would
pass something that needs to be eval'd. If that's rally part of the requirement,
then:
(defmacro makeAbrevFun (name val)
`(defun ,(eval name) () (insert ,val)))
(makeAbrevFun 'aa "aaaaaaaaaaaaaa")
In both cases: (symbol-function 'aa) gives:
(lambda () (insert "aaaaaaaaaaaaaa"))