From: Vladimir Zolotykh
Subject: calling function at compile time
Date: 
Message-ID: <3BCB1A0E.639693F5@eurocom.od.ua>
Suppose I have two functions: F and G. F calls G once with arguments
related to the meaning of F. I'd like not to call G each time F
called.  Is it possible to have something like "Call G when F being
compiled and always use the returned result when F will actually be
called" ? I wish not exactly this above, only not to call G many
times. All G's arguments are numbers and/or strings.

-- 
Vladimir Zolotykh                         ······@eurocom.od.ua

From: Coby Beck
Subject: Re: calling function at compile time
Date: 
Message-ID: <XdFy7.371798$8c3.67801800@typhoon.tampabay.rr.com>
"Vladimir Zolotykh" <······@eurocom.od.ua> wrote in message
······················@eurocom.od.ua...
> Suppose I have two functions: F and G. F calls G once with arguments
> related to the meaning of F. I'd like not to call G each time F
> called.  Is it possible to have something like "Call G when F being
> compiled and always use the returned result when F will actually be
> called" ? I wish not exactly this above, only not to call G many
> times. All G's arguments are numbers and/or strings.
>

Sounds like G is a macro.

Coby
--
(remove #\space "coby . beck @ opentechgroup . com")
From: Tim Bradshaw
Subject: Re: calling function at compile time
Date: 
Message-ID: <nkj3d4kdcim.fsf@davros.tardis.ed.ac.uk>
Vladimir Zolotykh <······@eurocom.od.ua> writes:

> Suppose I have two functions: F and G. F calls G once with arguments
> related to the meaning of F. I'd like not to call G each time F
> called.  Is it possible to have something like "Call G when F being
> compiled and always use the returned result when F will actually be
> called" ? I wish not exactly this above, only not to call G many
> times. All G's arguments are numbers and/or strings.
> 

There are several ways you could do this.  If G is defined when F is loaded
then you could use (load-time-value (G ...)).  Note that you *can't* rely
on the lexical environment of F if you do this, but if you want something
to happen at compile time you can't rely on that anyway.  If you do 
need to call G `properly' - when F is actually called, but only once, 
you could do an ad-hoc memoizing trick:

        (let ((gr (load-time-value (cons nil nil))))
          (or (car gr)
              (setf (car gr) (g ...))))

There are other things you could do, of course.

--tim
From: Vladimir Zolotykh
Subject: Re: calling function at compile time
Date: 
Message-ID: <3BCB2349.3B606CD6@eurocom.od.ua>
Vladimir Zolotykh wrote:
> 
> ....I wish not exactly this above, only not to call G many
> times. All G's arguments are numbers and/or strings.

I think #.(G...) give me what I look for. At load time, though.
Sorry. 
--
Vladimir Zolotykh                         ······@eurocom.od.ua
From: Janis Dzerins
Subject: Re: calling function at compile time
Date: 
Message-ID: <87669gov5t.fsf@asaka.latnet.lv>
Vladimir Zolotykh <······@eurocom.od.ua> writes:

> Vladimir Zolotykh wrote:
> > 
> > ....I wish not exactly this above, only not to call G many
> > times. All G's arguments are numbers and/or strings.
> 
> I think #.(G...) give me what I look for. At load time, though.

That is the read time.

-- 
Janis Dzerins

  Eat shit -- billions of flies can't be wrong.
From: Tim Bradshaw
Subject: Re: calling function at compile time
Date: 
Message-ID: <nkjy9mbq1gk.fsf@omega.tardis.ed.ac.uk>
Vladimir Zolotykh <······@eurocom.od.ua> writes:


> I think #.(G...) give me what I look for. At load time, though.
> Sorry. 

No, this is at read time which is much earlier than load time, and is more 
likely to be just before compile time & macro expansion time.

--tim
From: Kent M Pitman
Subject: Re: calling function at compile time
Date: 
Message-ID: <sfwvghfvmhp.fsf@world.std.com>
Tim Bradshaw <···@tfeb.org> writes:

> Vladimir Zolotykh <······@eurocom.od.ua> writes:
> 
> 
> > I think #.(G...) give me what I look for. At load time, though.
> > Sorry. 
> 
> No, this is at read time which is much earlier than load time, and is more 
> likely to be just before compile time & macro expansion time.

(LOAD-TIME-VALUE (G ...)) works to do something at load time.

Understanding #.(...) means thinking about the various times the reader
might conceivably be called. It's in the runtime environment for uncompiled
code.  It's in the compile-time environment for compiled code.  It's in
someone's randomly unprepared environment for code-walking tools that are
just calling read on your file as if it contained only data.  So use it
sparingly.

For constants to be folded at compile-time, DEFCONSTANT is often the way
to go, and then use a reference to the named constant inline.  I don't think
there's an anonymous way to do this, unfortunately.