From: ·······@LoyalistC.ON.CA
Subject: defun variable scope?
Date: 
Message-ID: <se3e25d9.001@LoyalistC.ON.CA>
  I've almost completed my Lisp interpreter, Almost out of cigarettes
and the coffee's cold.  (don't you love it!)  It's working
wonderfully, I really do like playing with this lisp stuff.
  Here's my problem: I'm unable to come up with a definitive method
of handling defun variables from the references I've collected.  I
have three different methods, the definitive one, I don't really
like, and the other two are slightly vague, I want to get this
right...

( defun foo ( x y ) ... )

    How should 'x' and 'y' be handled in the global sense?  
    If I call ( foo 5 "YELLOW" ) should 'x' and 'y' remain as global
variables bound to 5 and "YELLOW" after the function terminates?  I'm
pretty sure that this is so.  Also: (assuming 'x' and 'y' are global)
What is the best method for declaring temporary and optional
variables in the arg list?  Is there a standard method for declaring
an optional variable that will become global?
    If you have an opinion on how this should be done, even if it
doesn't agree with the 'standards' I would very much apreciate
hearing it.
                         thanx
                                    Rick
  Rick Graham                                the Binary Workshop
  ·······@loyalistc.on.ca  Data:(613)476-4898  Fax:(613)476-1516

From: Peter Dudey
Subject: Re: defun variable scope?
Date: 
Message-ID: <DUDEYP.94Aug2083824@hume.cs.orst.edu>
In article <············@LoyalistC.ON.CA> ·······@LoyalistC.ON.CA writes:

     Here's my problem: I'm unable to come up with a definitive method
   of handling defun variables from the references I've collected.  I
   have three different methods, the definitive one, I don't really
   like, and the other two are slightly vague, I want to get this
   right...

   ( defun foo ( x y ) ... )

       How should 'x' and 'y' be handled in the global sense?  
       If I call ( foo 5 "YELLOW" ) should 'x' and 'y' remain as global
   variables bound to 5 and "YELLOW" after the function terminates?  I'm
   pretty sure that this is so.  Also: (assuming 'x' and 'y' are global)

That doesn't sound right to me.  Wouldn't that make recursion a bit
messy?

See p. 77 (and thereabouts) in CLtL2.  It says "...in general, the
parameter is bound as a lexical variable...".

-- 
; Peter Dudey, MS student in Artificial Intelligence, Oregon State University ;
; ······@research.cs.orst.edu | hagbard on IGS | 257 NE 13th, Salem, OR 97301 ;
;    I like to think of myself as the "bad boy" of artificial intelligence.   ;
;       NOTICE:  As of August 20, 1994, I will become Peter Dudey Drake.      ;
From: Richard Lynch
Subject: Re: defun variable scope?
Date: 
Message-ID: <lynch-0208941442070001@lynch.ils.nwu.edu>
In article <············@LoyalistC.ON.CA>, ·······@LoyalistC.ON.CA wrote:

|  I've almost completed my Lisp interpreter, Almost out of cigarettes
|and the coffee's cold.  (don't you love it!)  It's working
|wonderfully, I really do like playing with this lisp stuff.
|  Here's my problem: I'm unable to come up with a definitive method
|of handling defun variables from the references I've collected.  I
|have three different methods, the definitive one, I don't really
|like, and the other two are slightly vague, I want to get this
|right...
|
|( defun foo ( x y ) ... )
|
|    How should 'x' and 'y' be handled in the global sense?  
|    If I call ( foo 5 "YELLOW" ) should 'x' and 'y' remain as global
|variables bound to 5 and "YELLOW" after the function terminates?

NOOOOOOOO!

If x and y have a 'global' definition, the sequence:

(defparameter x "RED")
(defparameter y 42)

(defun foo (x y) ...)

(foo 5 "YELLOW")

should end up with x bound to "RED" and y bound to 42.

If x and y have no 'global' value, then they should remain with no global
value when the function block is exited.  There is a book all about
writing a LISP interpreter/compiler.  It's in the FAQ, and you really
should check it out.

-- 
-- 
--
-- "TANSTAAFL"  Rich ·····@ils.nwu.edu
From: Marco Antoniotti
Subject: Re: defun variable scope?
Date: 
Message-ID: <MARCOXA.94Aug3094626@mosaic.nyu.edu>
In article <············@LoyalistC.ON.CA> ·······@LoyalistC.ON.CA writes:

     I've almost completed my Lisp interpreter, Almost out of cigarettes
   and the coffee's cold.  (don't you love it!)  It's working
   wonderfully, I really do like playing with this lisp stuff.
     Here's my problem: I'm unable to come up with a definitive method
   of handling defun variables from the references I've collected.  I
   have three different methods, the definitive one, I don't really
   like, and the other two are slightly vague, I want to get this
   right...

   ( defun foo ( x y ) ... )

Have you seen Guy L. Steele, Common Lisp the Language, Digital Press,
1990? Or "Lambda the ultimate imperative" by the same author (sorry I
do not remember the TR number).

Why don't you use CLISP or GCL?

Happy lisping
--
Marco Antoniotti - Resistente Umano
-------------------------------------------------------------------------------
Robotics Lab		| room: 1220 - tel. #: (212) 998 3370
Courant Institute NYU	| e-mail: ·······@cs.nyu.edu

...e` la semplicita` che e` difficile a farsi.
...it is simplicity that is difficult to make.
				Bertholdt Brecht
From: Barry Margolin
Subject: Re: defun variable scope?
Date: 
Message-ID: <31ojhbINNa1j@early-bird.think.com>
In article <············@LoyalistC.ON.CA> ·······@LoyalistC.ON.CA writes:
>  Here's my problem: I'm unable to come up with a definitive method
>of handling defun variables from the references I've collected.  I
>have three different methods, the definitive one, I don't really
>like, and the other two are slightly vague, I want to get this
>right...
>
>( defun foo ( x y ) ... )
>
>    How should 'x' and 'y' be handled in the global sense?  
>    If I call ( foo 5 "YELLOW" ) should 'x' and 'y' remain as global
>variables bound to 5 and "YELLOW" after the function terminates?  I'm
>pretty sure that this is so.  

In every Lisp I've used, lambda variables are temporary.  In Common Lisp,
if they're not declared SPECIAL, they are local, lexical variables; if
they're declared SPECIAL (either globally using PROCLAIM, DEFVAR, etc., or
locally with DECLARE) then the symbols are dynamically bound to the
arguments when the function is entered, and the old values (or
unboundedness) restored when the function exits.

>			       Also: (assuming 'x' and 'y' are global)
>What is the best method for declaring temporary and optional
>variables in the arg list?  

In Common Lisp this is done with &OPTIONAL and &AUX.

>			     Is there a standard method for declaring
>an optional variable that will become global?

I'm not sure what this means.  You can put a special variable in the
&OPTIONAL section of an argument list, if that's what you're asking about.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Michael Cornelio
Subject: Re: defun variable scope?
Date: 
Message-ID: <cornelio-030894133433@129.197.46.96>
In article <············@LoyalistC.ON.CA>, ·······@LoyalistC.ON.CA wrote:

>   Here's my problem: I'm unable to come up with a definitive method
> of handling defun variables from the references I've collected.  I
> have three different methods, the definitive one, I don't really
> like, and the other two are slightly vague, I want to get this
> right...
> 
> ( defun foo ( x y ) ... )
> 
>     How should 'x' and 'y' be handled in the global sense?  
>     If I call ( foo 5 "YELLOW" ) should 'x' and 'y' remain as global
> variables bound to 5 and "YELLOW" after the function terminates?  I'm
> pretty sure that this is so.  Also: (assuming 'x' and 'y' are global)

LEXICAL SCOPE
-------------
I would tend to agree since 'x' and 'y' are implicitly local in the
lexical sense.  That is, 'x' and 'y' are lexically visible to any nested
block within the defun.  For example:

   (defvar x 1)
   (defvar y "RED")

   (defun foo (x y)
        (let (a b c)
             (setq a x)  ;; a = 5
             (setq b y)  ;; b = "YELLOW"
             (setq c (bar))
             #'(lambda() (list x y))
        )
   )

The local definition of 'x' and 'y' should also be permanently
visible to any closures defined within "foo" ...  In the above
example (setq z (foo 5 "YELLOW")) returns a closure ... and
(funcall z) should forever return (5 "YELLOW") no matter what
'x' and 'y' are setq'ed to globally.

if "foo" should call "bar" and "bar" is defun'ed as:

   (defun bar ()
      (list x y)
   )

... then 'x' and 'y' should resolve to their global values.  That is,
(bar) should return (1 "RED") in the above example.

> What is the best method for declaring temporary and optional
> variables in the arg list?  Is there a standard method for declaring
> an optional variable that will become global?

I don't know what's best, but setting up an environment for each
closure should do it.  That is, when (foo 5 "YELLOW") is invoked,
the interpreter sets up an environment on the lexical stack with
something like (push '((x . 5) (y . "YELLOW")) LEXICAL_STACK)

Internally, when the interpreter evaluates (value-of 'x') to resolve
the (setq a x), it should look on the lexical stack to resolve 'x'.
If 'x' is not found there, then look in the GLOBAL area.

DYNAMIC EXTENT
--------------
If your interpreter supports dynamic extent (i.e., resolving a
variable on the call stack, then the environment is pushed on the
call stack ... like (push '((x . 5) (y . "YELLOW")) CALL_STACK)
and the (value-of 'x') call should look on the call stack to
resolve it.

>     If you have an opinion on how this should be done, even if it
> doesn't agree with the 'standards' I would very much apreciate
> hearing it.

CAVEAT
------
I'm not a LISP interpreter expert, but hope this helps.  If not ...
well ... bad advice is always free :-)

You might also try looking at the source code for any publc domain
LISP interpreters (like XLisp).  XLisp 2.1g contains a discussion
of the "XLisp internals" if you're interested.  XLisp is available
just about anywhere via FTP for several different machines.

;;-----------------------------------------------------------------
(setq identity '(( name "Mike Cornelio" )
                 ( company "Lockheed Media Systems Integration" )
                 ( address "1111 Lockheed Way" )
                 ( city+state "Sunnyvale, CA")
                 ( voicemail  "408.756.7882" )))   
From: Jens Kilian
Subject: Re: defun variable scope?
Date: 
Message-ID: <JENSK.94Aug4082345@hpbbrn.bbn.hp.com>
RGRAHAM> ( defun foo ( x y ) ... )

RGRAHAM>     How should 'x' and 'y' be handled in the global sense?  

That depends.  If they have been declared 'special', the function call should
affect the global value, otherwise the binding is local to 'foo' and the global
values should not change.  (This is a question of 'scope' and 'extent'. You
will find more information about this in "Common Lisp: The Language", or any
decent textbook on Lisp.)

RGRAHAM>     If I call ( foo 5 "YELLOW" ) should 'x' and 'y' remain as global
RGRAHAM> variables bound to 5 and "YELLOW" after the function terminates?

Never, never, never, never, never.  When control leaves a binding construct,
the bindings must vanish.

Bye,
	Jens.
--
Internet: ·····@hpbbn.bbn.hp.com | Phone: (0|+49)7031-14-4785 (TELNET 778-4785)
MausNet:  Jens Kilian @ BB       | Fax :  (0|+49)7031-14-2049
---------------------------------+---------------------------------------------
As the air to a bird, or the sea to a fish, so is contempt to the contemptible.