Page 1 of 1

Reaching recursion limit

Posted: Sat Jul 20, 2013 8:44 pm
by JeffO2013
Hi everyone,

I have a function within my program that calls itself again if a certain condition is not met. I basically need it to pick 1 of five total images and display it on the screen (then the user gives some input) then pick another image to display that is not the same as the previous one and so on. I don't want the program to repeat an image until all the other images have been displayed.

This is the code I have to accomplish this so far:

Code: Select all

on getRandom
   put random(5) into arrayNumber
        
   if trialBlockArray[arrayNumber] is 1 then // if the number has been used 
      getRandom
   else
      put 1 into trialBlockArray[arrayNumber]   // tell the program that it has been used
      set the filename of img "target" to targetArray[arrayNumber] // put the target img in the appropriate place
      add 1 to getRandomComplete // used to reset the trailBlockArray so we can then restart after everything has been shown once
      if getRandomComplete is 5 then
         put 0 into getRandomComplete
         put 0 into trialBlockArray[1-5]
      end if
  end if
end getRandom
trialBlockArray[arrayNumber] is used to track whether the number 1-5 has been used previously. The program puts 0 into trialBlockArray[1-5] earlier in the code. getRandomComplete is used to reset the trialBlockArray[1-5] - in this case put 0 back into it so it can start over picking the images. It works well from what I tested, but it keeps running into a recursion limit.

Is there a way I can get around this or better logic I could be using?

Thanks!

Jeff

Re: Reaching recursion limit

Posted: Sat Jul 20, 2013 9:20 pm
by shaosean
To get around the recursion limits, look at using the send command..

Code: Select all

on getRandom
   put random(5) into arrayNumber
        
   if trialBlockArray[arrayNumber] is 1 then // if the number has been used 
      send "getRandom" to me in 0 seconds
   else
      put 1 into trialBlockArray[arrayNumber]   // tell the program that it has been used
      set the filename of img "target" to targetArray[arrayNumber] // put the target img in the appropriate place
      add 1 to getRandomComplete // used to reset the trailBlockArray so we can then restart after everything has been shown once
      if getRandomComplete is 5 then
         put 0 into getRandomComplete
         put 0 into trialBlockArray[1-5]
      end if
  end if
end getRandom

Re: Reaching recursion limit

Posted: Sun Jul 21, 2013 12:02 am
by JeffO2013
Thanks, but I figured it out. It was the logic in the put 0 into trialBlockArray[1-5]. I used a repeat loop to fix it like so:

repeat with x = 1 to 5
put 0 into trialBlockArray[x]
end repeat

Thanks again!

Jeff

Re: Reaching recursion limit

Posted: Sun Jul 21, 2013 4:38 am
by jacque
Here's a way that doesn't require you to keep track of used items:

Code: Select all

local sCurList
on getRandom
  if sCurList = "" then put "1,2,3,4,5" into sCurList
  put random(the number of items in sCurList) into arrayNumber
  set the filename of img "target" to targetArray[item arrayNumber of sCurList]
  delete item arrayNumber of sCurList
end getRandom

Re: Reaching recursion limit

Posted: Mon Jul 22, 2013 12:29 am
by JeffO2013
Thanks Jacque!

I'll give this a try. It definitely seems safer than the way I'm currently doing it since there is a chance that it could reach it's recursion limit normally (even though it is highly unlikely).

I'll let you know what I come up with.

Jeff

Re: Reaching recursion limit

Posted: Mon Jul 22, 2013 6:29 am
by shaosean
I believe the recommendation is to use send in time when trying to do something an unknown times in a loop as it completely removes itself from the recursion limits.. Use repeat when you have a known amount of times to loop or else put in a safety net to bail out of the loop..