unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Macro vs Function
@ 2006-04-12  8:22 Herbert Euler
  2006-04-12  9:07 ` David Kastrup
  0 siblings, 1 reply; 26+ messages in thread
From: Herbert Euler @ 2006-04-12  8:22 UTC (permalink / raw)


Hello,

After reading defmacro and eval in eval.c, I conclude that, in
Emacs Lisp, if I substitute "defmacro" at the beginning of
definition of a macro with "defun" and quote each argument in the
call the behavior of code will not be altered.  Is this correct?
And, is it correct for other Lisp implementations?

Thanks in advance.

Regards,
Guanpeng Xu

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12  8:22 Herbert Euler
@ 2006-04-12  9:07 ` David Kastrup
  2006-04-12  9:17   ` Herbert Euler
  0 siblings, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12  9:07 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

> Hello,
>
> After reading defmacro and eval in eval.c, I conclude that, in
> Emacs Lisp, if I substitute "defmacro" at the beginning of
> definition of a macro with "defun" and quote each argument in the
> call the behavior of code will not be altered.  Is this correct?

No.  The macro is executed at function definition time, the function
at execution time.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12  9:07 ` David Kastrup
@ 2006-04-12  9:17   ` Herbert Euler
  2006-04-12  9:24     ` David Kastrup
  0 siblings, 1 reply; 26+ messages in thread
From: Herbert Euler @ 2006-04-12  9:17 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 11:07:48 +0200
>
>The macro is executed at function definition time, the function
>at execution time.

Sorry I don't quite understand this.  Could you please explain it?
Thanks.  But the following two programs seem to be same:

    (defun g ()
      nil)

    ;; macro
    (defmacro m (a)
      (if a
          t
        nil))
    (m (g))

    ;; function
    (defun f (a)
      (if a
          t
        nil))
    (f '(g))

And, these two seem to be same as well:

    ;; macro
    (defmacro m (a)
      (if (eval a)
          t
        nil))
    (m (g))

    ;; function
    (defun f (a)
      (if (eval a)
          t
        nil))
    (f '(g))

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12  9:17   ` Herbert Euler
@ 2006-04-12  9:24     ` David Kastrup
  2006-04-12  9:39       ` Herbert Euler
  0 siblings, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12  9:24 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: David Kastrup <dak@gnu.org>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 11:07:48 +0200
>>
>>The macro is executed at function definition time, the function
>>at execution time.
>
> Sorry I don't quite understand this.  Could you please explain it?
> Thanks.  But the following two programs seem to be same:

Which two programs?

>    (defun g ()
>      nil)
>
>    ;; macro
>    (defmacro m (a)
>      (if a
>          t
>        nil))
>    (m (g))
>
>    ;; function
>    (defun f (a)
>      (if a
>          t
>        nil))
>    (f '(g))

There is a difference between calling a macro directly, and compiling
a function refering to a macro.


-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12  9:24     ` David Kastrup
@ 2006-04-12  9:39       ` Herbert Euler
  2006-04-12  9:53         ` David Kastrup
  0 siblings, 1 reply; 26+ messages in thread
From: Herbert Euler @ 2006-04-12  9:39 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 11:24:16 +0200
>
>Which two programs?

If I define a "program" as a macro or function definition followed by
an application to this macro or function, there were four "programs"
included in my last post (except the definition of "g", which is an
introduction to functions refered in "programs").

>There is a difference between calling a macro directly, and compiling
>a function refering to a macro.

So the behavior is same, but when compiling there are slight
differences?

Regards,
Guanpeng Xu

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! 
http://search.msn.com/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12  9:39       ` Herbert Euler
@ 2006-04-12  9:53         ` David Kastrup
  2006-04-12 10:39           ` Herbert Euler
  0 siblings, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12  9:53 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: David Kastrup <dak@gnu.org>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 11:24:16 +0200
>>
>>Which two programs?
>
> If I define a "program" as a macro or function definition followed by
> an application to this macro or function, there were four "programs"
> included in my last post (except the definition of "g", which is an
> introduction to functions refered in "programs").
>
>>There is a difference between calling a macro directly, and compiling
>>a function refering to a macro.
>
> So the behavior is same, but when compiling there are slight
> differences?

The behavior is not the same.  When a function call gets executed, the
arguments are first evaluated and then the call is made.  When a macro
call gets executed, the arguments are quoted, the macro gets called,
and the resulting form gets evaluated.

Here is your original claim:

> After reading defmacro and eval in eval.c, I conclude that, in
> Emacs Lisp, if I substitute "defmacro" at the beginning of
> definition of a macro with "defun" and quote each argument in the
> call the behavior of code will not be altered.  Is this correct?

Now take a look:

(defmacro xxx (a) a)
(defun yyy (a) a)

(xxx (+ 3 4)) => 7
(yyy '(+ 3 4)) => (+ 3 4)

So even when not compiling, your description is wrong.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12  9:53         ` David Kastrup
@ 2006-04-12 10:39           ` Herbert Euler
  2006-04-12 11:14             ` Andreas Schwab
  2006-04-12 11:19             ` David Kastrup
  0 siblings, 2 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 10:39 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 11:53:25 +0200
>
>(defmacro xxx (a) a)
>(defun yyy (a) a)
>
>(xxx (+ 3 4)) => 7
>(yyy '(+ 3 4)) => (+ 3 4)

I'm confused now.  Take a look at this:

    (defun f ()
      nil)

    (defmacro m (a)
      (if a
          t
        nil))

    (m (f)) => t

Why does (f) in this example not get evaluated?

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 10:39           ` Herbert Euler
@ 2006-04-12 11:14             ` Andreas Schwab
  2006-04-12 11:56               ` Herbert Euler
  2006-04-12 11:19             ` David Kastrup
  1 sibling, 1 reply; 26+ messages in thread
From: Andreas Schwab @ 2006-04-12 11:14 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

> Why does (f) in this example not get evaluated?

Because the point of a macro is that you can transform the arguments into
a new form to be evaluated.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 10:39           ` Herbert Euler
  2006-04-12 11:14             ` Andreas Schwab
@ 2006-04-12 11:19             ` David Kastrup
  1 sibling, 0 replies; 26+ messages in thread
From: David Kastrup @ 2006-04-12 11:19 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: David Kastrup <dak@gnu.org>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 11:53:25 +0200
>>
>>(defmacro xxx (a) a)
>>(defun yyy (a) a)
>>
>>(xxx (+ 3 4)) => 7
>>(yyy '(+ 3 4)) => (+ 3 4)
>
> I'm confused now.  Take a look at this:
>
>    (defun f ()
>      nil)
>
>    (defmacro m (a)
>      (if a
>          t
>        nil))
>
>    (m (f)) => t
>
> Why does (f) in this example not get evaluated?

Uh, why should it get evaluated?  Macro arguments are _never_
evaluated.  They are passed quoted into the macro, the macro gets
executed, and the return value of the macro gets evaluated.

In the above, m is called with '(f) as its argument.  (if '(a)
... obviously evaluates to t which the macro returns.  Then t gets
evaluated, but since it is a self-quoting form, nothing else happens
with it.

Here is a more complex example:

(defmacro m (a)
  (append a '(2 4)))

(m (+)) => 6

And here is what happens in compilation:

(defun xxx () (m (or)))

M-x disassemble RET xxx RET

byte code for xxx:
  args: nil
0	constant  2
1	return	  

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 11:14             ` Andreas Schwab
@ 2006-04-12 11:56               ` Herbert Euler
  2006-04-12 12:08                 ` Herbert Euler
  2006-04-12 12:12                 ` David Kastrup
  0 siblings, 2 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 11:56 UTC (permalink / raw)
  Cc: emacs-devel

>From: Andreas Schwab <schwab@suse.de>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 13:14:19 +0200
>
>Because the point of a macro is that you can transform the arguments into
>a new form to be evaluated.

So parameters in macro definition is always not evaluated, but
left there, and when the macro returns the expansion, Lisp
interpreter will evaluate them?

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
@ 2006-04-12 12:05 Herbert Euler
  2006-04-12 12:13 ` David Kastrup
  0 siblings, 1 reply; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 12:05 UTC (permalink / raw)


>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 13:19:17 +0200
>
>Macro arguments are _never_ evaluated.  They are passed quoted
>into the macro, the macro gets executed, and the return value of
>the macro gets evaluated.

This verifies my hypothesis.  Thanks.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 11:56               ` Herbert Euler
@ 2006-04-12 12:08                 ` Herbert Euler
  2006-04-12 12:19                   ` David Kastrup
  2006-04-12 12:12                 ` David Kastrup
  1 sibling, 1 reply; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 12:08 UTC (permalink / raw)


By the way, if I want my Emacs Lisp program able to be successfully
compiled, shall macros in the program "finite"?  For example, the
following macro can't be compiled, can it?

(defmacro xgp-casi2-safe-call-iter (func largs)
  (if largs
      `(condition-case nil
	   (apply ,func (car ,largs))
	 (error (xgp-casi2-safe-call-iter ,func (cdr ,largs))))
    'nil))

Is there some way of making it able to be compiled?

Thanks in advance.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 11:56               ` Herbert Euler
  2006-04-12 12:08                 ` Herbert Euler
@ 2006-04-12 12:12                 ` David Kastrup
  1 sibling, 0 replies; 26+ messages in thread
From: David Kastrup @ 2006-04-12 12:12 UTC (permalink / raw)
  Cc: schwab, emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: Andreas Schwab <schwab@suse.de>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 13:14:19 +0200
>>
>>Because the point of a macro is that you can transform the arguments into
>>a new form to be evaluated.
>
> So parameters in macro definition is always not evaluated, but
> left there, and when the macro returns the expansion, Lisp
> interpreter will evaluate them?

Or compile them, if it is currently compiling.  In either case, the
macro is called with unevaluated arguments at the time it is
encountered, and then the Lisp interpreter or compiler afterwards
interprets the results as if they had been written there in the first
place.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 12:05 Macro vs Function Herbert Euler
@ 2006-04-12 12:13 ` David Kastrup
  2006-04-12 12:17   ` Herbert Euler
  0 siblings, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12 12:13 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: David Kastrup <dak@gnu.org>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 13:19:17 +0200
>>
>>Macro arguments are _never_ evaluated.  They are passed quoted
>>into the macro, the macro gets executed, and the return value of
>>the macro gets evaluated.
>
> This verifies my hypothesis.

There is no need to make "hypotheses".  Read the Elisp manual.

(info "(elisp) Macros")

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 12:13 ` David Kastrup
@ 2006-04-12 12:17   ` Herbert Euler
  0 siblings, 0 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 12:17 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 14:13:15 +0200
>
>There is no need to make "hypotheses".  Read the Elisp manual.
>
>(info "(elisp) Macros")

Well, I've read that, but didn't understand it.  (What a stupid man,
hah :-()  Thanks to mailing list, I understand it now after discussion.

Thank all of you.

Sincerely yours,
Guanpeng Xu

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! 
http://search.msn.click-url.com/go/onm00200636ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 12:08                 ` Herbert Euler
@ 2006-04-12 12:19                   ` David Kastrup
  2006-04-12 13:37                     ` Herbert Euler
  0 siblings, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12 12:19 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

> By the way, if I want my Emacs Lisp program able to be successfully
> compiled, shall macros in the program "finite"?  For example, the
> following macro can't be compiled, can it?
>
> (defmacro xgp-casi2-safe-call-iter (func largs)
>  (if largs
>      `(condition-case nil
> 	   (apply ,func (car ,largs))
> 	 (error (xgp-casi2-safe-call-iter ,func (cdr ,largs))))
>    'nil))
>
> Is there some way of making it able to be compiled?

By not mixing up what you do when?

(defmacro xgp-casi2-safe-call-iter (func largs)
 (if largs
     `(condition-case nil
	   (apply ,func ,(car largs))
	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))
   'nil))


-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 12:19                   ` David Kastrup
@ 2006-04-12 13:37                     ` Herbert Euler
  2006-04-12 13:52                       ` David Kastrup
  0 siblings, 1 reply; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 13:37 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 14:19:24 +0200
>
>By not mixing up what you do when?
>
>(defmacro xgp-casi2-safe-call-iter (func largs)
>  (if largs
>      `(condition-case nil
>	   (apply ,func ,(car largs))
>	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))
>    'nil))

Hmm...  The correct one should be:

(defmacro xgp-casi2-safe-call-iter (func largs)
  (if largs
      `(condition-case nil
           (apply ,func ,((lambda (a) `(quote ,(car a))) largs))
         (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))
    'nil))

Only ,(car largs) is not sufficient.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 13:37                     ` Herbert Euler
@ 2006-04-12 13:52                       ` David Kastrup
  2006-04-12 15:13                         ` Herbert Euler
  0 siblings, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12 13:52 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: David Kastrup <dak@gnu.org>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 14:19:24 +0200
>>
>>By not mixing up what you do when?
>>
>>(defmacro xgp-casi2-safe-call-iter (func largs)
>>  (if largs
>>      `(condition-case nil
>>	   (apply ,func ,(car largs))
>>	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))
>>    'nil))
>
> Hmm...  The correct one should be:
>
> (defmacro xgp-casi2-safe-call-iter (func largs)
>  (if largs
>      `(condition-case nil
>           (apply ,func ,((lambda (a) `(quote ,(car a))) largs))
>         (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))
>    'nil))
>
> Only ,(car largs) is not sufficient.

Oh good grief.  What _are_ you trying to accomplish?  What you wrote
above is a complete and utter contorted mess.

So please come up with an actual _example_ of what you intend the
stuff to do.  I'd be surprised if my proposal would not be closer to
doing the intended thing than what you wrote.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 13:52                       ` David Kastrup
@ 2006-04-12 15:13                         ` Herbert Euler
  2006-04-12 15:26                           ` Stuart D. Herring
  2006-04-12 15:29                           ` David Kastrup
  0 siblings, 2 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 15:13 UTC (permalink / raw)
  Cc: emacs-devel

What I'm trying to do can be found at

    http://lists.gnu.org/archive/html/help-gnu-emacs/2006-04/msg00185.html

The reason why ,(car largs) is not sufficient is that 'apply' requires
a list as the last argument. While I call 'xgp-casi2-safe-call-iter' with

    (xgp-casi2-safe-call-iter '+ ((1 2 a) (3 4)))

(car largs) is (1 2 a), which it's not a list when evaluating

    (apply '+ (1 2 a))

(I've modified xgp-casi2-safe-call but it's not accessible now.  I'll post 
it when
it's accessible.)

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 15:13                         ` Herbert Euler
@ 2006-04-12 15:26                           ` Stuart D. Herring
  2006-04-12 15:54                             ` Herbert Euler
  2006-04-12 15:29                           ` David Kastrup
  1 sibling, 1 reply; 26+ messages in thread
From: Stuart D. Herring @ 2006-04-12 15:26 UTC (permalink / raw)
  Cc: emacs-devel

> The reason why ,(car largs) is not sufficient is that 'apply' requires
> a list as the last argument. While I call 'xgp-casi2-safe-call-iter' with
>
>     (xgp-casi2-safe-call-iter '+ ((1 2 a) (3 4)))
>
> (car largs) is (1 2 a), which it's not a list when evaluating
>
>     (apply '+ (1 2 a))

This much you can solve simply by using ',(car largs).  In general, in a `:

x              "use literally and eval later"
'x             "never eval at all"
,x             "use symbolically and eval later"
(eval x)       "use literally and eval twice later"
,(eval x)      "evaluate now and later"
',(eval x)     "evaluate now only"
',x            "use symbolically and don't eval later"

(...where 'now' is macro expansion time and 'later' is interpretation
time.)  You want the last one, since largs is a macro argument (use
symbolically) but you don't want the resulting list evaluated.  (I hope I
got all those meanings right!)

Davis

-- 
This product is sold by volume, not by mass.  If it appears too dense or
too sparse, it is because mass-energy conversion has occurred during
shipping.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 15:13                         ` Herbert Euler
  2006-04-12 15:26                           ` Stuart D. Herring
@ 2006-04-12 15:29                           ` David Kastrup
  2006-04-12 15:56                             ` Herbert Euler
  1 sibling, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12 15:29 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

> What I'm trying to do can be found at
>
>    http://lists.gnu.org/archive/html/help-gnu-emacs/2006-04/msg00185.html
>
> The reason why ,(car largs) is not sufficient is that 'apply' requires
> a list as the last argument. While I call 'xgp-casi2-safe-call-iter' with
>
>    (xgp-casi2-safe-call-iter '+ ((1 2 a) (3 4)))
>
> (car largs) is (1 2 a), which it's not a list when evaluating
>
>    (apply '+ (1 2 a))
>
> (I've modified xgp-casi2-safe-call but it's not accessible now.  I'll
> post it when
> it's accessible.)

Why are you bothering with "apply" in the first place?  It does not
look like you have any need for it.  Just write

(defmacro xgp-casi2-safe-call-iter (func largs)
 (and largs
     `(condition-case nil
	   (,func ,@(car largs))
	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))))

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 15:26                           ` Stuart D. Herring
@ 2006-04-12 15:54                             ` Herbert Euler
  0 siblings, 0 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 15:54 UTC (permalink / raw)
  Cc: emacs-devel

>From: "Stuart D. Herring" <herring@lanl.gov>
>Reply-To: herring@lanl.gov
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 08:26:05 -0700 (PDT)
>
>This much you can solve simply by using ',(car largs).  In general, in a `:
>
>x              "use literally and eval later"
>'x             "never eval at all"
>,x             "use symbolically and eval later"
>(eval x)       "use literally and eval twice later"
>,(eval x)      "evaluate now and later"
>',(eval x)     "evaluate now only"
>',x            "use symbolically and don't eval later"
>
>(...where 'now' is macro expansion time and 'later' is interpretation
>time.)  You want the last one, since largs is a macro argument (use
>symbolically) but you don't want the resulting list evaluated.  (I hope I
>got all those meanings right!)

Oh yes!  I'm still a bit confused by the time when I wrote that definition.
Yes, ',x => (quote (\, x)) is what I need.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 15:29                           ` David Kastrup
@ 2006-04-12 15:56                             ` Herbert Euler
  2006-04-12 16:14                               ` Herbert Euler
  2006-04-12 16:59                               ` David Kastrup
  0 siblings, 2 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 15:56 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 17:29:40 +0200
>
>Why are you bothering with "apply" in the first place?  It does not
>look like you have any need for it.  Just write

Because func is supposed to be a variable set by, for instance,

    (setq f '+)

And '+ is just for testing purpose.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 15:56                             ` Herbert Euler
@ 2006-04-12 16:14                               ` Herbert Euler
  2006-04-12 16:59                               ` David Kastrup
  1 sibling, 0 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-12 16:14 UTC (permalink / raw)


Now I think I've understood how macro works.  A macro is
equivalent to a "function" described as follows:

1. This function has the same body as the macro, and when
   calling this function, substitute all arguments with quoted
   ones.
2. Then the body of the function is evaluated, and a s-exp
   is returned.  (`, \, and ,@ are treated as normal functions
   here.)  All parameters are normal variables that have value
   of the orignal arguments.
3. Next, evaluated this s-exp.

This will cause the same effect as the macro.

Thank you, David Kastrup, Andreas Schwab, and Stuart D.
Herring.  This is I think the biggest step I made after studying
(Emacs) Lisp.

Regards,
Guanpeng Xu

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! 
http://search.msn.click-url.com/go/onm00200636ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 15:56                             ` Herbert Euler
  2006-04-12 16:14                               ` Herbert Euler
@ 2006-04-12 16:59                               ` David Kastrup
  2006-04-13  2:17                                 ` Herbert Euler
  1 sibling, 1 reply; 26+ messages in thread
From: David Kastrup @ 2006-04-12 16:59 UTC (permalink / raw)
  Cc: emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:

>>From: David Kastrup <dak@gnu.org>
>>To: "Herbert Euler" <herberteuler@hotmail.com>
>>CC: emacs-devel@gnu.org
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 17:29:40 +0200
>>
>>Why are you bothering with "apply" in the first place?  It does not
>>look like you have any need for it.  Just write
>
> Because func is supposed to be a variable set by, for instance,
>
>    (setq f '+)
>
> And '+ is just for testing purpose.

Could you _please_ quote the relevant definitions when answering?  It
causes a lot of extra work if I have to go constantly to previous
parts of the thread to dig them out.  I still don't see why you can't
just write

(defmacro xgp-casi2-safe-call-iter (func largs)
 (and largs
     `(condition-case nil
	   (funcall ,func ,@(car largs))
	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))))

Or, if you really insist on "apply",

(defmacro xgp-casi2-safe-call-iter (func largs)
 (and largs
     `(condition-case nil
	   (apply ,func (list ,@(car largs)))
	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))))


-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Macro vs Function
  2006-04-12 16:59                               ` David Kastrup
@ 2006-04-13  2:17                                 ` Herbert Euler
  0 siblings, 0 replies; 26+ messages in thread
From: Herbert Euler @ 2006-04-13  2:17 UTC (permalink / raw)
  Cc: emacs-devel

>From: David Kastrup <dak@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: emacs-devel@gnu.org
>Subject: Re: Macro vs Function
>Date: Wed, 12 Apr 2006 18:59:45 +0200
>
>"Herbert Euler" <herberteuler@hotmail.com> writes:
>
> >>From: David Kastrup <dak@gnu.org>
> >>To: "Herbert Euler" <herberteuler@hotmail.com>
> >>CC: emacs-devel@gnu.org
> >>Subject: Re: Macro vs Function
> >>Date: Wed, 12 Apr 2006 17:29:40 +0200
> >>
> >>Why are you bothering with "apply" in the first place?  It does not
> >>look like you have any need for it.  Just write
> >
> > Because func is supposed to be a variable set by, for instance,
> >
> >    (setq f '+)
> >
> > And '+ is just for testing purpose.
>
>Could you _please_ quote the relevant definitions when answering?  It
>causes a lot of extra work if I have to go constantly to previous
>parts of the thread to dig them out.

I'm sorry.

>I still don't see why you can't just write
>
>(defmacro xgp-casi2-safe-call-iter (func largs)
>  (and largs
>      `(condition-case nil
>	   (funcall ,func ,@(car largs))
>	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))))
>
>Or, if you really insist on "apply",
>
>(defmacro xgp-casi2-safe-call-iter (func largs)
>  (and largs
>      `(condition-case nil
>	   (apply ,func (list ,@(car largs)))
>	 (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))))

Yes, these two both work.  When I wrote the "complete
and utter contorted mess":

>(defmacro xgp-casi2-safe-call-iter (func largs)
>   (if largs
>       `(condition-case nil
>            (apply ,func ,((lambda (a) `(quote ,(car a))) largs))
>          (error (xgp-casi2-safe-call-iter ,func ,(cdr largs))))
>     'nil))

I didn't understand the mechanism of macros.  If I'm writing
them now, I too will write simple ones like what Stuart D.
Herring and you write.

Thank you very much, for making me understand how macro
works.

Sincerely yours,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2006-04-13  2:17 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-12 12:05 Macro vs Function Herbert Euler
2006-04-12 12:13 ` David Kastrup
2006-04-12 12:17   ` Herbert Euler
  -- strict thread matches above, loose matches on Subject: below --
2006-04-12  8:22 Herbert Euler
2006-04-12  9:07 ` David Kastrup
2006-04-12  9:17   ` Herbert Euler
2006-04-12  9:24     ` David Kastrup
2006-04-12  9:39       ` Herbert Euler
2006-04-12  9:53         ` David Kastrup
2006-04-12 10:39           ` Herbert Euler
2006-04-12 11:14             ` Andreas Schwab
2006-04-12 11:56               ` Herbert Euler
2006-04-12 12:08                 ` Herbert Euler
2006-04-12 12:19                   ` David Kastrup
2006-04-12 13:37                     ` Herbert Euler
2006-04-12 13:52                       ` David Kastrup
2006-04-12 15:13                         ` Herbert Euler
2006-04-12 15:26                           ` Stuart D. Herring
2006-04-12 15:54                             ` Herbert Euler
2006-04-12 15:29                           ` David Kastrup
2006-04-12 15:56                             ` Herbert Euler
2006-04-12 16:14                               ` Herbert Euler
2006-04-12 16:59                               ` David Kastrup
2006-04-13  2:17                                 ` Herbert Euler
2006-04-12 12:12                 ` David Kastrup
2006-04-12 11:19             ` David Kastrup

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).