This package allows you to write local definitions, scoped to the surrounding begin:
(require begin-with-local)
(begin
(local n 0)
(define (count)
(set! n (add1 n))
n))
(count) ;; => 1
(count) ;; => 2
n ;; error: n: unbound identifierThis is syntactic sugar around splicing-let from racket/splicing:
(require racket/splicing)
(splicing-let ([n 0])
(define (count)
(set! n (add1 n))
n))Which is in turn similar to the let-over-lambda idiom:
(define count
(let ([n 0])
(lambda ()
(set! n (add1 n))
n)))You may also be interested in define/memo from memoize, depending
on your use case.
raco pkg install begin-with-local