jjwiseman
2018-4-24 20:11:27

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?


lexi.lambda
2018-4-24 20:13:22

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


jjwiseman
2018-4-24 20:13:55

ah, right, now i’m reminded that i’d seen that before. thank you.


lexi.lambda
2018-4-24 20:14:05

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.


jjwiseman
2018-4-24 20:14:23

i’m fine with the scheme idiom. thanks for your help!


dedbox
2018-4-24 21:06:35

@jjwiseman splicing-let ~works the same way~ has the same result as Common Lisp let does in your example. You can put defines inside


dedbox
2018-4-24 21:08:05

(splicing-let ([x 1]) (define (foo) (print x)))