Discuss Scratch

TPAL125
Scratcher
75 posts

Recursion. -BY ME

First up, what is recursion?

This.


Basically, it's where the define block goes inside itself, wrapping it up in many layers of blocks, without having to



… Do that.

The reason someone might do that is because the repeat block creates a slight delay which actually occurs every time its inside script has finished running, and that is to end its script and switch to another repeat or forever script.

Maybe.

I wrote:

By the way, I name that delay the LLLIBGBINIOIKV, and represent myself as the founder of it. You have no control over it.
Bwahahahahahahahahaha

The new def block is extremely useful, because it greatly shortens scripts in sprites and makes things much more understandable.
But that's not the reason why I like the def block. It's because…

…
…
…
Recursion (hence the subject title).

If you've wanted to load up a list or test multiple variables or something, you can use the def block to go from this:


To this.

… That shrinks up the scripts pretty fast.

If it's in a program with extra sprites and dozens of scripts it can use it's triple speed to run around the world and trip forever and repeat blocks from behind.

Pros:

•Fast (if extra scripts are included in the program).

•Gives understanding magic.

•Insane strange recursion magic which is fun.

Wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee goes the variables.


So why am I asking for your opinion on this topic?
It's because there are some negative effects to recursion that you should know.

Cons:

•If you recurze too far or too fast this happens:
(happens at approximately 11,000,000 recursions.)

That's not good.

•Once you're done with scratch the program looks more like a labyrinth than a program (experience - that's one word).

•If your project happens to have a def recursion block and also happens to have only about 1 - 4 repeat or forever scripts, I'll let you know that in that case repeat signs beat def blocks with twice the power.

I wrote:

Well, that's too bad.
What kind of paint tool is that guy using?
*sigh*… Talking to myself again.

… So what were we up to?

The pros and cons have been shown out, I've talked about recursion so now my part is done!



Not yet.
Please post what you think about def recursion and whether you think they're worth using or not. And while you're here go ahead and check these small projects about recursion while you're here:

-Def recursion vs repeat block (Clicky)
-How far can your computer recurze? (Also clicky)

I'm done here.



____________________________________________________________________________________________

Last edited by TPAL125 (Sept. 7, 2013 12:41:22)

scubajerry
Scratcher
1000+ posts

Recursion. -BY ME

Recursion is fun, recursion is wonderful. I highly recommend it. In some cases, it can have some performance overhead. I wrote this just to see if my hypothesis is accurate. Big disclaimer - I know you don't need recursion to make a number list and I know it is not a particularly good example.

http://scratch.mit.edu/projects/12224444/
ErnieParke
Scratcher
1000+ posts

Recursion. -BY ME

Why is everyone using George Street Shuffle in their projects?

Anyway, I haven't used recursion as of yet, so I cannot put a claim on which is faster, but I think the define block method is certainly neater/more advanced. The repeat block on the other hand is easier to understand, and it's what I've been using so far for anything recursion-related. I'm actually considering switching to def related recursion in my Fractal Maker now that you've brought this up…

With regards,

ErnieParke
kayybee
Scratcher
1000+ posts

Recursion. -BY ME

Recursion can also work by broadcasts, which may differ in speed from definitions or repeats. Maybe you could test that.
scimonster
Scratcher
1000+ posts

Recursion. -BY ME

Recursion is a very powerful tool, when used properly. Just ask @bharvey about that…
But the best power of recursion is when you have recursive reporters. Take a look at a block like this:
define letters (start) to (finish) of (word) // pretend this is a reporter
if <(start) = (finish)> then
report (letter (start) of (word))
else
report (letters ((start)+(1)) to (finish) of (word))
In my mind, that's much more elegant than:
define letters (start) to (finish) of (word) 
set [result v] to []
set [i v] to (start)
repeat ((finish) - (start))
set [result v] to (join (result) (letter (i) of (word)))
end
scubajerry
Scratcher
1000+ posts

Recursion. -BY ME

I think this is an excellent example of recursion. In this case, you don't know ahead of time how many small list you need to sort. You just keep having QUICKSORT caling QUICKSORT until it is done. http://scratch.mit.edu/projects/11888406/

As I type this, I suspect recursion is probably the best answer for responding to collision detection in platforms. I will put that on my list of things to play with.
TPAL125
Scratcher
75 posts

Recursion. -BY ME

ErnieParke wrote:

Why is everyone using George Street Shuffle in their projects?

I… Don't…. Know…


Kevin Macleod is forcing me to do it… I think…



I still don't know.
scubajerry
Scratcher
1000+ posts

Recursion. -BY ME

And thanks for bringing up recursion and making me think about it. I used it in this project. http://scratch.mit.edu/projects/12240264/
ProdigyZeta7
Scratcher
1000+ posts

Recursion. -BY ME

scimonster wrote:

define letters (start) to (finish) of (word) 
set [result v] to []
set [i v] to (start)
repeat until <(i) > (finish)> //Better
set [result v] to (join (result) (letter (i) of (word)))
change [i v] by (1) //You forgot this
end
Fixed.

Now I need to figure out a way of recursion for string calculation…

Last edited by ProdigyZeta7 (Sept. 8, 2013 21:41:02)

ErnieParke
Scratcher
1000+ posts

Recursion. -BY ME

TPAL125 wrote:

ErnieParke wrote:

Why is everyone using George Street Shuffle in their projects?

I… Don't…. Know…


Kevin Macleod is forcing me to do it… I think…



I still don't know.
Hmm… maybe he has some brain-wave control-o-matic hidden somewhere in his house?

Anyway, just to say, earlier today I had implemented recursion in my project and it actually seems to be going slower because of it (which is why I removed it). From what I've seen, it looks like recursion is faster then a repeat block if Scratch isn't facing any big CPU loads, but if Scratch is lagging, recursion isn't such a good idea anymore.

With regards,

ErnieParke
TPAL125
Scratcher
75 posts

Recursion. -BY ME

ErnieParke wrote:

_____________________________________________________________________________________________________
Anyway, just to say, earlier today I had implemented recursion in my project and it actually seems to be going slower because of it (which is why I removed it). From what I've seen, it looks like recursion is faster then a repeat block if Scratch isn't facing any big CPU loads, but if Scratch is lagging, recursion isn't such a good idea anymore.

With regards,

ErnieParke

But how do you know that recursion is slower than looping? - in the lagging part I mean.


Have you already tested it?
DadOfMrLog
Scratcher
1000+ posts

Recursion. -BY ME

Regarding recursion, it's something I would normally avoid if something can be done reasonably simply without it. Since it has to store some kind of current ‘environment’ and ‘return’ information each time it enters a new level so it knows how to ‘jump back’ up to the previous level, it's eventually got to have some degree of negative impact - your memory is limited, so too many will of course cause a crash.

Also due to this extra overhead, I doubt recursion would normally be quicker than a loop. Note too, for recursion you need a condition test to stop it (otherwise infinite -> crash, as above). But a repeat loop can (often) be a known number of times (repeat N), which is significantly quicker than interpreting the condition script every time at the next recursion level.


ProdigyZeta7 wrote:

[…scimonster's script, fixed…]

Now I need to figure out a way of recursion for string calculation…

As I mentioned above, “repeat ( )” is a faster than “repeat until <>” -because the interpreter doesn't have to continually interpret the condition script. It's worth using it if you can, so I'd suggest sticking with scimonster's original script there (but with the necessary +1 correction, and the increment of variable “i”, as PZ7 added):

define letters (start) to (finish) of (word) 
set [result v] to [] // note this is blank, not a space!
set [i v] to (start)
repeat ( ( (finish)-(start) ) + (1) ) // needs an extra one added
set [result v] to (join (result) (letter (i) of (word)))
change [i v] by (1)
end

Last edited by DadOfMrLog (Sept. 8, 2013 23:12:55)

turkey3
Scratcher
1000+ posts

Recursion. -BY ME

ProdigyZeta7 wrote:

scimonster wrote:

define letters (start) to (finish) of (word) 
set [result v] to []
set [i v] to (start)
repeat until <(i) > (finish)> //Better
set [result v] to (join (result) (letter (i) of (word)))
change [i v] by (1) //You forgot this
end
Fixed.

Now I need to figure out a way of recursion for string calculation…
What type of string calculation?
ProdigyZeta7
Scratcher
1000+ posts

Recursion. -BY ME

turkey3 wrote:

ProdigyZeta7 wrote:

scimonster wrote:

define letters (start) to (finish) of (word) 
set [result v] to []
set [i v] to (start)
repeat until <(i) > (finish)> //Better
set [result v] to (join (result) (letter (i) of (word)))
change [i v] by (1) //You forgot this
end
Fixed.

Now I need to figure out a way of recursion for string calculation…
What type of string calculation?
ask [Enter an expression to calculate] and wait
Calculate (answer)
ask (result) and wait

define Calculate (string)
separate into two (string)
??? //Um...
if <(op) = [+]>
set [result v] to ((num1) + (num2))
end
//and so on...

define separate into two (string)
something to separate into two strings from the "pivot" operation
??? //Absolutely clueless here
ErnieParke
Scratcher
1000+ posts

Recursion. -BY ME

TPAL125 wrote:

ErnieParke wrote:

_____________________________________________________________________________________________________
Anyway, just to say, earlier today I had implemented recursion in my project and it actually seems to be going slower because of it (which is why I removed it). From what I've seen, it looks like recursion is faster then a repeat block if Scratch isn't facing any big CPU loads, but if Scratch is lagging, recursion isn't such a good idea anymore.

With regards,

ErnieParke

But how do you know that recursion is slower than looping? - in the lagging part I mean.


Have you already tested it?
Yes, I have tested it. From what I saw, without recursion, I got a rendering time of 15- seconds. With the recursion, I got 17+ seconds. That might not seem like much, but in some projects, it can mean quite a bit. And because of it, I was able to improve the iteration depth of the Mandelbrot set from 20 to 30. (my main goal is speed, though I like some quality)

With regards,

ErnieParke
TPAL125
Scratcher
75 posts

Recursion. -BY ME

DadOfMrLog wrote:

Regarding recursion, it's something I would normally avoid if something can be done reasonably simply without it. Since it has to store some kind of current ‘environment’ and ‘return’ information each time it enters a new level so it knows how to ‘jump back’ up to the previous level, it's eventually got to have some degree of negative impact - your memory is limited, so too many will of course cause a crash.
______________________________________________________________________________
Also due to this extra overhead, I doubt recursion would normally be quicker than a loop. Note too, for recursion you need a condition test to stop it (otherwise infinite -> crash, as above). But a repeat loop can (often) be a known number of times (repeat N), which is significantly quicker than interpreting the condition script every time at the next recursion level.
______________________________________________________________________________

It's not. In fact, you actually need some sort of lag I think, for it to ‘speed up’ .

Last edited by TPAL125 (Sept. 9, 2013 07:25:51)

TPAL125
Scratcher
75 posts

Recursion. -BY ME

ErnieParke wrote:

TPAL125 wrote:

ErnieParke wrote:

_____________________________________________________________________________________________________
Anyway, just to say, earlier today I had implemented recursion in my project and it actually seems to be going slower because of it (which is why I removed it). From what I've seen, it looks like recursion is faster then a repeat block if Scratch isn't facing any big CPU loads, but if Scratch is lagging, recursion isn't such a good idea anymore.

With regards,

ErnieParke

But how do you know that recursion is slower than looping? - in the lagging part I mean.


Have you already tested it?
Yes, I have tested it. From what I saw, without recursion, I got a rendering time of 15- seconds. With the recursion, I got 17+ seconds. That might not seem like much, but in some projects, it can mean quite a bit. And because of it, I was able to improve the iteration depth of the Mandelbrot set from 20 to 30. (my main goal is speed, though I like some quality)

With regards,

ErnieParke

Have you tested both on making a list? If you have, show the list, and hopefully that's enough to make the recursion faster.
ErnieParke
Scratcher
1000+ posts

Recursion. -BY ME

TPAL125 wrote:

ErnieParke wrote:

TPAL125 wrote:

ErnieParke wrote:

_____________________________________________________________________________________________________
Anyway, just to say, earlier today I had implemented recursion in my project and it actually seems to be going slower because of it (which is why I removed it). From what I've seen, it looks like recursion is faster then a repeat block if Scratch isn't facing any big CPU loads, but if Scratch is lagging, recursion isn't such a good idea anymore.

With regards,

ErnieParke

But how do you know that recursion is slower than looping? - in the lagging part I mean.


Have you already tested it?
Yes, I have tested it. From what I saw, without recursion, I got a rendering time of 15- seconds. With the recursion, I got 17+ seconds. That might not seem like much, but in some projects, it can mean quite a bit. And because of it, I was able to improve the iteration depth of the Mandelbrot set from 20 to 30. (my main goal is speed, though I like some quality)

With regards,

ErnieParke

Have you tested both on making a list? If you have, show the list, and hopefully that's enough to make the recursion faster.
Tests doesn't always have to be on making a list. Recursion can be used elsewhere, especially in the Mandelbrot set, where recursion is a key factor in its making. And I have already tested with times. The results are above in my earlier post, and they show that recursion is slower when Scratch is under a heavy CPU load. (and to correct one of my mistakes, I'm using a repeat until, not a repeat, which lags more then a repeat, but not more then recursion)

With regards,

ErnieParke
jgatcomb
Scratcher
90 posts

Recursion. -BY ME

TPAL125 wrote:

First up, what is recursion?

This.


Basically, it's where the define block goes inside itself, wrapping it up in many layers of blocks, without having to



… Do that.

Recursion is an extremely powerful tool to have. With that said, I disagree with the conclusions you are making about it being faster than an iterative solution.

I would not have written that long if/else chain. Instead I would have written:

repeat until <not recursion> = 3000 {
next costume
change recursion by 1
}

This points out another issue. Your iterative version and your recursive version aren't doing the same unit of work (the iterative version changes costumes every time recursion changes by 1 but your recursive version doesn't change until it reaches 3000).

I am not saying there aren't situations where recursion is faster. There are programming languages (functional languages like Haskell) where there aren't any loops (you have to use recursion). These languages have been optimized to make recursion fast.

In most imperative languages (like Scratch), there is a lot of overhead to set up recursion so I suspect more often then not, when you find it faster it is because the iterative and recursive implementations aren't equivalent.

There are some problems that it is easier to write a recursive version than an iterative version (Towers of Hanoi, Depth First Search, etc) but the iterative versions are almost always faster.

Cheers,
Joshua



TPAL125
Scratcher
75 posts

Recursion. -BY ME

jgatcomb wrote:

TPAL125 wrote:

First up, what is recursion?

This.


Basically, it's where the define block goes inside itself, wrapping it up in many layers of blocks, without having to



… Do that.

Recursion is an extremely powerful tool to have. With that said, I disagree with the conclusions you are making about it being faster than an iterative solution.

I would not have written that long if/else chain. Instead I would have written:

repeat until <not recursion> = 3000 {
next costume
change recursion by 1
}

This points out another issue. Your iterative version and your recursive version aren't doing the same unit of work (the iterative version changes costumes every time recursion changes by 1 but your recursive version doesn't change until it reaches 3000).

I am not saying there aren't situations where recursion is faster. There are programming languages (functional languages like Haskell) where there aren't any loops (you have to use recursion). These languages have been optimized to make recursion fast.

In most imperative languages (like Scratch), there is a lot of overhead to set up recursion so I suspect more often then not, when you find it faster it is because the iterative and recursive implementations aren't equivalent.

There are some problems that it is easier to write a recursive version than an iterative version (Towers of Hanoi, Depth First Search, etc) but the iterative versions are almost always faster.

Cheers,
Joshua




You're talking to someone who's already tested it out. : D

Yeah, It's true recursion normally bets beaten by everything in speed, but if you test it out in a very laggy project (Sorry, not available anymore) you'll find it'll sometimes (yeah, just sometimes) beat repeating.


Unfortunately, it works in laggy scripts. That means it probably won't work out well in a project whose point is for speed.

However, leaving the topic of speed, you can do things with recursion that you can't do with other scripts.




You can't make two recursion scripts like this using ‘repeat’ or ‘repeat until’



Some Guy wrote:


Replace the define blocks with broadcast.

Oh.


Okay…

_________________________________________________________________________________________

Hi there. What are you reading this for?

Last edited by TPAL125 (Sept. 11, 2013 11:26:14)

Powered by DjangoBB