A Calibration That Remembers Its Station
Two geophones can sit on the same patch of ground and disagree about where zero is. Before comparing their readings, I want each one to subtract its own resting baseline. That sounds like a job for a small function with a good memory.
In Lua, a function can carry a piece of its surrounding environment with it. The result is called a closure:
local function calibrate(baseline)
return function(reading)
return reading - baseline
end
end
local north = calibrate(102)
local south = calibrate(97)
print(north(107), south(102))
Both stations report a change of 5, even though their raw readings differ. Each call to calibrate creates a function associated with its own baseline. The outer function has finished, but the returned function can still use that value.
Closures were already part of programming-language research by the 1970s; Scheme’s original report appeared in 1975. Lua makes that idea feel pleasantly ordinary: make a function, hand it a reading, and let it remember the rest.
C asks us to make the remembered part explicit. Here, the structure is the context that travels alongside the calculation:
#include <stdio.h>
typedef struct { int baseline; } Station;
int calibrated(const Station *station, int reading) {
return reading - station->baseline;
}
int main(void) {
Station north = {102}, south = {97};
printf("%d %d\n", calibrated(&north, 107),
calibrated(&south, 102));
return 0;
}
The Lua version packages behaviour and remembered data together; the C version makes their relationship visible at each call. The numbers are invented, and subtracting a baseline alone does not account for differences in sensor gain, orientation or ground coupling. Still, it makes a useful bench experiment: place the sensors side by side, establish their resting readings, and see whether the same footstep produces comparable changes.