GO TO XO

On the XO Laptop

Python-XO: The Farmer in the Dell

with one comment

[First published as xo-python: The Farmer in the Dell on November 29, 2007.]

Programming is a form of writing. Poets write poetry, novelists write novels, dramatists write plays, and programmers write computer software code.

Programming has its own rules and practices, as do the writing of plays and poems.

Programmers tend to get paid more than poets and dramatists because of the way in which their writing is used: to instruct computers how to operate.

For example, some programmers, especially some folks who have worked — or are currently working — for Microsoft and Google have made a lot of money with their writing. This doesn’t mean they are better writers. It just means they get paid more for their writing.

However, some people with programming skills believe there is a special quality to their writing because of the monetary value of code, but that is the subject for other posts.

Here is a small program, written in the Python programming language, to indicate the flavor of the writing that is computer code.

We shall take as our starting point a verse of unknown origin, but one that I expect most of the people reading this have heard before, most likely when they were children. It is the verse The Farmer in the Dell. It begins with:

The farmer in the dell
The farmer in the dell
Hi-ho, the derry-o
The farmer in the dell

The farmer takes a wife
The farmer takes a wife
Hi-ho, the derry-o
The farmer takes a wife

The wife takes a child
The wife takes a child
Hi-ho, the derry-o
The wife takes a child

The child takes a nurse
The child takes a nurse
Hi-ho, the derry-o
The child takes a nurse

The nurse takes a cow
The nurse takes a cow
Hi-ho, the derry-o
The nurse takes a cow

Here is a Python program that prints out that text when it is executed:


def lin(this, phrase, that):
    print "The",  this,  phrase, that;
def hiho():
    print "Hi-ho, the derry-o";
def verse_one(this,phrase, that):
    lin(this,phrase, that); lin(this,phrase, that); hiho(); lin(this,phrase,that); print ;

def verse_two(this,phrase, that):
    for i in range(1, 4):
        if i==3:
            hiho();
        else:
            lin(this,phrase,that);


verse_one("farmer", "in the", "dell");
verse_one("farmer", "takes a", "wife");
verse_one("wife",   "takes a",  "child");
verse_one("child",  "takes a", "nurse");
verse_two("nurse",  "takes a", "cow");

This shows how small programs can capture some structure in a precise way. In this case, the program is shorter than the output it generates, and just the part I have written serves as a guide to completing the program so that it prints out the complete song.

This example also shows there can be different ways to accomplish the same purpose. Both “verse_one” and “verse_two” are instances of what are called “procedures.” Each generates the same output, but they differ in how they do it.

We’ll learn more about Python in upcoming posts.

Advertisements

Written by daveshields

December 12, 2007 at 3:09 pm

Posted in xo-laptop

One Response

Subscribe to comments with RSS.

  1. […] wrote in a previous post, Python-XO: The Farmer in the Dell that programming is just a form of writing. The key point I wish to make here , especially in […]


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: