From: Barry Margolin
Subject: Re: closure vs global
Date: 
Message-ID: <barmar-7DDD85.22105515042006@comcast.dca.giganews.com>
In article <······················@speakeasy.net>,
 massimo <····@speakeasy.net> wrote:

> ok.
> 
> so if i have this global *test*, which is used in other functions, how 
> can i get rid of the global and use lexical variable using let??
> 
> (defparameter *test* (another-function-call))
> 
> this is your solution:
> 
> (let ((*test* (another-function-call))
>        ;; processing
>        ;; calls to other functions referencing *test*>
>          ) ;; end of let
> 
> 
> how would I use closure to include all functions that use *test*?
> let's say that this is one of the function that uses *test*:
> 
> (defun function-name ()
>      <do-something-with> *test*)

(let ((test (func)))
  (defun func1 ...)
  (defun func2 ...)
  (defun func3 ...))

However, beware that some implementations won't compile functions that 
are defined within a non-null lexical environment.

I'm not sure why you want to do this.  What's wrong with a global 
variable for something that really is global?

-- 
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: Ken Tilton
Subject: Re: closure vs global
Date: 
Message-ID: <Zgt0g.20$np.13@fe08.lga>
massimo wrote:
> I don't think it's about being lazy. I'm new to LISP and i'm just trying 
> to find a solution. 

Oh. I thought you answered Barry that you agreed a global would be fine 
but that you had already changed all the code and did not want to go 
back. But I am a careless reader.

> I like to explore different ones so i learn more.

Not a bad idea. Lisp is kinda big tho and the trick is knowing when to 
use what, so just trying things for the sake of ... well, might work.

> Then if i'm bothering someone, well I'm sorry it went that way. It 
> wasn't my intention.

Actually, I am not saying it was, I am trying without success to get you 
to read and respond to what people are offering.

> I don't like using global. I think it can be done 
> in better ways than global..i don't know if in lisp it's the same/.

In the solution you seem to have settled on, it seems to me you still 
have a global in spirit, you have just hid it in a lexical behind a 
lambda behind a function (which is global). But maybe that is all you 
want, namely to hide the global behind an interface. If so...

The Spirit of Lisp is not to slow ourselves down by creating artificial 
barriers to productivity in the hope that somehow they increase 
productivity by preventing us from shooting ourselves in the foot. So 
just make it a global.

> 
> no the code i sent you is the same. I just got rid of the global *test*.
> Basically what i wanted to do is: (and I might easily be wrong)
> i have that global variable which refers to (foo). This global is 
> accessed from other functions. and i think we have that down. np
> 
> i was wondering if there was a way using lexical variable, to write a 
> closure so i could get rid of the global and still refer to the variable 
> from those functions where i had *test* as global. so I thought i could 
> lambda in the closure But i'm not even sure it's possible.
> 
> The solution you gave i used incorrectly. and I apologize. If i'm 
> wasting your time, I apologize for that too.

<g> I appreciate that, but I am more interested in newbies being 
responsive to any help offered. Every few months we get someone in here 
asking questions and rejecting all answers. Usually, like you, they have 
fixated on some obscure syntactic goal and do not seem to be focused 
much on any actual functional task. Lisp will eat anyone alive if they 
do not worry more about the task than about all the things possible in 
the language.

ken

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.