muLISP has an interesting way of implementing lexical-style closures in a dynami...

@psf@oldbytes.space

muLISP has an interesting way of implementing lexical-style closures in a dynamically scoped #Lisp, a method it claims to be "patterned after the Lisp Machine LISP dynamic closure package".

$ (setq quintupler (let-closed ((a 5)) '(lambda (x) (* x a))))
(LAMBDA CLOSURE-ARGLIST (CLOSURE-APPLY (QUOTE (LAMBDA (X) (* X A))) (QUOTE ((A .
5))) CLOSURE-ARGLIST))

$ (setq b 6)
6

$ (setq sextupler (closure '(b) '(lambda (x) (* x b))))
(LAMBDA CLOSURE-ARGLIST (CLOSURE-APPLY (QUOTE (LAMBDA (X) (* X B))) (QUOTE ((B .
6))) CLOSURE-ARGLIST))

(btw lambda being followed by an atom like CLOSURE-ARGLIST rather than a list is how muLISP does varargs, so don't be confused by that part like I was)

You can see that it packages up the values of captured variables in an alist. CLOSURE-APPLY puts those variables into scope, calls the function, then when it's done, packages those variables back into the alist for later.