by chimeracoder · 14y ago · 0 comments
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
http://docs.python.org/release/1.4/lib/node10.html#SECTION00...
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.
It's not a matter of in-place mapping, but if I remember correctly, map() returns a generator in Python3, which has the effect of simulating single-traversal and lazy evaluation like Haskell does.
> mutability is a property of data-structures, not of functions
True, but in a (purely) functional language, if all data structures are immutable and functions are simply mappings of input values to output values with no side-effects, that's inconsequential. We're talking about simulating a functional paradigm within a non-functional language with mutable data structures, so writing functions without side-effects would be more idiomatic - hence the problems with trying to write Python in a functional style.