And Even More Turtles!

(Wow, two posts in a just a few days! Must be something really exciting to report.)

And yes, it is something really exciting. This Turtle Graphics project has captured my attention, and I can’t help but work on it. Fortunately, there’s been lots of success, so I can write enthusiastically about recent milestones.

The other day (see last post) I got as far as creating a simple LOGO language parser that could move the turtle forward and backward, turn right or left, and loop a number of times. That was cool, but somewhat limited. Based on the various web articles I’ve found (and even some of the comments to my last post), there’s way more potential awaiting. All I need to do is get variables and procedures functioning, because then I can explore recursive algorithms and fractal geometry.

Procedures turned out to be fairly simple; the layout of the parser mechanism I built adapted nicely to interpreting from different “source” arrays. In my design, the LOGO program is parsed into a String array, which is passed to the Parser with an index pointer saying where to translate next. A “pre-parser” would scan for functions (code that begins with “TO” and ends with “END”), and would copy the function to another array before removing it from the program. Thus, when the parser encounters a call to a function, the code merely locates the function “code” in the other array and calls the parser with it.

Variables were initially simple … until they got complicated. Well, okay, things were fine until I got into recursion. Without recursion, the code that handled variables was straightforward: As the parser encounters a variable, it would add the variable to an array. Each time that variable gets referenced elsewhere it gets retrieved from the array. In effect, every variable was global. No problem.

One of the websites I found ( shows this lovely fractal tree, and that was what I was using as a guide for my efforts. I quickly found (or should I say, remembered?) that recursive routines don’t work with global variables. Recursive routines need local (i.e., stack based) variables because each iteration of the recursive routine needs its own data. Figuring a solution to this took a day or two of thinking (no coding). Late last night it dawned on me: I store variables in an array, but that array can contain multiple copies of the same variable. So I quickly wrote a “CreateLocalVariable” function that forcibly adds another copy of a variable to the end of the variable storage array. The “variable retrieval” function scans the entire array, looking for the variable in question. If there happens to be multiple copies, it will report the last one it finds, which will in effect be the local variable! (The only caveat is that I need to remember to remove those “local” variables from the array when the procedure is done.)

Moments later, I had my fractal tree:

Fractal Tree

So now I have a functional Turtle Graphics / LOGO interpreter. There’s still more that could be added (changeable colors, text support, etc.), but that’s only if I want to turn this into a full blown effort. My intention was to provide high school freshmen with a chance to get a computer to “do what they tell it to do”, as a gentle way of introducing the idea of programming. I don’t think they’ll spend more than a day or two at the most on this, which raises the question of why bother with more.

But, yeah, I’ll probably add more later.


About Mr. I

After 17 years as a PC Software Engineer I gave it all up in 2000 to become a High School Computer Teacher
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s