Professional Documents
Culture Documents
Ve LZ Que Z Iturbide 2000
Ve LZ Que Z Iturbide 2000
310
The paper has the following structure. The second section A gradual approach can aid in teaching recursion by
gives a global view to what we call a "gradual approach," by following several steps. We start with the concept of
showing examples of its successful application in other recursion and later show its occurrence in three fields of
computer science fields. The third section describes our computer science. They are introduced in increasing level of
gradual approach to teaching recursion, with three subsections difficulty, making explicit the mechanisms which make each
corresponding to the three fields we propose to study instance of recursion more difficult than the previous one.
recursion. Section 4 contains a discussion on our proposal, When recursion in imperative languages is explained, the
andin section 5 we summarize our conclusions. student already has experience with recursion and is prepared
to understand it properly.
In all of these methodologies a central idea lies: complex Definition. A grammar rule is recursive if the non-terminal
products are developed more easily in a sequence of small symbol at the left-hand side also appears at the right-hand
steps than in a single large step. Gradual methods are also side.
being used in computer science education. For instance, there Operational model. It consists in a form of rewriting,
are experiences (e.g. [6]) in programming projects where called derivation, that allows proving that a sentence
students are urged to develop programs in gradual steps. belongs to the language defined by a grammar. We start
In the paper, we advocate this approach to explain a difficult from the initial symbol and successively apply grammar
concept: recursion. We think that recursion is not too difficult rules until the target sentence is obtained. For each
by itself. The problem comes from trying to introduce rewriting, we select a grammar rule and substitute in the
recursion as it is instantiated in imperative programming last sentential form an occurrence of the rule left-hand side
languages. This particular instance of recursion is difficult (a non-terminal symbol) by its right-hand side (a'sequence
because of its interaction with other mechanisms specific to of arbitrary symbols).
imperative languages. For instance, the sentence a a a is derived from the previous
grammar as follows:
311
_s rewriting it into other expressions until an expression that
---> a S cannot be further simplified is obtained. The final
aaS ) expression is called the value of the original one. Rules are
aaa defined to evaluate each class of expression allowed in the
language (function application, conditional expression, etc.)
Notice that there are very few mechanisms associated to
rewriting in sentence derivations: For instance, the step-by-step evaluation of the factorial of
4 is given by the following sequence of expressions:
• Selection.For each rewriting step, a nonterminal
fact 4
symbol a in the sentential form and a rule with a in its $
left hand side are selected. In the previous derivation,
if 4=0 then 1 else 4*fact(4-1)
we have underlined the nonterminal selected in every
sentential form (notice that in this example only one
if false then 1 else 4*fact(4-1)
nonterminal can be selected at each step!).
$
• Substitution. The sentential form is rewritten by simply 4" f a c t (4-1)
replacing an occurrence of the nonterminal symbol by
the right hand side of the selected rule. 4" f a c t (3)
$
. . o
312
3.3 Recursion in Imperative Programming Activation records and the control stack. An activation
Finally, recursion in imperative programming is taught as a record is a structure invisible to the programmer but
mechanism complementary to loops in order to perform necessary to start and finish procedure calls. It keeps
repetitive computations. We show its use by means of a information necessary to execute a procedure and later
procedure that computes the factorial function (we use a restore execution in the statement following the
procedure rather than a function for generality). In Pascal: procedure call. This information is composed at least by
actual parameters, local variables and the return
procedure fact (n: integer; address.
v a t f: i n t e g e r ) ;
begin The control stack is the structure that contains activation
if n=O then records during program execution. It is a complex
f := 1 structure designed to store all of the information
else b e g i n necessary to make procedure calls, with some intricacies
f a c t (n-l, f); to handle names collisions.
f := n * f
end
end; 4 0 i s c u s s i o n and Related Work
Representation of information. Information is stored in In the academic year 1.997/98 we began to teach a course
abstractions of memory cells called variables. Pointer on principles of programming languages for freshmen. The
variables are especially remarkable because they do not course is atypical because similar courses are offered in
contain values, but memory addresses (of other variables). other universities to junior or, at least, sophomore students.
Most memory cells are declared explicitly by the This timing forced us t o design a course with innovative
programmer (variables), but others exist in run time approaches to many topics, including the gradual approach
without declaring them (the control stack). to recursion we have described in the paper.
Definition. A recursive procedure contains some call of The previous introduction to recursion was mostly driven
itself in its body, but applied to different arguments. by computations. A different line of exposition can be
followed by focusing on data types. The similarity of
We find again parameters and well-founded orderings in recursion in grammars, functional data types and
recursive procedures. In addition, parameters have the imperative data types is even greater than in recursive
following feature: control, so the exposition is easier. However, its main
• Parameter modes. Each formal parameter has drawback is the requirement to know pointers.
associated a passing mode, typically either by value or We do not know of people who use our method to teach
by reference. Each of these modes determines the kind recursion, although there are similar, less ambitious
of actual parameter that can be used. Thus, the actual proposals. Richard Pattis [10] proposes to teach EBNF in
parameter for a parameter by value can be any the first lecture of CS 1. EBNF is simple but rich enough to
imperative expression, but for a parameter by reference, illustrate a set of computer science concepts, including
it must be a variable. recursion. Mary Shaw et al. [13, p.65] also had mentioned
Operational model. The operational model of procedure this possibility. Both proposals introduce recursion using
call is associated to the operational model of imperative EBNF, but they do not mention how to make use (not even if
programming defined as state transformation. The current they do) of this knowledge in order to teach recursion in
state of execution is given by the contents of variables plus imperative languages.
information about the next statement to execute (stored in Nowadays, there are many experiences in the use of
the program counter). functional programming as a first language [1t14]. However,
A procedure call is a control break with return. In addition, the transition from functional programming to imperative
the set of accessible variables changes. The main concepts programming usually does not address explicitly the
necessary to understand all of these changes are: similarities and differences of recursion in both paradigms.
Besides, they do not include the prologue about recursion in
• Parameter binding. Depending on the mode of each grammars, which I find very convenient.
parameter, a different binding is established between
formal and actual parameters. For instance, a parameter We are aware that our proposal is unrealistic for most CSI/
passed by value is an expression that is first evaluated, CS2 programs, but it can be adopted, limited to grammars and
then a new local variable is created and finally it is imperative programming. From a pedagogical point of view,
assigned that value. However, for a parameter passed by even this restricted approach participates and benefits from
reference, a pointer is allocated to point to the actual the sequencing, spacing and spiraling methods [11]. In effect,
parameter variable. we propose a particular sequencing of several instances of
313
recursion. Consequently, recursion is spaced out over several [3] Bird, R., "Tabulation techniques for recursive
months and it is revisited several times. programs," ACM Computing Surveys, Vol. 12, No. 4,
Notice that our approach only provides a framework to teach December 1980, pp. 403-417
recursion in a gradual way. However, the approach is [4] Ginat, D., Shifroni, E., ''Teaching recursion in a
orthogonal to debates about how to teach certain instances of procedural environment- How much should we
recursion, most notably imperative recursion. We simply emphasize the computing model?," 30th SIGCSE
require to identify key concepts and mechanisms, but they Technical Symposium on Computer Science
can be taught in many different ways. However, we want to
Education, 1999, pp. 127-131
make two recommendations from our experience: [5] Haynes, S.M., "Explaining recursion to the
unsophisticated," SIGCSE Bulletin, Vol. 27, No. 3, Sept.
• A problem with explaining recursion is that there are 1995, pp. 3-6 and 14
many other concepts which can arise, although they are [6] Leeper, R., "Gradual project assignments in computer
not exclusive of recursion. Some examples are: scope courses", 20th SIGCSE Technical Symposium on
of identifiers, subprogram pre- and postconditions, etc. Computer Science Education, 1989, pp. 88-92
We recommend the teacher to teach them first, so that [7] McCracken, D.D., "Ruminations on computer science
the subsequent explanation of recursion will not be curricula," Communications oftheACM, Vol. 30, No. 1,
darkened by these details. January 1987, pp. 3-5
[8] Naps., T.L., Stenglein, J., ''Tools for visual exploration
• The explanation of every instance of recursion can of scope and parameter passing in a programming
benefit from visualization facilities, such as derivation languages course," 27th SIGCSE Technical Symposium
trees for grammars. Elsewhere can be found on Computer Science Education, 1996, pp. 305-309
visualizations of sentence derivations [2], evaluation of [9] Partsch, H.A., Specification and Transformation of
functional expressions [18] and control stack issues Programs, Springer-Verlag, 1990
[8,16]. [10] Pattis, R.E., "Teaching EBNF first in CS 1," 25th
SIGCSE Technical Symposium on Computer Science
Education, 1994, pp. 300-303
5 Conclusions [11] Powers, K.D., D.T. Powers, "Making sense of
We have proposed a gradual approach to simplify the teaching methods in computer science", 1999
teaching of reeursion. It is introduced in increasing level of Frontiers in Education Conference, session 1 lb3
difficulty by means of three occurrences of recursion in [ 12] Reinfelds, J., "A three paradigm first course for CS
different computer science fields, namely formal grammars, majors," 26th SIGCSE Technical Symposium on
functional programming and imperative programming. The Computer Science Education, 1995, pp. 223-227
approach has three main advantages. First, the student [13] Shaw, M., et al. (eds.), The Carnegie-Mellon
perceives recursion as a recurrent concept in the discipline of Curriculum for Undergraduate Computer Science,
computer science, not only existing in imperative Springer-Verlag, 1985
programming. Second, the concept of recursion is isolated [14] Thomson, S., Wadler, P. (eds.), monographic issue on
and differentiated from any other concept or mechanism "Functional programming in education," Journal of
associated to a particular instance of recursion. Finally, the Functional Programming, vol. 3, n°. 1, January 1993
teaching of recursion is simplified because it is taught in a [15] Tucker, A., et al, Computing Curricula 1991, ACM
gradual way. Press and IEEE Computer Society Press, 1991
[16] Veh~zquez-Iturbide, J.A., ''Formalization of the control
stack," SIGPLAN Notices, Vol. 24, No. 3, March 1989,
6 Acknowledgments pp. 46-54
[17] Velfizquez-Iturbide, J.A., "A methodology for monitor
I want to thank the valuable comments of the anonymous development in concurrent programs," SIGCSE
referees. This work was supported by the Comunidad Bulletin, Vol. 26, No. 3, September 1994, pp. 22-28
Aut6noma de Madrid under project no. 07T/0036/98. [ 18] Velfizquez-Iturbide, J.A., A. Presa-V~zquez,
"Customization of visualizations in a functional
programming environment," 1999 Frontiers in
References Education Conference, session 12b3
[1] Astrachan, O., "Self-reference is an illustrative [19] Wirth, N., "Program development by stepwise
essential," 25th SIGCSE Technical Symposium on refinement," Communications of the ACM, Vol. 14, No.
Computer Science Education, 1994, pp. 238-242 4, April 1971, pp. 221-227
[2] Bilska, A. O., et al., "A collection of tools for making [20] Wu, C.-C., Dale, N.B., Bethel, LJ., ''Conceptual models
automata theory and formal languages come alive," and cognitive learning styles in teaching recursion,"
28th SIGCSE Technical Symposium on Computer 29th SIGCSE Technical Symposium on Computer
Science Education, 1997, pp. 15-19 Science Education, 1998, pp. 292-296
314