Skip to content

Instantly share code, notes, and snippets.

@beoliver
Created May 10, 2015 19:18
Show Gist options
  • Save beoliver/2ffedd4bad090f98511a to your computer and use it in GitHub Desktop.
Save beoliver/2ffedd4bad090f98511a to your computer and use it in GitHub Desktop.
(define (make-counter-1)
(let ((counter 0))
(lambda ()
(set! counter (+ counter 1))
counter)))
(define (make-counter-2)
(define counter 0)
(lambda ()
(set! counter (+ counter 1))
counter))
(define (make-counter-3)
(define counter 0)
(define (count)
(set! counter (+ counter 1))
counter)
count)
;; in the REPL ...
> (define counter-1 (make-counter-1))
> (define counter-2 (make-counter-2))
> (define counter-3 (make-counter-2))
> (counter-1)
1
> (counter-2)
1
> (counter-3)
1
> (counter-1)
2
> (counter-2)
2
> (counter-3)
2
> (counter-1)
3
> (counter-1)
4
> (counter-1)
5
> (counter-2)
3
> (counter-2)
4
> (counter-2)
5
> (counter-3)
3
> (counter-3)
4
> (counter-3)
5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment