lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Tony Finch <> writes:

> On Wed, 13 Jan 2010, David Kastrup wrote:
>> Wesley Smith <> writes:
>> >> Tail calls are good for state machines.
>> >>
>> >
>> > I find coroutines more elegant.
>> Yup.  Nothing beats using the instruction pointer and dynamic state as
>> state variable.
> What I meant was how to represent the coupling between the instruction
> pointer and the program state in the source code. You can represent
> each state as a function so that state transitions are tailcalls.

But that often causes contortions as well.  You gain a lot more
expressive power if "state transitions are calls" merely.  Sometimes it
is not easy to arrange for a call to get tail.

> If C supported tail calls, then I could implement each state as a
> function, state transitions as tail calls, and nesting as a normal
> function call. No need for a trans_table[], no process() loop, and no
> need for an explicit ifstate[] array of nested states.

But nesting does not work as a tail call, so you can't save the state
between state machine invocations.

David Kastrup