Blogger

Delete comment from: The History of Python

NevilleDNZ said...

re: Closures - Compare Python to Algol 68...
# Python #
from math import *
root = lambda x: lambda y: exp(log(y)/x);
square_root = root(2); cube_root = root(3);
print square_root(64), cube_root(64)
# Algol 68 #
MODE F = PROC(REAL)REAL;
PROC root = (REAL x)F: (REAL y)REAL: exp(ln(y)/x);
F square root = root(2), cube root = root(3);
print((square root(64), cube root(64), new line))
Output: 8.0 4.0

The key point about scoping is that with Algol 68 each argument "x" exists only on the LOC stack. Hence when "root" returns "x" becomes out of scope (off the stack). In python each "x" is in HEAP, then "follows" square_root and cube_root until these functions are no longer referenced.

Quick fix for Algol 68 could be to put x on the HEAP eg.
PROC root = (HEAP REAL x)F: (REAL y)REAL: exp(ln(y)/x);
And let the garbage collector sort out x's closure.

Minor other differences: Algol 68 is "IMPLICIT NONE", (useful for picking up bugs prior to runtime).

May 24, 2009, 8:03:41 AM


Posted to Origins of Python's "Functional" Features

Google apps
Main menu