[chuck-users] Time logic
ssalazar at CS.Princeton.EDU
Sun Nov 11 04:10:44 EST 2007
On Nov 10, 2007, at 6:04 PM, Juan-Pablo Caceres wrote:
> A 'while(expression)' cycle will stop executing the statement as
> soon as
> the expression evaluates to 'false'. In the case of evaluating (dur =>
> now), I think that there's also a timing logic involved, in the sense
> that if (1::second => now) evaluates to 'true' only when 1 second has
> passed, it should evaluate to false in every other time, which would
> cause the control to exit the block.
> What (I think) is going on is that (dur => now) as an expression is
> waiting, advancing time, and as soon as time has advances 'dur', it
> evaluates to true by default. The fact that this expression
> evaluates to
> true after the waiting time is what confuses me, compared to
> programing language control structures.
As with the assignment operation in C and others, the specific
semantics of (A => B) are that the expression evaluates to whatever
the value of B is after the => part has occurred.
If B happens to be 'now', chucking to it has the side effect of
pausing that shred for the duration specified by A. Nothing in the
control flow of a shred executes when that shred is waiting for the
duration of A to pass.
Additionally, keeping in mind that (A => B) evaluates to B regardless
of what A and B are, if B is not zero after the chuck operation, it
will evaluate to true in a conditional expression. If B happens to be
'now', it will almost always evaluate to true, the exception being at
the very beginning of the ChucK virtual machine.
In consequence, running this program like so:
// begin 0samp.ck
if( 0::samp => now )
<<< "Hello!" >>>;
// end 0samp.ck
$ chuck 0samp.ck
produces no output, because the program runs while 'now' is still 0
(i.e. no time has passed), and thus evaluates to false in the
conditional. Running this one does print something, because the
program allows one sample's worth of time to pass:
// begin 1samp.ck
if( 1::samp => now )
<<< "Hello!" >>>;
// end 1sampl.ck
$ chuck 1samp.ck
(Note that these examples don't work right in miniAudicle or through a
chuck --loop, because the virtual machines created in those
environments allow 'now' to become non-zero before any programs can
Though in my humble opinion, its not very clear or readable to put
things like in 1::second => now in a conditional expression.
Hopefully this doesn't confuse matters more...
> Sorry for the lengthy and confusing email!
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
More information about the chuck-users