lua-users home
lua-l archive

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

It was thus said that the Great nobody once stated:
> Hey all,
> I'm currently doing stuff that involves a mixture of random sampling and
> enumeration.  I'm passing around a bunch of states that get modified,
> and sometimes that requires choices of which path(s) to take.
> The best thing I could come up with so far is to basically write in
> continuation passing style, i.e.
>     function foo( state )
>         -- ...unconditional stuff...
>         return choice { doThis, doThat, doSomethingElse } ( state )
>     end
> which allows `choice` to either select a branch at random or run all of
> the branches (with copies of the state).  (This "branch both ways"
> wouldn't be possible with normal if/then/else or even coroutines –
> there's no way to return to the previous point of execution.)

  That's about the best way to do it.

> (I guess I'll also be experimenting with goto and "phase tags", if I
> manage to make the choices stackless that might just be good enough…)

  If each function has a signature like:

	function foo(state)

And the last thing you do is return to another function with the same
signature, then you don't have to worry about the stack since tail call
optimzation kicks in and no new stack frame is created.
> Has anyone else done something similar and/or knows a better / more
> convenient / interesting / … alternative way to get multi-branching?

  Not without changing the language.

  -spc (And I know of a language that allows for this type of programming
	but I've only found a copy for MS-DOS and I've been having to
	reverse engineer it ... )