Saturday, December 30, 2017

Singing with Loops

Loops are programming constructs for repeating a set of instructions until a given termination a criterion is met.

Ann cheered at the sight of the brightly lit inn. A sign by the door read, “One night only! The world famous bard Larry de Loop!” After another full day of walking, she felt tired and seemed no closer to finishing her quest. But, at least, she’d found a place to rest. And some cheerful music might help take her mind off her quest.

Ann chose a small table by the stage. After ordering a bowl of Surprise Stew, regrettably the only item on the menu, she settled in for the show.

Though unable to play even a basic tune on his accordion, Larry de Loop was the most enthusiastic bard Ann had seen. He belted out three simple songs, then asked for requests.

“The Ballad of Lady Algorithm,” called Ann, wanting to hear the tale of her favorite adventurer.

Larry looked surprised. “I’m sorry miss. I only sing songs in loop form.”

“Loop form?” Ann asked. “I’ve never heard of that style of music.”

“It’s quite popular in the North. The songs have to use either FOR loops or WHILE loops. Loops are constructs for repeating things until some conditions are met.”

“What?” asked Ann. Naturally she was familiar with the concept of loops. They were a basic building block of algorithms. She used loops in archery (while you have arrows, shoot at the target), cooking (stir for two minutes), counting coins (for each coin, add its value to the total), and even walking (while I’m not there yet, take another step). But she’d never heard of loop-based music.

“I mostly sing in FOR loops,” explained Larry. “A FOR loop iterates over a set number of things. 99 Bottles of Beer on the Wall uses a FOR loop. FOR each number of bottles from 99 down to 1, sing a verse. Or perhaps you’ve heard of old McDonald’s Farm? FOR each animal on the farm, sing about the cute noises it makes. Or—“

“Aren’t those songs all quite repetitive?” Ann interrupted. Of course, repetition was precisely the function of a loop.

Larry smiled broadly. “That’s why I also use a WHILE loop. WHILE loops repeat a set of actions until a condition is met. In my case I always use the same loop: WHILE no one has thrown a tomato at me, keep singing. So once the first tomato is thrown, I know it's time to stop.”

Thursday, December 21, 2017

Variables, IF statements, and Magic Boots

A variable represents a location in the computer’s memory where you can store a single piece of data. You can use the variable’s name to look up, set, or modify the value stored in that location.

Ann jolted in surprise as a loud tone cut through the silence. She quickly tapped her boots together, resetting them before they could blare their pre-recorded message. She mumbled a few insults at her footwear. Did they have to alert her after every mile?

Ann realized this was the eighth warning chime today. Had she walked eight miles already? She’d been so lost in her thoughts, that she hadn’t been paying much attention to the journey itself. She was now three days into her quest to save the kingdom and she still had no idea what to do. Why couldn’t the seers have been more specific than, “There is a darkness coming. Princess Ann must travel forth alone to save the kingdom.” Maybe they could have told her what the darkness was or, at least, hinted at which direction to travel. Stupid vague prophecies.

Ann looked down at her boots and debated for the hundredth time whether they were worth the annoyance. The boots had been a birthday gift from Marcus, the royal wizard. They represented his first foray into variable magic—a form of magic that allowed an object to store a single piece of information. In this case, her boots’ variable (helpfully called dist) stored the distance she traveled. After each step, dist increased by the length of the step.

dist = dist + step_length

Initially the boots had provided wonderful entertainment. Ann measured everything. She measured the length and width of her room, the distance to the kitchen, the distance to all ten of the castle’s bathrooms, the circumference of the castle wall, and the diameter of Fido’s turtle pond (the boots were, of course, water proof). She’d even annoyed the castle architect by noting the courtyard was six inches shorter than advertised.

After every trip she’d read the variable’s value from her left heel. Then she’d click the heels together to set the distance back to zero.

IF heels clicked: set dist = 0

If she also used her watch to track the time, she could even compute her speed:

Average speed = dist / time

Variable magic provides such wonderful power.

Unfortunately, Marcus had gone overboard. In addition to the variable itself, he added his own IF-statement based enhancements. After walking a mile the boots would loudly recommend that she take a break:

IF dist > 1 mile: Alert the wearer to take a break

The message, which sounded like the castle herald with hiccups, grated on her nerves. Worse, Marcus hadn’t properly thought through the details of his IF statement. The statement only checked if dist was greater than a mile. So once she’d walked a mile, the boots would “helpfully” alert her to this fact after every single step. Ann quickly learned to reset the distance to zero the moment she heard the chime.

With a sigh, Ann decided to leave on the boots. While they were extremely annoying, at least they tracked useful information. Marcus had once confided that his original design enabled the boots to track their smelliness. She shuddered as she considered what that alert might say.


There are now three books out in the Computational Fairy Tales universe!  See the books page for more information.