From: Marco Antoniotti
Subject: Another FAQ (my brain isn't working)
Date: 
Message-ID: <cSHga.133$oj7.12094@typhoon.nyu.edu>
Hi

Ok.  I know this is a FAQ.  Anyway, I have a macro (it's some LW stuff I 
don't have a way to reproduce easily - ordinarily I would just manually 
macroexpand) that essentially has the following form

(defmacro plus (&rest args) `(+ ,@args))

Now I have a list

(defvar *numbers* (list 1 2 3))

You know the question.  How do I do the equivalent of

(apply #'+ *numbers*)

with PLUS?

Any help appreciated.

Thanks


--
Marco Antoniotti

From: Raymond Wiker
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <864r5ohbd8.fsf@raw.grenland.fast.no>
Marco Antoniotti <·······@cs.nyu.edu> writes:

> Hi
> 
> Ok.  I know this is a FAQ.  Anyway, I have a macro (it's some LW stuff
> I don't have a way to reproduce easily - ordinarily I would just
> manually macroexpand) that essentially has the following form
> 
> 
> (defmacro plus (&rest args) `(+ ,@args))
> 
> Now I have a list
> 
> (defvar *numbers* (list 1 2 3))
> 
> You know the question.  How do I do the equivalent of
> 
> (apply #'+ *numbers*)
> 
> with PLUS?

(eval (cons 'plus *numbers*))

?

-- 
Raymond Wiker                        Mail:  ·············@fast.no
Senior Software Engineer             Web:   http://www.fast.no/
Fast Search & Transfer ASA           Phone: +47 23 01 11 60
P.O. Box 1677 Vika                   Fax:   +47 35 54 87 99
NO-0120 Oslo, NORWAY                 Mob:   +47 48 01 11 60

Try FAST Search: http://alltheweb.com/
From: Marco Antoniotti
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <zhIga.134$oj7.12898@typhoon.nyu.edu>
Raymond Wiker wrote:

> Marco Antoniotti  writes:
>
>
> >Hi
> >
> >Ok.  I know this is a FAQ.  Anyway, I have a macro (it's some LW stuff
> >I don't have a way to reproduce easily - ordinarily I would just
> >manually macroexpand) that essentially has the following form
> >
> >
> >(defmacro plus (&rest args) `(+ ,@args))
> >
> >Now I have a list
> >
> >(defvar *numbers* (list 1 2 3))
> >
> >You know the question.  How do I do the equivalent of
> >
> >(apply #'+ *numbers*)
> >
> >with PLUS?
>
>
> (eval (cons 'plus *numbers*))

Almost.  (1) I'd like to avoid EVAL, (2) the evaluation does not happen 
in an empty environment.

Cheers

--
Marco Antoniotti
From: Erann Gat
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <gat-2703031216440001@k-137-79-50-101.jpl.nasa.gov>
In article <···················@typhoon.nyu.edu>, ·······@cs.nyu.edu wrote:

> Raymond Wiker wrote:
> 
> > Marco Antoniotti  writes:
> >
> >
> > >Hi
> > >
> > >Ok.  I know this is a FAQ.  Anyway, I have a macro (it's some LW stuff
> > >I don't have a way to reproduce easily - ordinarily I would just
> > >manually macroexpand) that essentially has the following form
> > >
> > >
> > >(defmacro plus (&rest args) `(+ ,@args))
> > >
> > >Now I have a list
> > >
> > >(defvar *numbers* (list 1 2 3))
> > >
> > >You know the question.  How do I do the equivalent of
> > >
> > >(apply #'+ *numbers*)
> > >
> > >with PLUS?
> >
> >
> > (eval (cons 'plus *numbers*))
> 
> Almost.  (1) I'd like to avoid EVAL,

(apply (car (macroexpand (cons 'plus *numbers*))) *numbers*)

> (2) the evaluation does not happen in an empty environment.

Then you should extend your example to make it apparent why this matters
(specifically, which environment is not empty).

E.

-- 
The opinions expressed here are my own and do not necessarily
reflect the views of JPL or NASA.
From: Lars Brinkhoff
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <85brzvr86e.fsf@junk.nocrew.org>
···@jpl.nasa.gov (Erann Gat) writes:
> ·······@cs.nyu.edu wrote:
> > Raymond Wiker wrote:
> > > Marco Antoniotti  writes:
> > > > I know this is a FAQ.  Anyway, I have a macro that essentially
> > > > has the following form
> > > >   (defmacro plus (&rest args) `(+ ,@args))
> > > > Now I have a list
> > > >   (defvar *numbers* (list 1 2 3))
> > > > You know the question.  How do I do the equivalent of
> > > >   (apply #'+ *numbers*)
> > > > with PLUS?
> > > (eval (cons 'plus *numbers*))
> > Almost.  (1) I'd like to avoid EVAL,
> (apply (car (macroexpand (cons 'plus *numbers*))) *numbers*)

Expanding on that (ha ha), how about something like

  (defmacro apply-macro (macro &rest args)
    (let ((form (macroexpand (cons macro args))))
      ;; Perhaps check that (first form) is a valid function.
      `(apply #',(first form) ,@(rest form)))

?  (Call apply-macro without ' or #' before the macro argument.)

-- 
Lars Brinkhoff,         Services for Unix, Linux, GCC, PDP-10, HTTP
Brinkhoff Consulting    http://www.brinkhoff.se/
From: Gabe Garza
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <87llz05kn2.fsf@ix.netcom.com>
Marco Antoniotti <·······@cs.nyu.edu> writes:

> 
> (defmacro plus (&rest args) `(+ ,@args))
> 
> Now I have a list
> 
> (defvar *numbers* (list 1 2 3))
> 
> You know the question.  How do I do the equivalent of
> 
> (apply #'+ *numbers*)
> 
> with PLUS?
> 

Are you looking for compiler macros? E.g.:

(define-compiler-macro plus (&rest args)
  `(+ ,@args))

(defun plus (&rest args)
  (apply #'+ args))

Now you have (basically) a macro and an APPLY'able function with the
same name....

Gabe Garza 
From: Rob Warnock
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <gfKdnWZ-ZcOwpBmjXTWc-w@speakeasy.net>
Marco Antoniotti  <·······@cs.nyu.edu> wrote:
+---------------
|   (defmacro plus (&rest args) `(+ ,@args))
| Now I have a list
|   (defvar *numbers* (list 1 2 3))
| You know the question.  How do I do the equivalent of
|   (apply #'+ *numbers*)
+---------------

At the cost of losing the efficiency of the macro (assuming
the actual case is much more complex than you've given here),
you could always do:

    (reduce #'(lambda (x y) (plus x y)) *numbers*)

Which brings up a side point: Given that I use the Unix command
"xargs" with some frequency (expecially with the "-n" option),
it's often been frustrating to me that REDUCE doesn't accept a
keyword parameter specifying the maximum number of arguments that
can be passed to its first parameter per call, defaulting to 2.
Then you could portably chunk through *huge* reductions N args
at a time. In the limit (pardon the pun) even saying:

    (reduce #'+ *numbers* :max-args call-arguments-limit)

[Useful for Lisps with values of "call-arguments-limit" close to
the minimums required by the standard.]

Was this considered at all when REDUCE was standardized?


-Rob

-----
Rob Warnock, PP-ASEL-IA		<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Thomas F. Burdick
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <xcvisu35mtq.fsf@apocalypse.OCF.Berkeley.EDU>
····@rpw3.org (Rob Warnock) writes:

> Marco Antoniotti  <·······@cs.nyu.edu> wrote:
> +---------------
> |   (defmacro plus (&rest args) `(+ ,@args))
> | Now I have a list
> |   (defvar *numbers* (list 1 2 3))
> | You know the question.  How do I do the equivalent of
> |   (apply #'+ *numbers*)
> +---------------
> 
> Which brings up a side point: Given that I use the Unix command
> "xargs" with some frequency (expecially with the "-n" option),

Heh, in my portable file of random hacks I carry around with my, I
have a definition for a function named ORG.NO-CARRIER.UTILS:XARGS,
which is pretty much REDUCE reimplemented to be more like the Unix
xargs command.

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Kaz Kylheku
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <cf333042.0303280810.40460de4@posting.google.com>
Marco Antoniotti <·······@cs.nyu.edu> wrote in message news:<···················@typhoon.nyu.edu>...
> (defmacro plus (&rest args) `(+ ,@args))
> 
> Now I have a list
> 
> (defvar *numbers* (list 1 2 3))
> 
> You know the question.  How do I do the equivalent of
> 
> (apply #'+ *numbers*)
> 
> with PLUS?

Like this:

  (declaim (inline plus))
  (defun plus (&rest args) (apply #'+ args))

  (apply #'plus *numbers*)

Don't use macros for simple inlining jobs where no syntax
transformation is taking place! :)

Also, because PLUS is now a function, you can use
DEFINE-COMPILER-MACRO to optimize it.
From: Marco Antoniotti
Subject: Re: Another FAQ (my brain isn't working)
Date: 
Message-ID: <T46ha.141$oj7.13634@typhoon.nyu.edu>
Kaz Kylheku wrote:

> Marco Antoniotti  wrote in message news:...
>
> >(defmacro plus (&rest args) `(+ ,@args))
> >
> >Now I have a list
> >
> >(defvar *numbers* (list 1 2 3))
> >
> >You know the question.  How do I do the equivalent of
> >
> >(apply #'+ *numbers*)
> >
> >with PLUS?
>
>
> Like this:
>
>   (declaim (inline plus))
>   (defun plus (&rest args) (apply #'+ args))
>
>   (apply #'plus *numbers*)
>
> Don't use macros for simple inlining jobs where no syntax
> transformation is taking place! :)
>
> Also, because PLUS is now a function, you can use
> DEFINE-COMPILER-MACRO to optimize it.


I don't want to be rude, but, Kaz, have you read the fine print in my OP?

I have to deal with LW COM:CALL-DISPATCH-METHOD (a macro) which has 
signature

	(a b c) &rest args

I cannot even easily access its macroexpansion as it seems dependent on 
the state of the current set of running COM objects.

Cheers

--
Marco Antoniotti