Exercise 3: A Program With No Garbage — Possible Solution ==================================================================== THE PROGRAM ------------------------------ fun makeAdder(x) { fun adder(y) { return x + y; } return adder; } var a = makeAdder(10); var b = makeAdder(20); var c = makeAdder(30); print a(1); print b(1); print c(1); RESULT ------------------------------ output: ['11', '21', '31'] heap before collect_garbage(): 8 collect_garbage() collects: 1 heap after: 7 WHY IT ISN'T ZERO ------------------------------ Every object the WISP PROGRAM itself created is genuinely reachable: makeAdder's own closure via the global 'makeAdder', and each of the three (Upvalue, Closure) pairs via globals 'a', 'b', and 'c'. None of that -- 7 objects total -- gets touched. The one object collected is, once again, the top-level script's own Closure (object #1, in this exercise's own numbering, matching Exercise 1's own finding). It was only ever reachable through self.frames while the script itself was actively running; once run() returns, self.frames is empty, and nothing else in the whole program -- not globals, not any surviving closure's own upvalues -- ever pointed at the script's own closure to begin with. It was never part of the "real" Wisp program's own data; it was scaffolding this course's own compile_and_run() helper needed to get the program started at all. WHY THIS WORKS AS AN ANSWER ------------------------------ This confirms the collector behaves correctly at the boundary case that matters most for trust: when nothing is actually garbage, it doesn't invent any. The single object it does collect isn't a false positive -- it's a real, honestly unreachable object, just one this exercise's own framing ("no garbage from the WISP program") didn't originally count, the same oversight Exercise 1 already flagged. A collector that swept zero objects here would be just as correct in spirit; the fact that it correctly finds exactly the one genuinely dead object, and nothing else, is the actual demonstration of correctness -- not the raw number itself.