// closure.c // Example to show how lambda functions can be implemented using C code. #include typedef int (*fp_t)(); // function pointer typedef struct { int x; } G_ENV; typedef struct { fp_t lambda; G_ENV env; } G_CLOSURE; int g_lifted_lambda(G_ENV g_env) { return g_env.x; } G_CLOSURE f_create_closure(int x) { G_ENV g_env; g_env.x = x; G_CLOSURE clsr; clsr.env = g_env; clsr.lambda = g_lifted_lambda; return clsr; } int main() { G_CLOSURE a, b; // a = f(10) in Python becomes . . . a = f_create_closure(10); // b = f(20) in Python becomes . . . b = f_create_closure(20); // a() in Python becomes . . . printf("Output of evaluating a() = %d\n", a.lambda ( a.env )); // b() in Python becomes . . . printf("Output of evaluating b() = %d\n", b.lambda ( b.env )); return 1; }