[MWForum]When or if

Jeff R mwforum@lists.mathcats.com
Wed, 13 Nov 2002 13:01:56 +1100 (EST)


On Tue, 12 Nov 2002, Wendy Petti wrote:
> > Another way of implementing the effects of WHEN could be:
> > LAUNCH [FOREVER [IF condition [instruction list]]]
> 1) [the quibble]
> Using "launch" with "forever" is redundant. These two commands
> function the same:
Well yes, but the above is not a 'real' implementation of WHEN. I
redundantly used LAUNCH to emphasise that an independent parallel process
is being launched, which is what happens with FOREVER as well as WHEN
If the instructionlist includes CANCEL, as it syntactically should, then
it will produce Turtle behaviour the same as your example below. However,
to use CANCEL properly involves an impossible endless recursion. CANCEL
with no argument will work, but produce an error message. Hence the
existence of WHEN as a Primitive

Jeff

> Contrary to your assertion above, the sample commands I just gave do 
> NOT work the same as a replacement for this "when" command:
> when [colorunder = 0] [fd 10 bk 10]
> 
> In the commands in (1) above, the turtle vibrates back and forth; in (2) the turtle simply moves fd 10 bk 10 once.
> 
> If you place the "when" command inside a procedure named "test" and place a "test" button on your page (setting it to "once") and click it, the turtle will run the second instruction just once if the the first instruction list is true, but the button will stay clicked on, because the computer is still evaluating the "when" condition.  
> 
> If you set the button to "many times," the when command will run until we get an error message:  I can't start a new prrocess in "test" - and the turtle will stop vibrating (all processes will stop running), but the button will still be clicked on until pressed again.
> 
> Just as we should not use a "when" command with a button set to "many times," we should never use "when" inside of a "forever" instruction, since "when" is constantly being evaluated and does not stop running; the computer will launch more and more and more of the "when" condition until it produces an error message.  It is actually quite interesting to watch this happen if you have MWPro and look at the "processes" tab while running a doomed instruction such as:
> 
> forever [when [colorunder = 0] [fd 10 bk 10] ]
> 
> 3) Putting this all together:
> 
> 	a) Using "if" with a "forever" command (or a button set to "many times") makes sense when you want the "if" command to be implemented endlessly as long as the condition is true.  
> 
> 	b) Using "when" makes sense when you want the condition to be constantly evaluated but implemented only once when the condition is finally met.  
> 
> 	c) Using "when" with "forever" (or a button set to "many times") never makes sense.
> 
> Wendy Petti
> OpenWorld Learning
> http://mia.openworldlearning.org
> Math Cats
> http://www.mathcats.com
> 
> _______________________________________________
> MWForum mailing list
> MWForum@lists.mathcats.com
> http://lists.mathcats.com/mailman/listinfo/mwforum
>