A ListComp that maintains its own state (Was: Re: turing machine in an LC)

Carl Banks invalidemail at aerojockey.com
Tue Feb 8 21:34:20 EST 2005


Michael Spencer wrote:
> > Jeremy Bowers <jerf at jerf.org> writes:
> >
> >
> >>On Tue, 08 Feb 2005 17:36:19 +0100, Bernhard Herzog wrote:
> >>
> >>>Nick Vargish <nav+posts at bandersnatch.org> writes:
> >>>
> >>>>"Xah Lee" <xah at xahlee.org> writes:
> >>>>
> >>>>>is it possible to write python code without any indentation?
> >>>>
> >>>>Not if Turing-completeness is something you desire.
> >>>
> Bernhard Herzog wrote:
> ....
> a Turing Machine in one line plus assignments - nice!  Turns out that
pypy is more
> verbose than strictly necessary ;-)
> ...
>
>
> BTW, I realized that it is indeed possible for a LC to maintain its
own state
> without being passed an external mutable.  The trick is to use
itertools.repeat
> to return the same mutable object on each iteration.

Pay attention, chief.  I suggested this days ago to remove duplicates
from a list.

from itertools import *
[ x for (x,s) in izip(iterable,repeat(set()))
  if (x not in s,s.add(x))[0] ]

;)

There is a way to even avoid repeat if you're feeling EVIL.

[ x for x in iterable if x not in locals()['_[1]'].__self__ ]

Turning this into a turing machine is left as an exercise.  The recipe
in effect:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297


-- 
CARL BANKS




More information about the Python-list mailing list