News for hackers. Stuff that matters.
Re:Functional programming in Python by chimeracoder on Friday May 11, 2012 @06:04PM (#3961682)
That's the problem. I can fake a functional paradigm within Python in some settings, but it falls apart at the seams.
Re:Functional programming in Python by groovy2shoes on Friday May 11, 2012 @06:19PM (#3961740)
You can always use 2-tuples to get lists in the Lisp sense. You'd add an item to the head of a list by just pairing it:

    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]