Elektrine lite

← Feed

@psf@oldbytes.space

2026-07-02 17:32 UTC

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.

Replies (1)

  • @Kazinator@mstdn.ca 2026-07-02 21:02

    @psf@oldbytes.space What is that? Are closures just tree structure in muLISP, whereby you can use rplaca to replace some element, like the closed-over environment? Which is just a naive alist? (Why does that need the (quote ...) wrapping). So you made a couple of closures which reference a free variable, and then you injected a shared environment which closes the variable. Whether you can do that in another Lisp depends on whether such a manipulation of its functions is allowed. There may also be multiple representations to deal with: one way to manipulate the environment of a compiled closure vs. interpreted one. I suspect that in compiled Lisps, you may not be able to do it exactly like that. If a closure is compiled whose body refers to a free variable x, the generated code will not be accessing that x through the environment vector: the x references get compiled differently based on its scoping discipline.

    Open ##3542370