From: Blackguester
Subject: Ask a question about internal function
Date: 
Message-ID: <65e0dad1-491b-4867-93a4-ed5c02596675@b5g2000pri.googlegroups.com>
After I define these below:

(defun f1 ()
  (defun f ()
    1)
  (put 'f1 'f (lambda () (f)))
)

(defun f2 ()
  (defun f ()
    2)
  (put 'f2 'f (lambda () (f)))
)

(f1)
(f2)

(funcall (geth 'f1 'f))
(funcall (geth 'f2 'f))

where put and geth is implement by hash table
(defvar *op-table* (make-hash-table :test 'equal))

(defun put (op type proc)
  (setf (gethash (list type op) *op-table*)
	proc))

(defun geth (op type)
  (gethash (list type op) *op-table*))

when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
suppose to get 1 and 2 respectively, but it doesn't.
I get the answer depend on the execute sequence of (f1) (f2),
that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
'f)) and  (funcall (geth 'f2 'f)),
if execute (f2) first, I get 2 both.
It seems the f define in both function use a same storage, it refreshs
when (f1) or$B!!(B(f2) executed.
So I confused,  it should use different storages when internal
functions defined, even though they use the same name.
Who can explain this?

From: Blackguester
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <54333305-503e-4da6-a7a4-4d9e7bb36478@y18g2000pre.googlegroups.com>
It seems related to lambda, when I use #'f instead of  (lambda ()
(f)), I get the right answer.
But I still want to know why lambda behavior like this.
ps: I test this in both Gnu clisp and cmu lisp

Blackguester wrote:
> After I define these below:
>
> (defun f1 ()
>   (defun f ()
>     1)
>   (put 'f1 'f (lambda () (f)))
> )
>
> (defun f2 ()
>   (defun f ()
>     2)
>   (put 'f2 'f (lambda () (f)))
> )
>
> (f1)
> (f2)
>
> (funcall (geth 'f1 'f))
> (funcall (geth 'f2 'f))
>
> where put and geth is implement by hash table
> (defvar *op-table* (make-hash-table :test 'equal))
>
> (defun put (op type proc)
>   (setf (gethash (list type op) *op-table*)
> 	proc))
>
> (defun geth (op type)
>   (gethash (list type op) *op-table*))
>
> when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
> suppose to get 1 and 2 respectively, but it doesn't.
> I get the answer depend on the execute sequence of (f1) (f2),
> that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
> 'f)) and  (funcall (geth 'f2 'f)),
> if execute (f2) first, I get 2 both.
> It seems the f define in both function use a same storage, it refreshs
> when (f1) or$B!!(B(f2) executed.
> So I confused,  it should use different storages when internal
> functions defined, even though they use the same name.
> Who can explain this?
From: Rainer Joswig
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <joswig-382297.10205221052008@news-europe.giganews.com>
In article 
<····································@b5g2000pri.googlegroups.com>,
 Blackguester <············@gmail.com> wrote:

> After I define these below:
> 
> (defun f1 ()
>   (defun f ()
>     1)
>   (put 'f1 'f (lambda () (f)))
> )
> 
> (defun f2 ()
>   (defun f ()
>     2)
>   (put 'f2 'f (lambda () (f)))
> )
> 
> (f1)
> (f2)
> 
> (funcall (geth 'f1 'f))
> (funcall (geth 'f2 'f))
> 
> where put and geth is implement by hash table
> (defvar *op-table* (make-hash-table :test 'equal))
> 
> (defun put (op type proc)
>   (setf (gethash (list type op) *op-table*)
> 	proc))
> 
> (defun geth (op type)
>   (gethash (list type op) *op-table*))
> 
> when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
> suppose to get 1 and 2 respectively, but it doesn't.
> I get the answer depend on the execute sequence of (f1) (f2),
> that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
> 'f)) and  (funcall (geth 'f2 'f)),
> if execute (f2) first, I get 2 both.
> It seems the f define in both function use a same storage, it refreshs
> when (f1) or$B!!(B(f2) executed.
> So I confused,  it should use different storages when internal
> functions defined, even though they use the same name.
> Who can explain this?

DEFUN is not for defining external functions.
DEFUN defines a function for a global symbol. It is also usually
used as a toplevel function.

Define local functions with LABELS (for recursive local functions) or with FLET.

In your example, you just define one function F. The second call
overwrites the first one ( calling f1 or f2 ).

-- 
http://lispm.dyndns.org/
From: Blackguester
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <64e35ec5-c653-4bd8-b42b-4e3a24d52a52@g16g2000pri.googlegroups.com>
On May 21, 4:20 pm, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@b5g2000pri.googlegroups.com>,
>
>
>
>  Blackguester <············@gmail.com> wrote:
> > After I define these below:
>
> > (defun f1 ()
> >   (defun f ()
> >     1)
> >   (put 'f1 'f (lambda () (f)))
> > )
>
> > (defun f2 ()
> >   (defun f ()
> >     2)
> >   (put 'f2 'f (lambda () (f)))
> > )
>
> > (f1)
> > (f2)
>
> > (funcall (geth 'f1 'f))
> > (funcall (geth 'f2 'f))
>
> > where put and geth is implement by hash table
> > (defvar *op-table* (make-hash-table :test 'equal))
>
> > (defun put (op type proc)
> >   (setf (gethash (list type op) *op-table*)
> >    proc))
>
> > (defun geth (op type)
> >   (gethash (list type op) *op-table*))
>
> > when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
> > suppose to get 1 and 2 respectively, but it doesn't.
> > I get the answer depend on the execute sequence of (f1) (f2),
> > that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
> > 'f)) and  (funcall (geth 'f2 'f)),
> > if execute (f2) first, I get 2 both.
> > It seems the f define in both function use a same storage, it refreshs
> > when (f1) or$B!!(B(f2) executed.
> > So I confused,  it should use different storages when internal
> > functions defined, even though they use the same name.
> > Who can explain this?
>
> DEFUN is not for defining external functions.
> DEFUN defines a function for a global symbol. It is also usually
> used as a toplevel function.
>
> Define local functions with LABELS (for recursive local functions) or with FLET.
>
> In your example, you just define one function F. The second call
> overwrites the first one ( calling f1 or f2 ).
>
> --http://lispm.dyndns.org/

you mean that I should change it like this?
(defun f1 ()
  (labels ((f ()
    1)))
  (put 'f1 'f (lambda () (f)))
(f)
)

(defun f2 ()
  (labels ((f ()
    2)))
  (put 'f2 'f (lambda () (f)))
  (f)
)
From: Rainer Joswig
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <joswig-BB04BB.11083221052008@news-europe.giganews.com>
In article 
<····································@g16g2000pri.googlegroups.com>,
 Blackguester <············@gmail.com> wrote:

> On May 21, 4:20 pm, Rainer Joswig <······@lisp.de> wrote:
> > In article
> > <····································@b5g2000pri.googlegroups.com>,
> >
> >
> >
> >  Blackguester <············@gmail.com> wrote:
> > > After I define these below:
> >
> > > (defun f1 ()
> > >   (defun f ()
> > >     1)
> > >   (put 'f1 'f (lambda () (f)))
> > > )
> >
> > > (defun f2 ()
> > >   (defun f ()
> > >     2)
> > >   (put 'f2 'f (lambda () (f)))
> > > )
> >
> > > (f1)
> > > (f2)
> >
> > > (funcall (geth 'f1 'f))
> > > (funcall (geth 'f2 'f))
> >
> > > where put and geth is implement by hash table
> > > (defvar *op-table* (make-hash-table :test 'equal))
> >
> > > (defun put (op type proc)
> > >   (setf (gethash (list type op) *op-table*)
> > >    proc))
> >
> > > (defun geth (op type)
> > >   (gethash (list type op) *op-table*))
> >
> > > when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
> > > suppose to get 1 and 2 respectively, but it doesn't.
> > > I get the answer depend on the execute sequence of (f1) (f2),
> > > that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
> > > 'f)) and  (funcall (geth 'f2 'f)),
> > > if execute (f2) first, I get 2 both.
> > > It seems the f define in both function use a same storage, it refreshs
> > > when (f1) or$B!!(B(f2) executed.
> > > So I confused,  it should use different storages when internal
> > > functions defined, even though they use the same name.
> > > Who can explain this?
> >
> > DEFUN is not for defining external functions.
> > DEFUN defines a function for a global symbol. It is also usually
> > used as a toplevel function.
> >
> > Define local functions with LABELS (for recursive local functions) or with FLET.
> >
> > In your example, you just define one function F. The second call
> > overwrites the first one ( calling f1 or f2 ).
> >
> > --http://lispm.dyndns.org/
> 
> you mean that I should change it like this?
> (defun f1 ()
>   (labels ((f ()
>     1)))
>   (put 'f1 'f (lambda () (f)))
> (f)
> )

You are formatting the code a bit unusual.

(defun f1 ()
  (labels ((f ()
             1)))
  (put 'f1 'f (lambda () (f)))
  (f))

So, what is wrong with above? Step by step:

LABELS and FLET have a body where the function will be available.
You need to use the function F 'inside' FLET.

(defun f1 ()
  (labels ((f ()
             1))
    (put 'f1 'f (lambda () (f)))
    (f)))

FLET would be enough, since F is not recursive:

(defun f1 ()
  (flet ((f ()
           1))
    (put 'f1 'f (lambda () (f)))
    (f)))

The LAMBDA is not necessary. #'f is the function already.

(defun f1 ()
  (flet ((f ()
           1))
    (put 'f1 'f #'f)
    (f)))

(defun f2 ()
  (flet ((f ()
           2))
    (put 'f2 'f #'f)
    (f)))



CL-USER 4 > (f1)
1

CL-USER 5 > (f2)
2

CL-USER 6 > (funcall (geth 'f1 'f))
1

CL-USER 7 > (funcall (geth 'f2 'f))
2









> 
> (defun f2 ()
>   (labels ((f ()
>     2)))
>   (put 'f2 'f (lambda () (f)))
>   (f)
> )

-- 
http://lispm.dyndns.org/
From: Blackguester
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <c9c99a71-af4e-4774-9172-2693bff1fe0c@q27g2000prf.googlegroups.com>
On May 21, 5:08 pm, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@g16g2000pri.googlegroups.com>,
>
>
>
>  Blackguester <············@gmail.com> wrote:
> > On May 21, 4:20 pm, Rainer Joswig <······@lisp.de> wrote:
> > > In article
> > > <····································@b5g2000pri.googlegroups.com>,
>
> > >  Blackguester <············@gmail.com> wrote:
> > > > After I define these below:
>
> > > > (defun f1 ()
> > > >   (defun f ()
> > > >     1)
> > > >   (put 'f1 'f (lambda () (f)))
> > > > )
>
> > > > (defun f2 ()
> > > >   (defun f ()
> > > >     2)
> > > >   (put 'f2 'f (lambda () (f)))
> > > > )
>
> > > > (f1)
> > > > (f2)
>
> > > > (funcall (geth 'f1 'f))
> > > > (funcall (geth 'f2 'f))
>
> > > > where put and geth is implement by hash table
> > > > (defvar *op-table* (make-hash-table :test 'equal))
>
> > > > (defun put (op type proc)
> > > >   (setf (gethash (list type op) *op-table*)
> > > >    proc))
>
> > > > (defun geth (op type)
> > > >   (gethash (list type op) *op-table*))
>
> > > > when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
> > > > suppose to get 1 and 2 respectively, but it doesn't.
> > > > I get the answer depend on the execute sequence of (f1) (f2),
> > > > that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
> > > > 'f)) and  (funcall (geth 'f2 'f)),
> > > > if execute (f2) first, I get 2 both.
> > > > It seems the f define in both function use a same storage, it refreshs
> > > > when (f1) or$B!!(B(f2) executed.
> > > > So I confused,  it should use different storages when internal
> > > > functions defined, even though they use the same name.
> > > > Who can explain this?
>
> > > DEFUN is not for defining external functions.
> > > DEFUN defines a function for a global symbol. It is also usually
> > > used as a toplevel function.
>
> > > Define local functions with LABELS (for recursive local functions) or with FLET.
>
> > > In your example, you just define one function F. The second call
> > > overwrites the first one ( calling f1 or f2 ).
>
> > > --http://lispm.dyndns.org/
>
> > you mean that I should change it like this?
> > (defun f1 ()
> >   (labels ((f ()
> >     1)))
> >   (put 'f1 'f (lambda () (f)))
> > (f)
> > )
>
> You are formatting the code a bit unusual.
>
> (defun f1 ()
>   (labels ((f ()
>              1)))
>   (put 'f1 'f (lambda () (f)))
>   (f))
>
> So, what is wrong with above? Step by step:
>
> LABELS and FLET have a body where the function will be available.
> You need to use the function F 'inside' FLET.
>
> (defun f1 ()
>   (labels ((f ()
>              1))
>     (put 'f1 'f (lambda () (f)))
>     (f)))
>
> FLET would be enough, since F is not recursive:
>
> (defun f1 ()
>   (flet ((f ()
>            1))
>     (put 'f1 'f (lambda () (f)))
>     (f)))
>
> The LAMBDA is not necessary. #'f is the function already.
>
> (defun f1 ()
>   (flet ((f ()
>            1))
>     (put 'f1 'f #'f)
>     (f)))
>
> (defun f2 ()
>   (flet ((f ()
>            2))
>     (put 'f2 'f #'f)
>     (f)))
>
> CL-USER 4 > (f1)
> 1
>
> CL-USER 5 > (f2)
> 2
>
> CL-USER 6 > (funcall (geth 'f1 'f))
> 1
>
> CL-USER 7 > (funcall (geth 'f2 'f))
> 2
>
>
>
> > (defun f2 ()
> >   (labels ((f ()
> >     2)))
> >   (put 'f2 'f (lambda () (f)))
> >   (f)
> > )
>
> --http://lispm.dyndns.org/

Thank u very much for ur patience, I have fixed that.
Actually, I migrate some code from scheme with just replace the define
with defun,
and just wonder why this can't work in lisp. Thank u again.
From: Rainer Joswig
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <joswig-56B290.12251521052008@news-europe.giganews.com>
In article 
<····································@q27g2000prf.googlegroups.com>,
 Blackguester <············@gmail.com> wrote:

...

> Thank u very much for ur patience, I have fixed that.
> Actually, I migrate some code from scheme with just replace the define
> with defun,
> and just wonder why this can't work in lisp. Thank u again.

I love to help people migrate some code to Common Lisp. ;-)

-- 
http://lispm.dyndns.org/
From: Rob Warnock
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <f9KdnRAYEae2u6jVnZ2dnUVZ_szinZ2d@speakeasy.net>
Blackguester  <············@gmail.com> wrote:
+---------------
| Actually, I migrate some code from scheme with just replace the
| define with defun, and just wonder why this can't work in lisp.
+---------------

Because despite their strong surface similarities, Scheme and
Common Lisp really are *different* languages -- not just a few
keywords or function names different, but structurally different
and with different semantics [at least in places].

Also, the "internal DEFINE" in Scheme -- that is, a DEFINE inside
a <body> [as defined in the spec] -- is IMHO a very confusing
construct for newbies, since it's *really* not a "DEFINE" at all
but an implicit alias for a LETREC, see:

    http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.3

Now it happens to be the case that Common Lisp has no *exact*
equivalent for the full generality of LETREC [that is, binding
both recursive functions & local values intermingled within the
same binding form], though for bindings of functions the Common
Lisp LABELS form offers the same ability as LETREC to define
mutually-recursive functions, and the Common Lisp LET* form
offers the ability to perform variable bindings for which the
value-initializing expression can refer to earlier variable
bindings within the same LET*. So you can get the equivalent
semantics, though translating a pathologically-convoluted LETREC
might need several LABELS and/or LET* forms to get the same
resultant semantics.[1]


-Rob

[1] Hmmm... You *might* always be able to do it with only one
    SYMBOL-MACROLET form containing only one LABELS form, where
    the former defines some of the "variables" to be function
    calls of dummy functions in the latter returning quasi-contant
    values...  But I haven't verified that to be sure.

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Mark Wooding
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <slrng3b6qd.ihm.mdw@metalzone.distorted.org.uk>
Rob Warnock <····@rpw3.org> wrote:

> [1] Hmmm... You *might* always be able to do it with only one
>     SYMBOL-MACROLET form containing only one LABELS form, where
>     the former defines some of the "variables" to be function
>     calls of dummy functions in the latter returning quasi-contant
>     values...  But I haven't verified that to be sure.

I think you can always convert a Scheme LETREC into Common Lisp of the
form

  (let (...)
    (labels (...)
      (setf ...)
      ...))

There's a complication in working out a good ordering of the assignments
in the SETF form.  But R5RS imposes a restriction

: [I]t must be possible to evaluate each <init> without assigning or
: referring to the value of any <variable>.  If this restriction is
: violated, then it is an error.

which means that you don't actually have to bother, and any ordering
will in fact do.  R6RS has the same restriction, but seems to require
(11.4.6) that implementations diagnose user errors.

-- [mdw]
From: Rob Warnock
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <Z9udnWeJ-saMsKvVnZ2dnUVZ_h_inZ2d@speakeasy.net>
Mark Wooding  <···@distorted.org.uk> wrote:
+---------------
| Rob Warnock <····@rpw3.org> wrote:
| > [1] Hmmm... You *might* always be able to do it with only one
| >     SYMBOL-MACROLET form containing only one LABELS form, where
| >     the former defines some of the "variables" to be function
| >     calls of dummy functions in the latter returning quasi-contant
| >     values...  But I haven't verified that to be sure.
| 
| I think you can always convert a Scheme LETREC into Common Lisp
| of the form
|   (let (...)
|     (labels (...)
|       (setf ...)
|       ...))
| There's a complication in working out a good ordering of the
| assignments in the SETF form.  But R5RS imposes a restriction ...
+---------------

Ah, yes, of course, thanks!! And actually, becasue of that restriction,
and also because you can't [AFAIK] redefine LABELS functions with
a SETF, I don't think you need the SETF at all. Just put all the
variable bindings in the LET (*with* their initializers) and the
function bindings in the LABELS, and you're done.

Hmmm... This may mean that the full generality of the semantics
of Scheme LETREC *cannot* be expressed in CL!! For example, the
following is legal Scheme [albeit pathologically atypical!]:

    > (letrec ((a 45)
	       (b (lambda (x) (+ x 12)))
	       (c '()))
	(set! c a)
	(set! a b)
	(set! b (lambda (x) (* x 3)))
	(b (a c)))
    171
    > 

The closest I can get to that in CL is this brokenness:

    > (let ((a 45)
	    (c nil))
	(labels ((b (x) (+ x 12)))
	  (setf c a)
	  (setf a #'b)
	  (setf (fdefinition #'b) (lambda (x) (* x 3))) ; *ILLEGAL!!*
	  (b (funcall a c))))

    Invalid function name: #<Interpreted Function (LABELS B) {48948AC1}>
    [Condition of type SIMPLE-TYPE-ERROR]

    Restarts:
      0: [ABORT] Return to Top-Level.

    Debug  (type H for help)
    0] 

Oh, well...  ;-}


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Mark Wooding
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <slrng3cvb8.ihm.mdw@metalzone.distorted.org.uk>
Rob Warnock <····@rpw3.org> wrote:

> Ah, yes, of course, thanks!! And actually, becasue of that
> restriction, and also because you can't [AFAIK] redefine LABELS
> functions with a SETF, I don't think you need the SETF at all. Just
> put all the variable bindings in the LET (*with* their initializers)
> and the function bindings in the LABELS, and you're done.

Yes.  You do need the SETF to emulate R6RS's LETREC*, which partially
lifts the restriction by requiring left-to-right evaluation of the
initializers.

> Hmmm... This may mean that the full generality of the semantics
> of Scheme LETREC *cannot* be expressed in CL!!

Hmm.  The `problem' is CL's Lisp-2-ness.  I think you can still do it:
translate

  (letrec ((VAR VALUE) ...) . BODY)

to

  (let (VAR ...)
    (macrolet ((VAR (&rest #1=#:args) `(funcall VAR ,@#1#)) ...)
      (setf VAR VALUE ...)
      . BODY))

If you're feeling really brave, shadow FUNCTION and redefine it to do
the appropriate stupidity.

But this is a literal translation, and not really in the spirit of CL.
You can achieve the same effect by noting which identifiers you want to
be mutably bound to functions, and treating those as variables, using
FUNCALL explicitly where necessary.

-- [mdw]
From: ······@corporate-world.lisp.de
Subject: Re: Ask a question about internal function
Date: 
Message-ID: <440d8671-98a0-486d-b079-deba5bd9c5f9@l64g2000hse.googlegroups.com>
On 21 Mai, 10:20, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@b5g2000pri.googlegroups.com>,
>
>
>
>  Blackguester <············@gmail.com> wrote:
> > After I define these below:
>
> > (defun f1 ()
> >   (defun f ()
> >     1)
> >   (put 'f1 'f (lambda () (f)))
> > )
>
> > (defun f2 ()
> >   (defun f ()
> >     2)
> >   (put 'f2 'f (lambda () (f)))
> > )
>
> > (f1)
> > (f2)
>
> > (funcall (geth 'f1 'f))
> > (funcall (geth 'f2 'f))
>
> > where put and geth is implement by hash table
> > (defvar *op-table* (make-hash-table :test 'equal))
>
> > (defun put (op type proc)
> >   (setf (gethash (list type op) *op-table*)
> >    proc))
>
> > (defun geth (op type)
> >   (gethash (list type op) *op-table*))
>
> > when I run (funcall (geth 'f1 'f)) and  (funcall (geth 'f2 'f)), I
> > suppose to get 1 and 2 respectively, but it doesn't.
> > I get the answer depend on the execute sequence of (f1) (f2),
> > that is if execute (f1) first, I get 1 with both (funcall (geth 'f1
> > 'f)) and  (funcall (geth 'f2 'f)),
> > if execute (f2) first, I get 2 both.
> > It seems the f define in both function use a same storage, it refreshs
> > when (f1) or$B!!(B(f2) executed.
> > So I confused,  it should use different storages when internal
> > functions defined, even though they use the same name.
> > Who can explain this?
>
> DEFUN is not for defining external functions.

Oops ,I meant INTERNAL. DEFUN is not for defining internal functions.

> DEFUN defines a function for a global symbol. It is also usually
> used as a toplevel function.
>
> Define local functions with LABELS (for recursive local functions) or with FLET.
>
> In your example, you just define one function F. The second call
> overwrites the first one ( calling f1 or f2 ).
>
> --http://lispm.dyndns.org/