Elektrine lite

← Feed

@psf@oldbytes.space

2026-07-01 23:47 UTC

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

Replies (3)

  • @psf@oldbytes.space 2026-07-02 17:32

    Aha! If you can arrange for the "closures" to share structure, then you'll have the equivalent of "multiple functions closed over the same environment". Again in muLISP: (let ((alist '(quote ((x . 1))))) (setq do+1 (closure nil '(lambda () (setq x (+ x 1))))) (setq do-1 (closure nil '(lambda () (setq x (- x 1))))) ; Now arrange for both "closures" alists to be shared list structure (rplaca (cddr (caddr do+1)) alist) (rplaca (cddr (caddr do-1)) alist)) (funcall do+1) ; 2 (funcall do+1) ; 3 (funcall do-1) ; 2 (funcall do-1) ; 1 Does any brave soul want to try the equivalent code in Lisp Machine #lisp? ;) If one was gonna do this sorta thing frequently, one could extend the closures package with a macro automating these steps.

    Open ##3542371

  • @psf@oldbytes.space 2026-07-02 00:22

    Meanwhile, in a traditional 1960s or 1970s Lisp you would use FUNCTION, which would implement a FUNARG device that closes over the dynamic environment. However, I can't find a vintage environment offhand that lets me demonstrate this. Medley Interlisp-D seems to have dropped support at some point! (BTW, in the Interlisp version of FUNCTION, it appears you still must specify a list of variables to close over, not unlike in Lisp Machine Lisp's CLOSURE. It doesn't snag the whole environment by default. That's the purpose of the mysterious (X) after the LAMBDA.)

    Open ##5287719

  • @simon_brooke@mastodon.scot 2026-07-02 11:50

    @psf@oldbytes.space All destructive modification is a dirty hack. In the early days of #Lisp, we were running on machines with very small memories -- I learned Lisp on a machine with under 30Kb of memory available to the Lisp interpreter. On such a machine, destructive modification was sometimes necessary. But we knew that it was generally bad style, to be avoided if possible. Now we have more memory, Lisps with immutable data, like #Clojure, are to be preferred.

    Open ##5287720