[plt-scheme] "Hiding" a global variable
Please do not post homework questions here, esp. not partial
solutions. Better to contact your instructor (you are paying them,
right?) or at least don't include solutions so your classmates don't
see them.
Robby
On Tue, Oct 20, 2009 at 10:00 PM, Mike <winhqwebmstr at gmail.com> wrote:
> Hello,
>
> 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.... "
>
>
> I wrote the function implementing a global variable, as follows;
>
> (define times 0)
>
> ; next - returns how many times it has been called each time it is
> called
> ; @ none
> ; -> number
>
> (define (next)
> (begin (set! times (add1 times))
> times))
>
> I now need to make an implementation without using a global variable.
> Any suggestions?
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>