From: Matthew D Swank
Subject: Common Lisp flet
Date: 
Message-ID: <pan.2005.09.24.09.02.30.26993@c.net>
Are local (lexically scoped) function values not setf-able?

For instance I can do:

(let ((x 3))
  (setf x 7)
  x)
--> 7

but not:

(flet ((foo () 3))
  (setf (function 'foo) (function (lambda () 7)))
  (foo))

--this generates an error

or the following (it affects the top level binding of 'foo instead of the
local one):

(flet ((foo () 3))
  (setf (symbol-function 'foo) (function (lambda () 7)))
  (foo))
--> 3

(foo)

--> 7

I realize this makes no (well, not much) practical difference, but I did
find it surprising.

Matt

-- 
"You do not really understand something unless you can
 explain it to your grandmother." — Albert Einstein.

From: Rob Warnock
Subject: Re: Common Lisp flet
Date: 
Message-ID: <x8adnakIx9uosqjeRVn-ug@speakeasy.net>
Matthew D Swank  <·······································@c.net> wrote:
+---------------
| Are local (lexically scoped) function values not setf-able?
+---------------

No, they're not. The bindings created by FLET & LABELS sre not mutable.

However, depending on what you're *really* trying to do,
you can either:

1. Nest (shadow) lexical functions:

     > (flet ((foo () 3))
	 (flet ((foo () 7))
	   (foo)))

     7
     > 

2. Store the functions in mutable variables, and use FUNCALL on them:

    > (let ((foo (lambda () 3)))
	(print (funcall foo))
	(setf foo (lambda () 7))
	(print (funcall foo))
	(values))

     3
     7
     > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Matthew D Swank
Subject: Re: Common Lisp flet
Date: 
Message-ID: <pan.2005.09.24.13.04.01.734658@c.net>
On Sat, 24 Sep 2005 05:49:25 -0500, Rob Warnock wrote:


> However, depending on what you're *really* trying to do,
> you can either:
> 
> 1. Nest (shadow) lexical functions:
> 
>      > (flet ((foo () 3))
> 	 (flet ((foo () 7))
> 	   (foo)))
> 
>      7
>      > 
> 
> 2. Store the functions in mutable variables, and use FUNCALL on them:
> 
>     > (let ((foo (lambda () 3)))
> 	(print (funcall foo))
> 	(setf foo (lambda () 7))
> 	(print (funcall foo))
> 	(values))
> 
>      3
>      7
>      > 
> 
> 


Yes, both of which I have done.  Mostly, I was trying to see if it was
possible to implement 'labels' in terms of 'flet', but they are both
primitive.

Matt

-- 
"You do not really understand something unless you can
 explain it to your grandmother." — Albert Einstein.
From: Edi Weitz
Subject: Re: Common Lisp flet
Date: 
Message-ID: <ur7bev816.fsf@agharta.de>
On Sat, 24 Sep 2005 08:04:10 -0500, Matthew D Swank <·······································@c.net> wrote:

> Mostly, I was trying to see if it was possible to implement 'labels'
> in terms of 'flet', but they are both primitive.

  <http://home.pipeline.com/~hbaker1/MetaCircular.html>

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Matthew D Swank
Subject: Re: Common Lisp flet
Date: 
Message-ID: <pan.2005.09.24.17.28.32.593188@c.net>
On Sat, 24 Sep 2005 16:45:41 +0200, Edi Weitz wrote:

> On Sat, 24 Sep 2005 08:04:10 -0500, Matthew D Swank <·······································@c.net> wrote:
> 
>> Mostly, I was trying to see if it was possible to implement 'labels'
>> in terms of 'flet', but they are both primitive.
> 
>   <http://home.pipeline.com/~hbaker1/MetaCircular.html>
> 
> Cheers,
> Edi.

The Y combinator! I had thought if that, but my little brain couldn't
quite figure out how to handle the general case of several mutually
recursive functions.

Matt

-- 
"You do not really understand something unless you can
 explain it to your grandmother." — Albert Einstein.