Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Both methods have pros and cons, and the most advanced implementations use a hybrid method. The copying method is probably the best to start with. It may make things slower in special circumstances, but in the common case it is faster because lambdas tend to have a small number of free variables. It is also way simpler, because linked environments have a tendency to retain garbage if you are not careful. For example:

    (let ((x 1) (y 2)) 
       (lambda (z) (+ x z))
If you use linked closures naively then the closure will keep `y` in memory even though it's garbage.

However, keep in mind that what you usually not think of as free variables are free variables. For example if you have this:

    (let ((y 4))
       (map (lambda (x) (factorial (+ x y))) xs))
Then not only `y` but also `factorial` and `+` are free variables. With some work you can avoid storing those references in the closure. I believe that the state of the art is described in this paper: ftp://ftp.ecn.purdue.edu/qobi/99-105.pdf


All good points. I will surely read that paper, thanks for all your help.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: