



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




One of the fundamental ideas of computer science is to divide a complicated problem into one or more simpler pieces, solving them, and using their solution to compute a solution to the original problem. When the simpler sub-problems are instances of the original problem, this technique is called recursion. The functional abstraction enables us to implement this method in functions that call themselves on simpler input somewhere in the function body.
A function is called recursive if the body of that function calls the function itself, either directly or indirectly. That is, the process of executing the body of a recursive function may in turn require applying that function again. Recursive functions do not use any special syntax in Python, but they do require some care to define correctly.
As an introduction to recursive functions, we begin with the task of converting an English word into its Pig Latin equivalent. Pig Latin is a secret language: one that applies a simple, deterministic transformation to each word that veils the meaning of the word. Thomas Jefferson was supposedly an early adopter. The Pig Latin equivalent of an English word moves the initial consonant cluster (which may be empty) from the beginning of the word to the end and follows it by the "-ay" vowel. Hence, the word "pun" becomes "unpay", "stout" becomes "outstay", and "all" becomes "allay".
Our goal in this section is to define a set of functions to produce the Pig Latin version of a word. We use strings to represent words, but we need a way of separately accessing the first letter of a word and the remaining letters. We can use the expression s[0] to obtain the first letter of a string s and s[1:] to get the remaining letters:
The bracket notation is very general and will be discussed at length in Chapter 2, but this is all we need for now. We can now define functions to produce the Pig Latin version of a word:
The idea behind this definition is that the Pig Latin variant of a string that starts with a consonant is the same as the Pig Latin variant of another string: that which is created by moving the first letter to the end. Hence, the Pig Latin word for "sending" is the same as for "endings" ( endingsay ), and the Pig Latin word for "smother" is the same as the Pig Latin word for "mothers" ( othersmay ). Moreover, moving one consonant from the beginning of the word to the end results in a simpler problem with fewer initial consonants. In the case of "sending", moving the "s" to the end gives a word that starts with a vowel, and so our work is done.
This definition of pig_latin is both complete and correct, even though the pig_latin function is called within its own body.
The idea of being able to define a function in terms of itself may be disturbing; it may seem unclear how such a "circular" definition could make sense at all, much less specify a well-defined process to be carried out by a computer. We can, however, understand precisely how this recursive function applies successfully using our environment model of computation. The environment diagram and expression tree that depict the evaluation of pig_latin('pun') appear below.
II. Evaluating the operand string literal to the string 'pun'.
B. Applying the function pig_latin to the argument 'pun' by
I. Adding a local frame, II. Binding the formal parameter w to the argument 'pun' in that frame, and III. Executing the body of pig_latin in the environment that starts with that frame:
a. The initial conditional statement has no effect, because the header expression evaluates to False. b. The final return expression pig_latin(w[1:] + w[0]) is evaluated by
As this example illustrates, a recursive function applies correctly, despite its circular character. The pig_latin function is applied twice, but with a different argument each time. Although the second call comes from the body of pig_latin itself, looking up that function by name succeeds because the name pig_latin is bound in the environment before its body is executed.
This example also illustrates how Python's recursive evaluation procedure can interact with a recursive function to evolve a complex computational process with many nested steps, even though the function definition may itself contain very few lines of code. Some examples are quite long indeed: the word scythe results in the longest
terminating pig_latin process among words that appear in Shakespeare's works, ignoring punctuation.