[plt-scheme] "Hiding" a global variable

From: Mike (winhqwebmstr at gmail.com)
Date: Tue Oct 20 23:00:00 EDT 2009


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)
> (next)
> (next)

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
; @ none
; -> number

(define (next)
   (begin (set! times (add1 times))

I now need to make an implementation without using a global variable.
Any suggestions?

Posted on the users mailing list.