Wednesday, March 30, 2011

Loops and Making Horseshoes

Loops, such as the FOR loop or the WHILE loop, are programing constructs for repeating a set of instructions until some termination criteria is met. Two primary things define a loop: 1) What you do inside the loop, and 2) the conditions to stop looping.

Drex's new apprentice, Simon, was not working out. In fact, Drex never had a worse apprentice in his thirty-five years as a blacksmith. Simon could barely lift the hammer, let alone swing it with sufficient force to shape metal. However, worse than that Simon also lacked the necessary intelligence to carry out even simple tasks. Had it not been for his diminutive size, Drex might have thought that Simon was actually an ogre.

Drex found himself constantly repeating instructions:
“Now, hit the metal again.”
“And again.”
“And again...”
Drex's patience was wearing very thin. Drex hated repeating himself.

Finally, Drex decided to try an experiment. “Simon, hit the metal twice.” he commanded.

Clank. Clank. Simon complied.

“Now turn it over and hit it three times.” Drex commanded.

Simon flipped the deformed looking horseshoe with the tongs, and he hit it once. Then he paused. He looked back at Drex. Simon looked confused.

Drex sighed loudly. Was that really too much for Simon to handle? The boy was hopeless.

“It is a loop!” proclaimed Drex loudly. He knew that Simon would not understand, but at least shouting made Drex feel better. “A simple, simple loop.”

“A loop?” asked Simon.

“Yes. Haven't you ever heard of a loop?”

Simon shook his head sadly.

Drex realized that they had hit the core of the problem. How could Simon function as a reasonable blacksmith without understanding loops? Then again, Drex had no idea how Simon could function as a human without understanding loops.

“A loop is defined by two things: something to do and a way to know when to stop doing it. You keep doing that one thing over and over until you stop.” explained Drex calmly.

Simon stared back blankly.

“Think about a one mile race.” Drex suggested. “You run around the track, until you have gone a mile. That is four laps, right? So, the running is the thing you do and having run a full mile is the criteria for stopping. The track even looks like a loop.”

“I run until someone tells me to stop.” declared Simon loudly.

“Of course you do.” muttered Drex.

“In this case,” continued Drex, “I want you to keep hammering that horseshoe until it is flat. As soon as it is flat, you can stop. WHILE the horseshoe is not flat, hit it with the hammer.”

“Okay.” agreed Simon happily. He promptly set about hitting the horseshoe over and over again until it was flat. Then he stopped. By the end Simon was breathing heavily from the effort, but the horseshoe was completely flat.

Drex was stunned. How had Simon understood that?

“Good. Now go get the coals hot.” Drex commanded.

Simon looked confused again.

Drex sighed. “It is another loop. Pump the bellows 10 times. FOR each number that you count from 1 to 10, give the bellows a pump.”

“Okay.” Simon again got to work, pumping the bellows exactly ten times. He counted loudly each time.


Over the course of a week, Drex determined that Simon would repeat tasks if they were well specified in a loop. He would tell Simon exactly what task to repeat and exactly how long to repeat it. Sometimes he would simply tell Simon to count up to a certain number. Other times he would phrase it like a WHILE loop, telling Simon to continue doing something until a goal had been met.

Simon responded amazingly well to these structured commands. The blacksmith's shop was filled with the noise of Simon cheerfully counting and hammering. “One… bang… two... bang…”

Eventually, Drex was even able to move onto nested loops, issuing instructions such as “WHILE the sword is not thin enough: Turn it over and FOR each number from 1 to 5, hit it with the hammer.” And Simon would happily go about banging the sword into shape while turning it over after every five hits.

Unfortunately, this formalized approach only worked to a point. Disaster finally struck when Drex tried to teach Simon about multi-threading. As Drex and Simon stood outside the burning blacksmith's shop, Drex admitted defeat. Before leaving town for an open blacksmith's position in Garroow, he set Simon up with a better matching job -- counting laps for runners on the local track team.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.