Repeating a thyme task

About advanced scenes, and the Thyme scripting language used in Algodoo.

Repeating a thyme task

Postby FRA32 » Wed Aug 12, 2015 9:11 pm

I am using a variabel collection stored as array, and a random int generator always picks one of these values. Now it can happen that the source of these values sets them to undefined, thus making them unusuable. These should either be avoided by the random picked, or, what i thought of, everytime the random generator picks an undefined variable from the array, it should retry, until it successfully picks a defined variable. Is there a way to do this, or does a a effective workaround exist.
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: Repeating a thyme task

Postby homieeee » Thu Aug 13, 2015 9:43 pm

Since there are no proper code loops (while or a real for) in thyme you will have to use recursion.

Code: Select all
_pick = (n, list)=>{
    d = _random(n);
    (list(d) != undefined) ? {} : {_pick(n)};
    list(d)
}


Code: Select all
_random = (max)=>{
    math.toint(rand.uniform01 * max)
}


_random creates a random integer between 0 and max.

_pick when called uses _random to pick a spot in a list, then checks if the variable at that location in the list is defined. If not it calls _pick again and repeats till defined variable is found or stack overflows.

Any other questions just ask.
User avatar
homieeee
 
Posts: 14
Joined: Thu Oct 09, 2014 2:13 am

Re: Repeating a thyme task

Postby FRA32 » Thu Aug 13, 2015 10:51 pm

Nah, I am familiar with recursrion, it jsut didnt get in my mind ^^. Thanks for the help tho, now I can make my script fail-proof.
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: Repeating a thyme task

Postby griggy » Wed Dec 09, 2015 9:33 pm

Reading this forum it seems that many people do not understand recursion or why it is needed.
Seeing just the Thyme code is confusing to a novice. I will try to help.

Consider that you want simulate a walk to the shop. You know that that will involve taking 100 steps .

If you know about programming you immediately think: "loop". Depending on how old you are you either think
"Take one step; Am I there? If not repeat all the above", OR "Do while I am not there: take one step".

You can code that in most languages but NOT in Thyme! In Thyme you must think in a different way.
The task is to "walk from here to the shop". Do not think "loop". Instead think "my task is take one step
then walk from there to the shop". How does that help? Because "walk from THERE to the shop" is the same
as "walk from HERE to the shop" except that it needs one less step. We can use the same function for doing
both if we tell it how may steps to take. I will call it WalkNSteps. To walk to the shop we say WalkNSteps(100).

How do we define the function? By using our new way of thinking:

WalkNSteps = (N) => { N > 0 ? { TakeOneStep ; WalkNSteps(N-1)} : {} }

where N is the number of steps and TakeOneStep is a function you have already written to take ONE step. The
test for N > 0 means we stop if we have arrived (as indicated by 0 steps to take). Not a loop in sight.

This is called "recursion" because the function calls itself. The main disadvantage of using recursion is that
every time the function calls itself the computer has to store some information in memory - so that builds up
and can stop the script running. There are ways to get round that though. I think that N up to 100 should be okay.

If you have trouble with the above code try changing the the last 3 symbols to { false }} . I do not know why it
helps.
griggy
 
Posts: 6
Joined: Wed Dec 09, 2015 6:59 pm

Re: Repeating a thyme task

Postby FRA32 » Wed Dec 09, 2015 10:21 pm

I know what recursion is, I mentioned that above. I just forgot about that since I tend to use while loops in OTHER languages since recursion sometimes calls local variable madness. I have worked with recursion since a long time now("discovered" it myself) so I can use these with ease. Thanks for the answer anyways.

PS: I am not new to coding, check my scenes if you want to know my knowledge level ;)
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: Repeating a thyme task

Postby griggy » Thu Dec 10, 2015 2:51 pm

Hello FRA32. Sorry that I gave you the wrong impression. I was not addressing you in particular even though you were the original poster. I had been looking through the Thyme forum and saw that several people (not you) did not understand recursion, so I thought I would try to help them. I thought I would start a new thread, but found there already was one with my chosen title - your thread. So I added my post to that. I know that you are experienced in Thyme having read your other posts.
Best wishes.
griggy
 
Posts: 6
Joined: Wed Dec 09, 2015 6:59 pm

Re: Repeating a thyme task

Postby FRA32 » Thu Dec 10, 2015 3:40 pm

Okay, didn't think of the possibility that you were adressing the people who read this in the future. It's nice to see you think of others :)
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 13 guests