Hi there, I have another question concerning time logic. The following 2 examples are similar: // 1) while (true) { <<<"HELLO">>>; 1::second => now; } // 2) while (1::second => now) { <<<"HELLO">>>; } So the question is, is the second case using some syntactic sugar? Because conceptually it doesn't sound right to me. Maybe the chuck gurus out there can make more sense of it? Thanks! JPa.
lör 2007-11-10 klockan 09:29 -0800 skrev Juan-Pablo Caceres:
Hi there,
I have another question concerning time logic. The following 2 examples are similar:
// 1) while (true) { <<<"HELLO">>>; 1::second => now; }
// 2) while (1::second => now) { <<<"HELLO">>>; }
So the question is, is the second case using some syntactic sugar? Because conceptually it doesn't sound right to me. Maybe the chuck gurus out there can make more sense of it?
Well, it's pretty easy, actually. While takes an expression, and if that expression returns true, it evaluates the code inside the brackets, and then it tests the expression again. This expression can be anything from "true", to "i<100", or even a function (that can do absolutely anything). in the second case it's "1::second=>now", which will return true when 1 second have passed. Hope that helps, Gasten
Martin Ahnelöv wrote:
lör 2007-11-10 klockan 09:29 -0800 skrev Juan-Pablo Caceres:
Hi there,
I have another question concerning time logic. The following 2 examples are similar:
// 1) while (true) { <<<"HELLO">>>; 1::second => now; }
// 2) while (1::second => now) { <<<"HELLO">>>; }
So the question is, is the second case using some syntactic sugar? Because conceptually it doesn't sound right to me. Maybe the chuck gurus out there can make more sense of it?
Well, it's pretty easy, actually. While takes an expression, and if that expression returns true, it evaluates the code inside the brackets, and then it tests the expression again. This expression can be anything from "true", to "i<100", or even a function (that can do absolutely anything). in the second case it's "1::second=>now", which will return true when 1 second have passed.
Hi Gastern, I understand how 'while' works on programming languages. What confuses me in chuck is the meaning of the expression (dur => now) 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 just 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 traditional programing language control structures. Sorry for the lengthy and confusing email! JPa.
"The fact that this expression evaluates to
true after the waiting time is what confuses me, compared to traditional
programing language control structures."
I've been following this thread and now I'm also very curious
to know the answer here. It's one thing to call for a delay
in the main loop, but quite another to add a delay as condition.
- fadereu
On Nov 11, 2007 7:34 AM, Juan-Pablo Caceres
Martin Ahnelöv wrote:
lör 2007-11-10 klockan 09:29 -0800 skrev Juan-Pablo Caceres:
Hi there,
I have another question concerning time logic. The following 2 examples are similar:
// 1) while (true) { <<<"HELLO">>>; 1::second => now; }
// 2) while (1::second => now) { <<<"HELLO">>>; }
So the question is, is the second case using some syntactic sugar? Because conceptually it doesn't sound right to me. Maybe the chuck gurus out there can make more sense of it?
Well, it's pretty easy, actually. While takes an expression, and if that expression returns true, it evaluates the code inside the brackets, and then it tests the expression again. This expression can be anything from "true", to "i<100", or even a function (that can do absolutely anything). in the second case it's "1::second=>now", which will return true when 1 second have passed.
Hi Gastern,
I understand how 'while' works on programming languages. What confuses me in chuck is the meaning of the expression (dur => now)
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 just 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 traditional programing language control structures.
Sorry for the lengthy and confusing email! JPa.
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
-- ------- -.- 1/f ))) --. ------- ... http://www.algomantra.com
sön 2007-11-11 klockan 08:56 +0530 skrev AlgoMantra:
"The fact that this expression evaluates to true after the waiting time is what confuses me, compared to traditional programing language control structures."
I've been following this thread and now I'm also very curious to know the answer here. It's one thing to call for a delay in the main loop, but quite another to add a delay as condition.
- fadereu
On Nov 11, 2007 7:34 AM, Juan-Pablo Caceres < jcaceres@ccrma.stanford.edu> wrote: Martin Ahnelöv wrote: > lör 2007-11-10 klockan 09:29 -0800 skrev Juan-Pablo Caceres: >> Hi there, >> >> I have another question concerning time logic. The following 2 examples >> are similar: >> >> // 1) >> while (true) >> { >> <<<"HELLO">>>; >> 1::second => now; >> } >> >> // 2) >> while (1::second => now) >> { >> <<<"HELLO">>>; >> } >> >> >> So the question is, is the second case using some syntactic sugar? >> Because conceptually it doesn't sound right to me. Maybe the chuck gurus >> out there can make more sense of it? >> > > Well, it's pretty easy, actually. While takes an expression, and if that > expression returns true, it evaluates the code inside the brackets, and > then it tests the expression again. This expression can be anything from > "true", to "i<100", or even a function (that can do absolutely > anything). in the second case it's "1::second=>now", which will return > true when 1 second have passed.
Hi Gastern,
I understand how 'while' works on programming languages. What confuses me in chuck is the meaning of the expression (dur => now)
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 just 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 traditional programing language control structures.
Well, the way I think of it is that the keyword now is just a function or something, so passing dur to it is just like passing a float to Osc.freq. Does that make things clearer? Gasten
Juan-Pablo, 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 just 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 traditional 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 run.) 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... spencer
Sorry for the lengthy and confusing email! JPa.
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
participants (4)
-
AlgoMantra
-
Juan-Pablo Caceres
-
Martin Ahnelöv
-
Spencer Salazar