133 points · by plessthanpt05 · 14y ago · 61 comments
here are some different ways to implement functional sequence operations without native python syntax like `yield`:
https://github.com/dustingetz/sandbox/blob/master/etc/lazy.p... https://github.com/dustingetz/sandbox/blob/master/etc/map.py
and an unfinished, sloppy impl of monads in python with lots of minor errors:
http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-t...
Even things like tail call elimination, he said, would just encourage people to use more recursion, when iteration was easier to read, and in places where TCE would work, the programmer could obviously convert it to iteration himself and should do so for code clarity.
I like Python, and I like a lot of things about functional programming, so it wasn't the answer I was hoping for. Unless Guido has had a change of heart since then, I wouldn't pin my hopes on Python ever becoming a first-rate functional programming language. It's first rate at the things Guido values.
Decorators are another example of functional programming added to Python, but with a pythonic syntax.
Python supports both imperative, OO and functional idioms, but generally only prefers one of them for a specific task. For example loops are preferred to recursive functions when appropriate. Objects are preferred to closures for mutable state, so mutation of captures variables has not been supported (support have been added recently with nonlocal, but it has clearly not been a priority).
Python is interesting because it is multiparadigm while trying to avoid fragmentation where you can solve the same problem in totally different styles.
There's a quote from Guido about private attribute access where he said, "We're all adults", and that leaving the open is beneficial.
Recursion is another tool that he should let us adults use if it solves a problem better and TCE is lovely feature to have. Frankly, if you're using recursion, you either just learned it in a CompSci class or you know what you're doing. Either way, daddy Guido shouldn't tell us no :).
Anyway, at the very least there's two camps. On the one side, you have the hardcore Scheme folks whose conception of functional programming is "if I can't figure out how to write it with tail recursion it must not be computable". On the other side you have the Haskell folks whose conception of functional programming is "a monad is just a monoid in the category of endofunctors, what's the problem?"
Somewhere in between are people who actually get things done.
I started doing a lot of programming in CoffeeScript a few months ago, and between the JS underpinnings and the CS syntax you can do things that would just be painful in Python.
“… a persistent data structure is a data structure that always preserves the previous version of itself when it is modified. Such data structures are effectively immutable, as their operations do not (visibly) update the structure in-place, but instead always yield a new updated structure.”
Anyone can write whatever Python libraries they want, but I doubt anything of the sort would be added to the standard library, and certainly nothing that would require an FP-aware interpreter to optimize in a useful way.
That said, the standard library leaves something to be desired for the functional style. It makes sense for object-oriented programming to have non-mutating functions return the result and mutating functions return None (think sorted vs. sort), but this can get rather irritating when you're trying to write in a purely functional paradigm.
Also, am I forgetting my Python, or does it not have a great solution for appending to the front of a list? One thing I love about Lisp is that it's ridiculously easy to write (cons item some-list), or even (cons item1 (cons item2 some-list)). Doing the same thing in Python is irritating, because insert() doesn't return the result list.
...or maybe that's what I deserve for trying to write Lisp-like code in Python, anyway....!
EDIT: As noted in the comments, the '+' operator will suffice here. Though since lists are really arrays and not linked lists, this functional way of thinking will result in horribly inefficient CPython code.
lst = (2, (3, ()))
newlst = (1, lst)
Toss in some small helper functions and they become less unwieldy. You could pretty easily write an iterator for processing these lists: class List:
def __init__(self, lst):
self.cursor = lst
def __iter__(self):
return self
def next(self):
if self.cursor == ():
raise StopIteration
else:
head = self.cursor[0]
self.cursor = self.cursor[1]
return head
Then you can do stuff like: >>> for i in List(newlst):
... print i
...
1
2
3
>>> map(lambda x: x + 1, List(newlst))
[2, 3, 4] One thing I love about Lisp is that it's
ridiculously easy to write (cons item some-list),
or even (cons item1 (cons item2 some-list)).
Iterators can help (and in O(1)): >>> from itertools import chain
>>> some_list = range(5)
>>> item = 99
>>> a = chain([item], some_list)
>>> for x in a:
... print x
...
99
0
1
2
3
4However, I'll agree that it would be disingenuous to say it's not a little peculiar, and surprising to see the source for it :)
In all seriousness, though, if I really wanted a functional implementation, I wouldn't be using Python. It's more just nice to know that I can create an object that's immutable and which functions more or less like a CLOS object, so that I can think in that mindset instead. Also, I believe it doesn't come with all of the hidden functions that it would otherwise.
In the end, if I really cared, I'd actually write a wrapper around the type() function.
for concatenating lists, "+" is standard
The rest still holds, though - not all functions have a mutable and immutable counterpart (like sort) and map() returning lists makes them rather unwieldy. I think they fixed this in Python 3, though I'm not sure if it was backported to 2.7.
So I don't really get your concern, plus mutability is a property of data-structures, not of functions.