Elektrine lite

← Feed

@psf@oldbytes.space

2026-06-19 06:04 UTC

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.

Replies (2)

  • @psf@oldbytes.space 2026-07-01 23:47

    I found a weird thing about "Lisp machine closures", at least as implemented in muLISP. In ordinary lexically-scoped #Lisp, multiple functions can close over the same variables. (let ((x 1)) (defun do+1 () (setq x (+ x 1))) (defun do-1 () (setq x (- x 1)))) (do+1) ; 2 (do+1) ; 3 (do-1) ; 2 (do-1) ; 1 In muLISP it's considerably harder to do this because the closure function creates a new alist of "closed-over values" per closure, so they are all independent. Judging by the Lisp Machine Manual, things appear to work the same way over there too. (let ((x 1)) (setq do+1 (closure '(x) '(lambda () (setq x (+ x 1))))) (setq do-1 (closure '(x) '(lambda () (setq x (- x 1)))))) (funcall do+1) ; 2 (funcall do+1) ; 3 (funcall do-1) ; 0 (do-1 is a logically separate closure) (funcall do-1) ; -1 But what you can do is close over a list and do destructive list operations. Then everyone's alist has a reference to the same list, so the closures communicate by destructively modifying the list. I have no idea if this is just a dirty hack or if people used this in practice. (let ((x (list 1))) (setq do+1 (closure '(x) '(lambda () (incf (car x))))) (setq do-1 (closure '(x) '(lambda () (decf (car x)))))) (funcall do+1) ; 2 (funcall do+1) ; 3 (funcall do-1) ; 2 (funcall do-1) ; 1

    Open ##3542372

  • @psf@oldbytes.space I spent a lot of time on lispms in the 80s, and never saw anything like this. Since they use QUOTE rather than FUNCTION on the enclosed lambda, isn't it hopeless to compile this? OTOH, I've also never touched muLISP. Maybe there was only an interpreter.

    Open ##5287723