And a partridge in a pear tree
The traditional verses of The Twelve Days of Christmas lend themselves nicely to automatic generation in Logo using a pair of recursive procedures both of which aim at a numerical target. One adops wind-up recursion and the other wind-down. (If you don't know The Twelve Days of Christmas, or need to refresh your memory, look here.)
As the story has it, on each day of the twelve days after Christmas, the 'true love' sends his (or her) lover a progressively larger and larger collection of gifts. On the first day, a partridge in a pear tree, on the second day, two turtle doves and a partridge in a pear tree, on the third, three French hens, two turtle doves and a partridge in a pear tree and so on . . . .
In order to sing or recite the verses you need to know that:
Putting it simply, you need to be able to count up from 1 to 12 to keep track of the days and count down from today to day number 1 in order to list the gifts in their proper order.
- You start with the verse for the first day and finish with the verse for the twelfth day
- Each verse differs in its list of gifts. Each list starts with today's new gift and then repeats the gifts for each previous day in turn, finishing with the gift for the very first day. (Oh, yes! Another partridge!)
A logo recital
A fairly natural way to automate the recital of the Twelve Days of Christmas in Logo is
to use one recursive procedure to handle the succession of twelve verses and another
(called on by the first) to generate the appropriate list of gifts at each step. Suppose that we call the toplevel procedure twelve.days and its daughter gifts. The general pattern of the two interacting procedures might look like this - with a rough and ready English characterisation in italics to cover the bits of Logo still to be worked out:
to twelve.days :day
if :day > 12 [stop]
print (with a two-line intro) the list of gifts output by gifts :day
twelve.days :day + 1
to gifts :today
if :today = 0 [op ]
output a list comprising the gift for :today
followed by whatever you get from gifts :today - 1
Twelve.days will normally be started with its input set at 1 (setting the clock to
the first day) and, by adding 1 to :day on each call, will
wind up to the point where :day reaches 13
and the terminating condition is met. By contrast, gifts will be started with
its input set at whatever is today's numerical position and will wind down, subtracting one from :today at each step, until :today = 0.
If you would like to look at and maybe try out the system with the details filled in - click here.
Alternative approach tactics
You will come across many recursive procedures which use numerical counters to control events
precisely as in twelve.days or gifts, but the modifications to a numerical input when the recursive call is made are not restricted to incrementing and decrementing by 1. The value of the input can be raised (or lowered) at each step by whatever amount is appropriate - for example, by 2 or 10 or perhaps by some multiple of its present value . . . The only restriction is that it must be possible to express the modification generally since, clearly, it can be specified only once in the procedure definition. It is, of course, this maintenance of the pattern of change which gives the recursive process its particular direction and its momentum.
For the use of non-numerical targets try the next page.