PG: Change. I also really dispute leading people down the garden path. This is a common error, but we should not mandate it. If we're going to be didactic, we should not didact in the wrong direction.
I agree with Paul. This is not good pedagogy. I'd be find to have Alpha, Betsy, and Gamal run into and discuss this bug, but it's not ok to handhold kids into doing the wrong thing. --MF, 4/3/19
It seemed to make sense to replace all the look-alike numbered tree scripts with a single recursive script with the same structure:
...
But it didn't work:
tree
, the sprite draws smaller and smaller left branches until it seems to be just spinning around in one place. It never draws a right branch to finish the tree it's working on.
tree 1
, tree 2
, etc. blocks aren't all the same. The first one, tree 1
, is different; it just draws a line, no branches, and puts the sprite back where it started.
Alphie points to the tree 1
code and to the figure it draws.
tree
block has to do something different when the level = 1!
tree 3
with size 40.tree 2
with a smaller size.tree 2
, which is almost the same, will move, turn, and then call tree 1
, which just draws a line and goes back. Then, tree 2
tells the sprite to turn right 60° and draw another little tree 1
and.... tree 1
finishes, that lets tree 2
finish! So, it'll look like this when tree 2
finishes:tree 3
script turns right 60° and uses the whole tree 2
script again to create the other side. Then it'll turn back 35° and move backwards to the very beginning.tree 3
finishes. Now, about that "base case" thing for the recursive tree
script... The base case is the lowest level of the recursion, so that's like the tree 1
. tree 1
just draws a line and doesn't try to make another tree.tree
block to draw a line at level 1 instead of making more little trees forever.tree
block so it includes a base case to stop the script from calling itself forever. Here are two ways you could do that, and there are more. Use whatever makes most sense to you.tree level: 9 size: 50
. If it is now working properly, you should get a result like this:tree 1
produces just one stick. How many new sticks (branches) are produced by tree 2
? Then, how many new ones are produced by tree 3
? By tree 4
? Figure out how many new twigs are added at level 10. You can now see why it would take a long time to draw level 20.Recursive scripts call themselves. In order for them to stop, there must be some special case when they don't call themselves. We call that the base case, a simpler version of the script that doesn't call the block itself.
There is usually a conditional (like if-else
) with two cases: a base case for the lowest level that stops the recursion from going on forever and a recursive case that calls the script itself again and again at lower levels until it reaches the base case.
When a script does keep recursively calling itself forever (as when the sprite was just spinning around in one place), that's a bug, and we say the program is stuck in an infinite loop.
tree
block, to record the number of times that tree
is called when running different levels. Make a table. What pattern do you find?
level | count |
---|---|
1 | 1 |
2 | 3 |
3 | 7 |
4 | ? |
5 | |
6 | |
etc. | etc. |