Skip to content

Comment on SICP in Pythonparent

Comments

Python has some powerful elements too. It sure it lacks elements and features of historical and current pure functional[1] programming languages, but I find it elegant to use python as a set-theory powered language. Often I can solve complex problems in a few lines by thinking about the sets of elements I am manipulating, constructing and transforming them. Instead of imperative, python code becomes very descriptive. Indexing a dictionary with frozensets and making use of a proper definition of __hash__ to put various objects as dictionary keys or inside sets allows for tremendous power and terseness and explicitness.

In [2] the solution given by yairchu can be written succinctly via generators, which look extremely like a mathematical set definition.

    def grandKids(generation, kidsFunc, val):
        return reduce(lambda a, v: (x for v in a for x in kidsFunc(v)), xrange(generation), [val])
I often happen to think about a problem and explicit it with pen and paper using pure mathematical set notation then implement it in Python.

[1] http://stackoverflow.com/questions/1017621/why-isnt-python-v... [2] http://stackoverflow.com/questions/1016997/generate-from-gen...

Mind: I said "a pain in the arse", not "impossible". And i think not only about the writing (writing such things regularly is not what any programmer would want to do), but also the debugging perspective.

What you have done there is cute and commendable as a mental exercise, but if i'd ever encounter that in production code someone would make a close encounter with my chainsaw. This is basically undebuggable (i don't know the line/statement debuggers python has available, so i'm guessing here at what happens) because either the debugger will step over that in one step because it's one statement, or it'll just keep stepping on the same line again and again which is also entirely useless.

Lastly, what you did there is basically golf the living hell out of that code to bring it down from multiple statement lines to a single one. If i have to reach to such means i might as well use Perl. Only Perl actually does allow me to put multiple statement subs into a lambda, so i don't have to golf there.

Weird days when i have to golf in Python but don't need to in Perl.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.