From: zoav1602
Subject: declaring a function to be special
Date: 
Message-ID: <5ac22e6c-9f14-4ec8-99c9-df95d1a4a137@j12g2000vbl.googlegroups.com>
Hello,
What is the corect way to call a function which will be defined later
using flet/labels? Something like this:

(defun f() (print '(f is called)) (g))
(locally ((g () (print '(g is called)))) (f))

This code is not correct, as I discovered. The only solution I see so
far is using special variables, and defining f as (funcall *g*). Are
there any other solutions?

From: Barry Margolin
Subject: Re: declaring a function to be special
Date: 
Message-ID: <barmar-F701A2.03561922062009@news.eternal-september.org>
In article 
<····································@j12g2000vbl.googlegroups.com>,
 zoav1602 <········@gmail.com> wrote:

> Hello,
> What is the corect way to call a function which will be defined later
> using flet/labels? Something like this:
> 
> (defun f() (print '(f is called)) (g))
> (locally ((g () (print '(g is called)))) (f))
> 
> This code is not correct, as I discovered. The only solution I see so
> far is using special variables, and defining f as (funcall *g*). Are
> there any other solutions?

FLET and LABELS only create lexical bindings, there's no dynamic binding 
mechanism for function names.

Why do you want to do this, maybe there's a better way?

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: zoav1602
Subject: Re: declaring a function to be special
Date: 
Message-ID: <e795b7c2-1657-41d5-8482-93ad8351bdd8@n30g2000vba.googlegroups.com>
> Why do you want to do this, maybe there's a better way?
>

In a simple settings, imagine I write a numerical integrator routine
which works for arbitraty integrand, say f. I can pass #'f as a
variable to funcall it later. Of course the other solution would be to
define f each time I want to integrate it and hardcode the name inside
the integrator.
On the other hand if I want to control the accuracy, I can have a
variable either &optional or special. When it's special, it can be
used in other routines as well without explicit passing it. This is
what I want: to be able to switch between several existing f's without
passing them explicitly everywhere they are called.
From: Tamas K Papp
Subject: Re: declaring a function to be special
Date: 
Message-ID: <7a90igF1uar62U1@mid.individual.net>
On Mon, 22 Jun 2009 01:39:26 -0700, zoav1602 wrote:

>> Why do you want to do this, maybe there's a better way?
>>
>>
> In a simple settings, imagine I write a numerical integrator routine
> which works for arbitraty integrand, say f. I can pass #'f as a variable
> to funcall it later. Of course the other solution would be to define f
> each time I want to integrate it and hardcode the name inside the
> integrator.

The "other solution" would be extremely bad style.

> On the other hand if I want to control the accuracy, I can have a
> variable either &optional or special. When it's special, it can be used
> in other routines as well without explicit passing it. This is what I
> want: to be able to switch between several existing f's without passing
> them explicitly everywhere they are called.

You can make your variable optional, and have it default to a special
variable.  Eg

(defparameter *integrator-accuracy* 1d-5)

(defun integrate-with-my-favorite-method (f a b &key (accuracy *integrator-accuracy*))
  ...)

HTH,

Tamas
From: Kaz Kylheku
Subject: Re: declaring a function to be special
Date: 
Message-ID: <20090704084640.537@gmail.com>
On 2009-06-22, zoav1602 <········@gmail.com> wrote:
> Hello,
> What is the corect way to call a function which will be defined later
> using flet/labels? Something like this:
>
> (defun f() (print '(f is called)) (g))
> (locally ((g () (print '(g is called)))) (f))
>
> This code is not correct, as I discovered. The only solution I see so
> far is using special variables, and defining f as (funcall *g*). Are
> there any other solutions?

Common Lisp doesn't have dynamically scoped functions out of the box.

You can use dynamic variable bindings to set it up, like your solution,
but some macros can hide the plumbing.

Here is a paper which explores the subject:

http://p-cos.net/documents/dynfun.pdf

Some source code given at the very end.
From: Pascal J. Bourguignon
Subject: Re: declaring a function to be special
Date: 
Message-ID: <8763eo4s4c.fsf@galatea.local>
zoav1602 <········@gmail.com> writes:
Subject: Re: declaring a function to be special

> Hello,
> What is the corect way to call a function which will be defined later
> using flet/labels? Something like this:
>
> (defun f() (print '(f is called)) (g))
> (locally ((g () (print '(g is called)))) (f))
>
> This code is not correct, as I discovered. The only solution I see so
> far is using special variables, and defining f as (funcall *g*). Are
> there any other solutions?

By default, functions are "special".

(defun f () 
   (print '(f is called))  
   (g))

(defun g ()
  (print '(g is called))
  (when (zerop (random 2)) ; stop condition!
     (f)))


flet and labels declare lexical functions, you can only call them from
the lexical scope of those operators.

-- 
__Pascal Bourguignon__
From: Pascal Costanza
Subject: Re: declaring a function to be special
Date: 
Message-ID: <7a927kF1tudd0U1@mid.individual.net>
zoav1602 wrote:
> Hello,
> What is the corect way to call a function which will be defined later
> using flet/labels? Something like this:
> 
> (defun f() (print '(f is called)) (g))
> (locally ((g () (print '(g is called)))) (f))
> 
> This code is not correct, as I discovered. The only solution I see so
> far is using special variables, and defining f as (funcall *g*). Are
> there any other solutions?

Yes, that's the only solution.

See also http://p-cos.net/documents/dynfun.pdf and 
http://p-cos.net/documents/contextl-overview.pdf


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/