I want to implement a recursive algorythm that returns a modified version of the input data without modifying it directly, by returning a copy of it, but there are mutiple of these modified versions at once so i cant use a global variable.
You can use script variables script variables ((a)) @> . Script variables only exist for that script, aka custom block definition.
script variables ((a))
or...
(Right-click the hat of a custom block script)
Are you talking about block variables? It's hard to tell in that picture, because "script pic" is selected.
Yes, sorry, I didn't know how exactly to show it, and "script pic..." wouldn't deselect while taking the screenshot.
Use arrow keys to navigate to the "block variables".
However, block variables are not ideal for
because the block that is being called by itself will share the variable with all other calls of the recursion (because it is the same instance of the block).
Thanks to the reply, do you know if there is another way of doing it, I though about using JavaSrcipt, but I'd prefer a solution which doesn't involve JS.
What's your problem?
Do you want to pass a partial result to a recursive call? So you need a helper with an extra parameter, or a variadic optional parameter.
Or you learned, the hard way, that mutating a list passed as a parameter mutates the original list?
There is that makes a deep copy of the list, thus "decoupling" the result from the source.
There are also some "class" or static variables invented by forum members, i.e. variables persisting its value between calls, but this cure may be worse than the disease you are fighting
why?
By using script variables ((a)) @> .
It would help if you could explain a bit more about what you are doing.
Here are some examples of how to use the things suggested in this thread:
Script variables can be used to store information to be used later in the block's definition. Here is an example:
This saves the state of the pen and the location of the sprite and restores it after running.
Block Variables:
Block variables are unique because they are shared across every time the same instance of a block is called, but not every call on that block. This is more easily demonstrated like this:
Do you see what is going on here? Each time you see the block "counter", there is a different variable for that block.
This means that block variables are not very helpful when it comes to recursion:
I just whipped up a (probably not useful) recursive algorithm using block variables:
As you can see, this reports powers of two, skipping ahead the number of specified powers (here we skip 2, 8, 32, 128, and 512):
However, because the definition of the block shares the same instance of the recursion, another script can interfere/be interfered with:
By contrast, running the same script without first running the original example provides the intended result:
Well, actually, I just realized that it doesn't give the intended result, that my block doesn't actually work, but you get the picture of the issue with block variables in recursive algorithms. I give up on trying to figure out a recursive algorithm that might want block vars.
Linked Lists:
Besically, I want to implement the minimax algorithm, which takes a game state, mutates it and calls itself. The Problem is that i dont know how make the function call itself with a mutated version of the game state without using global variables.