A ListComp that maintains its own state (Was: Re: turing machine in an LC)
From: Michael Spencer (mahs_at_telcopartners.com)
Date: 02/08/05
- Next message: Diez B. Roggisch: "Re: Confused with methods"
- Previous message: Fredrik Lundh: "Re: Loop in list."
- In reply to: Bernhard Herzog: "Re: turing machine in an LC"
- Next in thread: Carl Banks: "Re: A ListComp that maintains its own state (Was: Re: turing machine in an LC)"
- Reply: Carl Banks: "Re: A ListComp that maintains its own state (Was: Re: turing machine in an LC)"
- Reply: Bernhard Herzog: "Re: A ListComp that maintains its own state"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: python-list@python.org Date: Tue, 08 Feb 2005 14:00:25 -0800
> Jeremy Bowers <jerf@jerf.org> writes:
>
>
>>On Tue, 08 Feb 2005 17:36:19 +0100, Bernhard Herzog wrote:
>>
>>>Nick Vargish <nav+posts@bandersnatch.org> writes:
>>>
>>>>"Xah Lee" <xah@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.
So, here's factorial in one line:
# state refers to list of state history - it is initialized to [1]
# on any iteration, the previous state is in state[-1]
# the expression also uses the trick of list.append() => None
# to both update the state, and return the last state
>>> [state.append(state[-1] * symbol) or state[-1]
... for symbol, state in it.izip(range(1,10),it.repeat([1]))
... ]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
>>>
Now, who was claiming that 'reduce' was opaque?
Michael ;-)
- Next message: Diez B. Roggisch: "Re: Confused with methods"
- Previous message: Fredrik Lundh: "Re: Loop in list."
- In reply to: Bernhard Herzog: "Re: turing machine in an LC"
- Next in thread: Carl Banks: "Re: A ListComp that maintains its own state (Was: Re: turing machine in an LC)"
- Reply: Carl Banks: "Re: A ListComp that maintains its own state (Was: Re: turing machine in an LC)"
- Reply: Bernhard Herzog: "Re: A ListComp that maintains its own state"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|