Re: eval in bash vs macro in lisp
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Sat, 26 Aug 2006 00:59:17 +0200
Weiguang Shi <wgshi@xxxxxxxxxxxxxxxxxxxxx> writes:
I don't understand LISP macro well but happen to do bash programming
often.
Given some parameters, I can use ``eval'' to create functions on the fly.
Are there some similarities between the two, in concept? What are the
major differences?
Indeed there are some similarities.
Macros are functions used to generate code.
$ m () {
local name=$1
local op=$2
eval "$name () {
expr \$1 '$op' \$2
}"
}
$ m add \+
$ m mul \*
$ add 1 2
3
$ mul 2 3
6
$
CL-USER> (defmacro m (name op)
`(defun ,name (a b) (,op a b)))
M
CL-USER> (m add +)
ADD
CL-USER> (m mul *)
MUL
CL-USER> (add 1 2)
3
CL-USER> (mul 2 3)
6
CL-USER>
There is an "eval" hidden behind each macro.
We could write it as a function if we wanted to be more similar to
what happens in bash:
(defun m (name op) ; don't do that in lisp!
(eval `(defun ,name (a b) (,op a b)))) ; use a macro rather!
but we'd have to call it quoting the arguments:
(m 'add '+)
(like we have to do in bash: m mul \*
--
__Pascal Bourguignon__
.
- Follow-Ups:
- Re: eval in bash vs macro in lisp
- From: Weiguang Shi
- Re: eval in bash vs macro in lisp
- References:
- eval in bash vs macro in lisp
- From: Weiguang Shi
- eval in bash vs macro in lisp
- Prev by Date: eval in bash vs macro in lisp
- Next by Date: Re: eval in bash vs macro in lisp
- Previous by thread: eval in bash vs macro in lisp
- Next by thread: Re: eval in bash vs macro in lisp
- Index(es):