PasteRack.org
Paste # 7930
2025-04-06 14:15:36

Fork as a new paste.

Paste viewed 412 times.


Embed:

  1. #lang racket
  2.  
  3. (define (make-counter)
  4.   (let ([count 0]) ;; local variable
  5.     (lambda ()
  6.       (set! count (+ count 1))
  7.       count))) ;; return the updated count
  8.  
  9. ;; Create a counter
  10. (define counter (make-counter))
  11.  
  12. ;; Call it a few times
  13. (displayln (counter)) ;; 1
  14. (displayln (counter)) ;; 2
  15. (displayln (counter)) ;; 3

=>

1

2

3