in Common Lisp, i can do (let ((x 1))
(defun foo ()
(print x)))
If i transcribe that to Racket, i get the error “no expression after a sequence of internal definitions.” is there a way to create a non-internal definition that is closed over a let
? Or is that not something people do in Racket?
Racket definitions are scoped to their surrounding block, but since (define (f x) ....)
is just sugar for (define f (lambda (x) ....))
, you can use the let-over-lambda idiom to write: (define foo
(let ([x 1])
(lambda ()
(print x))))
ah, right, now i’m reminded that i’d seen that before. thank you.
That’s the usual Scheme/Racket idiom, but you can also use splicing-let
from racket/splicing
if you want something that looks closer to your original snippet.
i’m fine with the scheme idiom. thanks for your help!
@jjwiseman splicing-let
~works the same way~ has the same result as Common Lisp let
does in your example. You can put define
s inside
(splicing-let ([x 1])
(define (foo)
(print x)))