[plt-scheme] "Hiding" a global variable

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Wed Oct 21 12:42:09 EDT 2009

On Oct 20, 2009, at 11:00 PM, Mike wrote:

> I have a rather difficult homework question. I would like to know what
> direction to look in in order to solve it:
>
> "Develop a function named next that takes no arguments, but each time
> you call it, it returns how many times it has been called:
>
>> (next)
> 1
>> (next)
> 2
>> (next)
> 3
>
>
> Hint: You can do this with a global variable, but it would be "neater"
> if you could hide the variable. Think about local.... "

Mike, one of your classmates was just in my office asking about the  
same problem.  Here's another hint.

You need a local in order to have a variable that the function can  
update, but not have this variable be globally visible.  But if you  
put the local inside the function definition, it'll create a NEW  
variable every time you call the function, so that's no good.  Thus  
the local must be OUTSIDE the function definition.

See if that gets you a little farther.



Dr. Stephen Bloch
Math/CS Department
Adelphi University
sbloch at adelphi.edu



Posted on the users mailing list.