(Tail Calls): New section.
[bpt/guile.git] / doc / ref / scheme-ideas.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @node Basic Ideas
8 @section Basic Ideas in Scheme
9
10 In this chapter, we introduce the basic concepts that underpin the
11 elegance and power of the Scheme language.
12
13 Readers who already possess a background knowledge of Scheme may happily
14 skip this chapter. For the reader who is new to the language, however,
15 the following discussions on data, procedures, expressions and closure
16 are designed to provide a minimum level of Scheme understanding that is
17 more or less assumed by the reference chapters that follow.
18
19 The style of this introductory material aims about halfway between the
20 terse precision of R5RS and the discursive randomness of a Scheme
21 tutorial.
22
23 @menu
24 * About Data:: Latent typing, types, values and variables.
25 * About Procedures:: The representation and use of procedures.
26 * About Expressions:: All kinds of expressions and their meaning.
27 * About Closure:: Closure, scoping and environments.
28 @end menu
29
30
31 @node About Data
32 @subsection Data Types, Values and Variables
33
34 This section discusses the representation of data types and values, what
35 it means for Scheme to be a @dfn{latently typed} language, and the role
36 of variables. We conclude by introducing the Scheme syntaxes for
37 defining a new variable, and for changing the value of an existing
38 variable.
39
40 @menu
41 * Latent Typing:: Scheme as a "latently typed" language.
42 * Values and Variables:: About data types, values and variables.
43 * Definition:: Defining variables and setting their values.
44 @end menu
45
46
47 @node Latent Typing
48 @subsubsection Latent Typing
49
50 The term @dfn{latent typing} is used to describe a computer language,
51 such as Scheme, for which you cannot, @emph{in general}, simply look at
52 a program's source code and determine what type of data will be
53 associated with a particular variable, or with the result of a
54 particular expression.
55
56 Sometimes, of course, you @emph{can} tell from the code what the type of
57 an expression will be. If you have a line in your program that sets the
58 variable @code{x} to the numeric value 1, you can be certain that,
59 immediately after that line has executed (and in the absence of multiple
60 threads), @code{x} has the numeric value 1. Or if you write a procedure
61 that is designed to concatenate two strings, it is likely that the rest
62 of your application will always invoke this procedure with two string
63 parameters, and quite probable that the procedure would go wrong in some
64 way if it was ever invoked with parameters that were not both strings.
65
66 Nevertheless, the point is that there is nothing in Scheme which
67 requires the procedure parameters always to be strings, or @code{x}
68 always to hold a numeric value, and there is no way of declaring in your
69 program that such constraints should always be obeyed. In the same
70 vein, there is no way to declare the expected type of a procedure's
71 return value.
72
73 Instead, the types of variables and expressions are only known -- in
74 general -- at run time. If you @emph{need} to check at some point that
75 a value has the expected type, Scheme provides run time procedures that
76 you can invoke to do so. But equally, it can be perfectly valid for two
77 separate invocations of the same procedure to specify arguments with
78 different types, and to return values with different types.
79
80 The next subsection explains what this means in practice, for the ways
81 that Scheme programs use data types, values and variables.
82
83
84 @node Values and Variables
85 @subsubsection Values and Variables
86
87 Scheme provides many data types that you can use to represent your data.
88 Primitive types include characters, strings, numbers and procedures.
89 Compound types, which allow a group of primitive and compound values to
90 be stored together, include lists, pairs, vectors and multi-dimensional
91 arrays. In addition, Guile allows applications to define their own data
92 types, with the same status as the built-in standard Scheme types.
93
94 As a Scheme program runs, values of all types pop in and out of
95 existence. Sometimes values are stored in variables, but more commonly
96 they pass seamlessly from being the result of one computation to being
97 one of the parameters for the next.
98
99 Consider an example. A string value is created because the interpreter
100 reads in a literal string from your program's source code. Then a
101 numeric value is created as the result of calculating the length of the
102 string. A second numeric value is created by doubling the calculated
103 length. Finally the program creates a list with two elements -- the
104 doubled length and the original string itself -- and stores this list in
105 a program variable.
106
107 All of the values involved here -- in fact, all values in Scheme --
108 carry their type with them. In other words, every value ``knows,'' at
109 runtime, what kind of value it is. A number, a string, a list,
110 whatever.
111
112 A variable, on the other hand, has no fixed type. A variable --
113 @code{x}, say -- is simply the name of a location -- a box -- in which
114 you can store any kind of Scheme value. So the same variable in a
115 program may hold a number at one moment, a list of procedures the next,
116 and later a pair of strings. The ``type'' of a variable -- insofar as
117 the idea is meaningful at all -- is simply the type of whatever value
118 the variable happens to be storing at a particular moment.
119
120
121 @node Definition
122 @subsubsection Defining and Setting Variables
123
124 To define a new variable, you use Scheme's @code{define} syntax like
125 this:
126
127 @lisp
128 (define @var{variable-name} @var{value})
129 @end lisp
130
131 This makes a new variable called @var{variable-name} and stores
132 @var{value} in it as the variable's initial value. For example:
133
134 @lisp
135 ;; Make a variable `x' with initial numeric value 1.
136 (define x 1)
137
138 ;; Make a variable `organization' with an initial string value.
139 (define organization "Free Software Foundation")
140 @end lisp
141
142 (In Scheme, a semicolon marks the beginning of a comment that continues
143 until the end of the line. So the lines beginning @code{;;} are
144 comments.)
145
146 Changing the value of an already existing variable is very similar,
147 except that @code{define} is replaced by the Scheme syntax @code{set!},
148 like this:
149
150 @lisp
151 (set! @var{variable-name} @var{new-value})
152 @end lisp
153
154 Remember that variables do not have fixed types, so @var{new-value} may
155 have a completely different type from whatever was previously stored in
156 the location named by @var{variable-name}. Both of the following
157 examples are therefore correct.
158
159 @lisp
160 ;; Change the value of `x' to 5.
161 (set! x 5)
162
163 ;; Change the value of `organization' to the FSF's street number.
164 (set! organization 545)
165 @end lisp
166
167 In these examples, @var{value} and @var{new-value} are literal numeric
168 or string values. In general, however, @var{value} and @var{new-value}
169 can be any Scheme expression. Even though we have not yet covered the
170 forms that Scheme expressions can take (@pxref{About Expressions}), you
171 can probably guess what the following @code{set!} example does@dots{}
172
173 @lisp
174 (set! x (+ x 1))
175 @end lisp
176
177 (Note: this is not a complete description of @code{define} and
178 @code{set!}, because we need to introduce some other aspects of Scheme
179 before the missing pieces can be filled in. If, however, you are
180 already familiar with the structure of Scheme, you may like to read
181 about those missing pieces immediately by jumping ahead to the following
182 references.
183
184 @itemize @bullet
185 @item
186 @ref{Lambda Alternatives}, to read about an alternative form of the
187 @code{define} syntax that can be used when defining new procedures.
188
189 @item
190 @ref{Procedures with Setters}, to read about an alternative form of the
191 @code{set!} syntax that helps with changing a single value in the depths
192 of a compound data structure.)
193
194 @item
195 @xref{Internal Definitions}, to read about using @code{define} other
196 than at top level in a Scheme program, including a discussion of when it
197 works to use @code{define} rather than @code{set!} to change the value
198 of an existing variable.
199 @end itemize
200
201
202 @node About Procedures
203 @subsection The Representation and Use of Procedures
204
205 This section introduces the basics of using and creating Scheme
206 procedures. It discusses the representation of procedures as just
207 another kind of Scheme value, and shows how procedure invocation
208 expressions are constructed. We then explain how @code{lambda} is used
209 to create new procedures, and conclude by presenting the various
210 shorthand forms of @code{define} that can be used instead of writing an
211 explicit @code{lambda} expression.
212
213 @menu
214 * Procedures as Values:: Procedures are values like everything else.
215 * Simple Invocation:: How to write a simple procedure invocation.
216 * Creating a Procedure:: How to create your own procedures.
217 * Lambda Alternatives:: Other ways of writing procedure definitions.
218 @end menu
219
220
221 @node Procedures as Values
222 @subsubsection Procedures as Values
223
224 One of the great simplifications of Scheme is that a procedure is just
225 another type of value, and that procedure values can be passed around
226 and stored in variables in exactly the same way as, for example, strings
227 and lists. When we talk about a built-in standard Scheme procedure such
228 as @code{open-input-file}, what we actually mean is that there is a
229 pre-defined top level variable called @code{open-input-file}, whose
230 value is a procedure that implements what R5RS says that
231 @code{open-input-file} should do.
232
233 Note that this is quite different from many dialects of Lisp ---
234 including Emacs Lisp --- in which a program can use the same name with
235 two quite separate meanings: one meaning identifies a Lisp function,
236 while the other meaning identifies a Lisp variable, whose value need
237 have nothing to do with the function that is associated with the first
238 meaning. In these dialects, functions and variables are said to live in
239 different @dfn{namespaces}.
240
241 In Scheme, on the other hand, all names belong to a single unified
242 namespace, and the variables that these names identify can hold any kind
243 of Scheme value, including procedure values.
244
245 One consequence of the ``procedures as values'' idea is that, if you
246 don't happen to like the standard name for a Scheme procedure, you can
247 change it.
248
249 For example, @code{call-with-current-continuation} is a very important
250 standard Scheme procedure, but it also has a very long name! So, many
251 programmers use the following definition to assign the same procedure
252 value to the more convenient name @code{call/cc}.
253
254 @lisp
255 (define call/cc call-with-current-continuation)
256 @end lisp
257
258 Let's understand exactly how this works. The definition creates a new
259 variable @code{call/cc}, and then sets its value to the value of the
260 variable @code{call-with-current-continuation}; the latter value is a
261 procedure that implements the behaviour that R5RS specifies under the
262 name ``call-with-current-continuation''. So @code{call/cc} ends up
263 holding this value as well.
264
265 Now that @code{call/cc} holds the required procedure value, you could
266 choose to use @code{call-with-current-continuation} for a completely
267 different purpose, or just change its value so that you will get an
268 error if you accidentally use @code{call-with-current-continuation} as a
269 procedure in your program rather than @code{call/cc}. For example:
270
271 @lisp
272 (set! call-with-current-continuation "Not a procedure any more!")
273 @end lisp
274
275 Or you could just leave @code{call-with-current-continuation} as it was.
276 It's perfectly fine for more than one variable to hold the same
277 procedure value.
278
279
280 @node Simple Invocation
281 @subsubsection Simple Procedure Invocation
282
283 A procedure invocation in Scheme is written like this:
284
285 @lisp
286 (@var{procedure} [@var{arg1} [@var{arg2} @dots{}]])
287 @end lisp
288
289 In this expression, @var{procedure} can be any Scheme expression whose
290 value is a procedure. Most commonly, however, @var{procedure} is simply
291 the name of a variable whose value is a procedure.
292
293 For example, @code{string-append} is a standard Scheme procedure whose
294 behaviour is to concatenate together all the arguments, which are
295 expected to be strings, that it is given. So the expression
296
297 @lisp
298 (string-append "/home" "/" "andrew")
299 @end lisp
300
301 @noindent
302 is a procedure invocation whose result is the string value
303 @code{"/home/andrew"}.
304
305 Similarly, @code{string-length} is a standard Scheme procedure that
306 returns the length of a single string argument, so
307
308 @lisp
309 (string-length "abc")
310 @end lisp
311
312 @noindent
313 is a procedure invocation whose result is the numeric value 3.
314
315 Each of the parameters in a procedure invocation can itself be any
316 Scheme expression. Since a procedure invocation is itself a type of
317 expression, we can put these two examples together to get
318
319 @lisp
320 (string-length (string-append "/home" "/" "andrew"))
321 @end lisp
322
323 @noindent
324 --- a procedure invocation whose result is the numeric value 12.
325
326 (You may be wondering what happens if the two examples are combined the
327 other way round. If we do this, we can make a procedure invocation
328 expression that is @emph{syntactically} correct:
329
330 @lisp
331 (string-append "/home" (string-length "abc"))
332 @end lisp
333
334 @noindent
335 but when this expression is executed, it will cause an error, because
336 the result of @code{(string-length "abc")} is a numeric value, and
337 @code{string-append} is not designed to accept a numeric value as one of
338 its arguments.)
339
340
341 @node Creating a Procedure
342 @subsubsection Creating and Using a New Procedure
343
344 Scheme has lots of standard procedures, and Guile provides all of these
345 via predefined top level variables. All of these standard procedures
346 are documented in the later chapters of this reference manual.
347
348 Before very long, though, you will want to create new procedures that
349 encapsulate aspects of your own applications' functionality. To do
350 this, you can use the famous @code{lambda} syntax.
351
352 For example, the value of the following Scheme expression
353
354 @lisp
355 (lambda (name address) @var{expression} @dots{})
356 @end lisp
357
358 @noindent
359 is a newly created procedure that takes two arguments:
360 @code{name} and @code{address}. The behaviour of the
361 new procedure is determined by the sequence of @var{expression}s in the
362 @dfn{body} of the procedure definition. (Typically, these
363 @var{expression}s would use the arguments in some way, or else there
364 wouldn't be any point in giving them to the procedure.) When invoked,
365 the new procedure returns a value that is the value of the last
366 @var{expression} in the procedure body.
367
368 To make things more concrete, let's suppose that the two arguments are
369 both strings, and that the purpose of this procedure is to form a
370 combined string that includes these arguments. Then the full lambda
371 expression might look like this:
372
373 @lisp
374 (lambda (name address)
375 (string-append "Name=" name ":Address=" address))
376 @end lisp
377
378 We noted in the previous subsection that the @var{procedure} part of a
379 procedure invocation expression can be any Scheme expression whose value
380 is a procedure. But that's exactly what a lambda expression is! So we
381 can use a lambda expression directly in a procedure invocation, like
382 this:
383
384 @lisp
385 ((lambda (name address)
386 (string-append "Name=" name ":Address=" address))
387 "FSF"
388 "Cambridge")
389 @end lisp
390
391 @noindent
392 This is a valid procedure invocation expression, and its result is the
393 string @code{"Name=FSF:Address=Cambridge"}.
394
395 It is more common, though, to store the procedure value in a variable ---
396
397 @lisp
398 (define make-combined-string
399 (lambda (name address)
400 (string-append "Name=" name ":Address=" address)))
401 @end lisp
402
403 @noindent
404 --- and then to use the variable name in the procedure invocation:
405
406 @lisp
407 (make-combined-string "FSF" "Cambridge")
408 @end lisp
409
410 @noindent
411 Which has exactly the same result.
412
413 It's important to note that procedures created using @code{lambda} have
414 exactly the same status as the standard built in Scheme procedures, and
415 can be invoked, passed around, and stored in variables in exactly the
416 same ways.
417
418
419 @node Lambda Alternatives
420 @subsubsection Lambda Alternatives
421
422 Since it is so common in Scheme programs to want to create a procedure
423 and then store it in a variable, there is an alternative form of the
424 @code{define} syntax that allows you to do just that.
425
426 A @code{define} expression of the form
427
428 @lisp
429 (define (@var{name} [@var{arg1} [@var{arg2} @dots{}]])
430 @var{expression} @dots{})
431 @end lisp
432
433 @noindent
434 is exactly equivalent to the longer form
435
436 @lisp
437 (define @var{name}
438 (lambda ([@var{arg1} [@var{arg2} @dots{}]])
439 @var{expression} @dots{}))
440 @end lisp
441
442 So, for example, the definition of @code{make-combined-string} in the
443 previous subsection could equally be written:
444
445 @lisp
446 (define (make-combined-string name address)
447 (string-append "Name=" name ":Address=" address))
448 @end lisp
449
450 This kind of procedure definition creates a procedure that requires
451 exactly the expected number of arguments. There are two further forms
452 of the @code{lambda} expression, which create a procedure that can
453 accept a variable number of arguments:
454
455 @lisp
456 (lambda (@var{arg1} @dots{} . @var{args}) @var{expression} @dots{})
457
458 (lambda @var{args} @var{expression} @dots{})
459 @end lisp
460
461 @noindent
462 The corresponding forms of the alternative @code{define} syntax are:
463
464 @lisp
465 (define (@var{name} @var{arg1} @dots{} . @var{args}) @var{expression} @dots{})
466
467 (define (@var{name} . @var{args}) @var{expression} @dots{})
468 @end lisp
469
470 @noindent
471 For details on how these forms work, see @xref{Lambda}.
472
473 (It could be argued that the alternative @code{define} forms are rather
474 confusing, especially for newcomers to the Scheme language, as they hide
475 both the role of @code{lambda} and the fact that procedures are values
476 that are stored in variables in the some way as any other kind of value.
477 On the other hand, they are very convenient, and they are also a good
478 example of another of Scheme's powerful features: the ability to specify
479 arbitrary syntactic transformations at run time, which can be applied to
480 subsequently read input.)
481
482
483 @node About Expressions
484 @subsection Expressions and Evaluation
485
486 So far, we have met expressions that @emph{do} things, such as the
487 @code{define} expressions that create and initialize new variables, and
488 we have also talked about expressions that have @emph{values}, for
489 example the value of the procedure invocation expression:
490
491 @lisp
492 (string-append "/home" "/" "andrew")
493 @end lisp
494
495 @noindent
496 but we haven't yet been precise about what causes an expression like
497 this procedure invocation to be reduced to its ``value'', or how the
498 processing of such expressions relates to the execution of a Scheme
499 program as a whole.
500
501 This section clarifies what we mean by an expression's value, by
502 introducing the idea of @dfn{evaluation}. It discusses the side effects
503 that evaluation can have, explains how each of the various types of
504 Scheme expression is evaluated, and describes the behaviour and use of
505 the Guile REPL as a mechanism for exploring evaluation. The section
506 concludes with a very brief summary of Scheme's common syntactic
507 expressions.
508
509 @menu
510 * Evaluating:: How a Scheme program is executed.
511 * Tail Calls:: Space-safe recursion.
512 * The REPL:: Interacting with the Guile interpreter.
513 * Syntax Summary:: Common syntactic expressions -- in brief.
514 @end menu
515
516
517 @node Evaluating
518 @subsubsection Evaluating Expressions and Executing Programs
519
520 In Scheme, the process of executing an expression is known as
521 @dfn{evaluation}. Evaluation has two kinds of result:
522
523 @itemize @bullet
524 @item
525 the @dfn{value} of the evaluated expression
526
527 @item
528 the @dfn{side effects} of the evaluation, which consist of any effects of
529 evaluating the expression that are not represented by the value.
530 @end itemize
531
532 Of the expressions that we have met so far, @code{define} and
533 @code{set!} expressions have side effects --- the creation or
534 modification of a variable --- but no value; @code{lambda} expressions
535 have values --- the newly constructed procedures --- but no side
536 effects; and procedure invocation expressions, in general, have either
537 values, or side effects, or both.
538
539 It is tempting to try to define more intuitively what we mean by
540 ``value'' and ``side effects'', and what the difference between them is.
541 In general, though, this is extremely difficult. It is also
542 unnecessary; instead, we can quite happily define the behaviour of a
543 Scheme program by specifying how Scheme executes a program as a whole,
544 and then by describing the value and side effects of evaluation for each
545 type of expression individually.
546
547 @noindent
548 So, some@footnote{These definitions are approximate. For the whole and
549 detailed truth, see @xref{Formal syntax and semantics,R5RS
550 syntax,,r5rs}.} definitions@dots{}
551
552 @itemize @bullet
553
554 @item
555 A Scheme program consists of a sequence of expressions.
556
557 @item
558 A Scheme interpreter executes the program by evaluating these
559 expressions in order, one by one.
560
561 @item
562 An expression can be
563
564 @itemize @bullet
565 @item
566 a piece of literal data, such as a number @code{2.3} or a string
567 @code{"Hello world!"}
568 @item
569 a variable name
570 @item
571 a procedure invocation expression
572 @item
573 one of Scheme's special syntactic expressions.
574 @end itemize
575 @end itemize
576
577 @noindent
578 The following subsections describe how each of these types of expression
579 is evaluated.
580
581 @c @menu
582 @c * Eval Literal:: Evaluating literal data.
583 @c * Eval Variable:: Evaluating variable references.
584 @c * Eval Procedure:: Evaluating procedure invocation expressions.
585 @c * Eval Special:: Evaluating special syntactic expressions.
586 @c @end menu
587
588 @c @node Eval Literal
589
590 @subsubheading Evaluating Literal Data
591
592 When a literal data expression is evaluated, the value of the expression
593 is simply the value that the expression describes. The evaluation of a
594 literal data expression has no side effects.
595
596 @noindent
597 So, for example,
598
599 @itemize @bullet
600 @item
601 the value of the expression @code{"abc"} is the string value
602 @code{"abc"}
603
604 @item
605 the value of the expression @code{3+4i} is the complex number 3 + 4i
606
607 @item
608 the value of the expression @code{#(1 2 3)} is a three-element vector
609 containing the numeric values 1, 2 and 3.
610 @end itemize
611
612 For any data type which can be expressed literally like this, the syntax
613 of the literal data expression for that data type --- in other words,
614 what you need to write in your code to indicate a literal value of that
615 type --- is known as the data type's @dfn{read syntax}. This manual
616 specifies the read syntax for each such data type in the section that
617 describes that data type.
618
619 Some data types do not have a read syntax. Procedures, for example,
620 cannot be expressed as literal data; they must be created using a
621 @code{lambda} expression (@pxref{Creating a Procedure}) or implicitly
622 using the shorthand form of @code{define} (@pxref{Lambda Alternatives}).
623
624
625 @c @node Eval Variable
626 @subsubheading Evaluating a Variable Reference
627
628 When an expression that consists simply of a variable name is evaluated,
629 the value of the expression is the value of the named variable. The
630 evaluation of a variable reference expression has no side effects.
631
632 So, after
633
634 @lisp
635 (define key "Paul Evans")
636 @end lisp
637
638 @noindent
639 the value of the expression @code{key} is the string value @code{"Paul
640 Evans"}. If @var{key} is then modified by
641
642 @lisp
643 (set! key 3.74)
644 @end lisp
645
646 @noindent
647 the value of the expression @code{key} is the numeric value 3.74.
648
649 If there is no variable with the specified name, evaluation of the
650 variable reference expression signals an error.
651
652
653 @c @node Eval Procedure
654 @subsubheading Evaluating a Procedure Invocation Expression
655
656 This is where evaluation starts getting interesting! As already noted,
657 a procedure invocation expression has the form
658
659 @lisp
660 (@var{procedure} [@var{arg1} [@var{arg2} @dots{}]])
661 @end lisp
662
663 @noindent
664 where @var{procedure} must be an expression whose value, when evaluated,
665 is a procedure.
666
667 The evaluation of a procedure invocation expression like this proceeds
668 by
669
670 @itemize @bullet
671 @item
672 evaluating individually the expressions @var{procedure}, @var{arg1},
673 @var{arg2}, and so on
674
675 @item
676 calling the procedure that is the value of the @var{procedure}
677 expression with the list of values obtained from the evaluations of
678 @var{arg1}, @var{arg2} etc. as its parameters.
679 @end itemize
680
681 For a procedure defined in Scheme, ``calling the procedure with the list
682 of values as its parameters'' means binding the values to the
683 procedure's formal parameters and then evaluating the sequence of
684 expressions that make up the body of the procedure definition. The
685 value of the procedure invocation expression is the value of the last
686 evaluated expression in the procedure body. The side effects of calling
687 the procedure are the combination of the side effects of the sequence of
688 evaluations of expressions in the procedure body.
689
690 For a built-in procedure, the value and side-effects of calling the
691 procedure are best described by that procedure's documentation.
692
693 Note that the complete side effects of evaluating a procedure invocation
694 expression consist not only of the side effects of the procedure call,
695 but also of any side effects of the preceding evaluation of the
696 expressions @var{procedure}, @var{arg1}, @var{arg2}, and so on.
697
698 To illustrate this, let's look again at the procedure invocation
699 expression:
700
701 @lisp
702 (string-length (string-append "/home" "/" "andrew"))
703 @end lisp
704
705 In the outermost expression, @var{procedure} is @code{string-length} and
706 @var{arg1} is @code{(string-append "/home" "/" "andrew")}.
707
708 @itemize @bullet
709 @item
710 Evaluation of @code{string-length}, which is a variable, gives a
711 procedure value that implements the expected behaviour for
712 ``string-length''.
713
714 @item
715 Evaluation of @code{(string-append "/home" "/" "andrew")}, which is
716 another procedure invocation expression, means evaluating each of
717
718 @itemize @bullet
719 @item
720 @code{string-append}, which gives a procedure value that implements the
721 expected behaviour for ``string-append''
722
723 @item
724 @code{"/home"}, which gives the string value @code{"/home"}
725
726 @item
727 @code{"/"}, which gives the string value @code{"/"}
728
729 @item
730 @code{"andrew"}, which gives the string value @code{"andrew"}
731 @end itemize
732
733 and then invoking the procedure value with this list of string values as
734 its arguments. The resulting value is a single string value that is the
735 concatenation of all the arguments, namely @code{"/home/andrew"}.
736 @end itemize
737
738 In the evaluation of the outermost expression, the interpreter can now
739 invoke the procedure value obtained from @var{procedure} with the value
740 obtained from @var{arg1} as its arguments. The resulting value is a
741 numeric value that is the length of the argument string, which is 12.
742
743
744 @c @node Eval Special
745 @subsubheading Evaluating Special Syntactic Expressions
746
747 When a procedure invocation expression is evaluated, the procedure and
748 @emph{all} the argument expressions must be evaluated before the
749 procedure can be invoked. Special syntactic expressions are special
750 because they are able to manipulate their arguments in an unevaluated
751 form, and can choose whether to evaluate any or all of the argument
752 expressions.
753
754 Why is this needed? Consider a program fragment that asks the user
755 whether or not to delete a file, and then deletes the file if the user
756 answers yes.
757
758 @lisp
759 (if (string=? (read-answer "Should I delete this file?")
760 "yes")
761 (delete-file file))
762 @end lisp
763
764 If the outermost @code{(if @dots{})} expression here was a procedure
765 invocation expression, the expression @code{(delete-file file)}, whose
766 side effect is to actually delete a file, would already have been
767 evaluated before the @code{if} procedure even got invoked! Clearly this
768 is no use --- the whole point of an @code{if} expression is that the
769 @dfn{consequent} expression is only evaluated if the condition of the
770 @code{if} expression is ``true''.
771
772 Therefore @code{if} must be special syntax, not a procedure. Other
773 special syntaxes that we have already met are @code{define}, @code{set!}
774 and @code{lambda}. @code{define} and @code{set!} are syntax because
775 they need to know the variable @emph{name} that is given as the first
776 argument in a @code{define} or @code{set!} expression, not that
777 variable's value. @code{lambda} is syntax because it does not
778 immediately evaluate the expressions that define the procedure body;
779 instead it creates a procedure object that incorporates these
780 expressions so that they can be evaluated in the future, when that
781 procedure is invoked.
782
783 The rules for evaluating each special syntactic expression are specified
784 individually for each special syntax. For a summary of standard special
785 syntax, see @xref{Syntax Summary}.
786
787
788 @node Tail Calls
789 @subsubsection Tail calls
790 @cindex tail calls
791 @cindex recursion
792
793 Scheme is ``properly tail recursive'', meaning that tail calls or
794 recursions from certain contexts do not consume stack space or other
795 resources and can therefore be used on arbitrarily large data or for
796 an arbitrarily long calculation. Consider for example,
797
798 @example
799 (define (foo n)
800 (display n)
801 (newline)
802 (foo (1+ n)))
803
804 (foo 1)
805 @print{}
806 1
807 2
808 3
809 @dots{}
810 @end example
811
812 @code{foo} prints numbers infinitely, starting from the given @var{n}.
813 It's implemented by printing @var{n} then recursing to itself to print
814 @math{@var{n}+1} and so on. This recursion is a tail call, it's the
815 last thing done, and in Scheme such tail calls can be made without
816 limit.
817
818 Or consider a case where a value is returned, a version of the SRFI-1
819 @code{last} function (@pxref{SRFI-1 Selectors}) returning the last
820 element of a list,
821
822 @example
823 (define (my-last lst)
824 (if (null? (cdr lst))
825 (car lst)
826 (my-last (cdr lst))))
827
828 (my-last '(1 2 3)) @result{} 3
829 @end example
830
831 If the list has more than one element, @code{my-last} applies itself
832 to the @code{cdr}. This recursion is a tail call, there's no code
833 after it, and the return value is the return value from that call. In
834 Scheme this can be used on an arbitrarily long list argument.
835
836 @sp 1
837 A proper tail call is only available from certain contexts, namely the
838 following special form positions,
839
840 @itemize @bullet
841 @item
842 @code{and} --- last expression
843
844 @item
845 @code{begin} --- last expression
846
847 @item
848 @code{case} --- last expression in each clause
849
850 @item
851 @code{cond} --- last expression in each clause, and the call to a
852 @code{=>} procedure is a tail call
853
854 @item
855 @code{do} --- last result expression
856
857 @item
858 @code{if} --- ``true'' and ``false'' leg expressions
859
860 @item
861 @code{lambda} --- last expression in body
862
863 @item
864 @code{let}, @code{let*}, @code{letrec}, @code{let-syntax},
865 @code{letrec-syntax} --- last expression in body
866
867 @item
868 @code{or} --- last expression
869 @end itemize
870
871 @noindent
872 The following core functions make tail calls,
873
874 @itemize @bullet
875 @item
876 @code{apply} --- tail call to given procedure
877
878 @item
879 @code{call-with-current-continuation} --- tail call to the procedure
880 receiving the new continuation
881
882 @item
883 @code{call-with-values} --- tail call to given procedure
884
885 @item
886 @code{eval} --- tail call to evaluate the form
887
888 @item
889 @code{string-any}, @code{string-every} --- tail call to predicate on
890 the last character (if that point is reached)
891 @end itemize
892
893 @sp 1
894 The above are just the core functions and special forms, tail calls in
895 other modules are described with the relevant documentation, for
896 example SRFI-1 @code{any} and @code{every} (@pxref{SRFI-1 Searching}).
897
898 It will be noted there are a lot of places which could potentially be
899 tail calls, for instance the last call in a @code{for-each}, but only
900 those explicitly described are guaranteed.
901
902
903 @node The REPL
904 @subsubsection Using the Guile REPL
905
906 If you start Guile without specifying a particular program for it to
907 execute, Guile enters its standard Read Evaluate Print Loop --- or
908 @dfn{REPL} for short. In this mode, Guile repeatedly reads in the next
909 Scheme expression that the user types, evaluates it, and prints the
910 resulting value.
911
912 The REPL is a useful mechanism for exploring the evaluation behaviour
913 described in the previous subsection. If you type @code{string-append},
914 for example, the REPL replies @code{#<primitive-procedure
915 string-append>}, illustrating the relationship between the variable
916 @code{string-append} and the procedure value stored in that variable.
917
918 In this manual, the notation @result{} is used to mean ``evaluates
919 to''. Wherever you see an example of the form
920
921 @lisp
922 @var{expression}
923 @result{}
924 @var{result}
925 @end lisp
926
927 @noindent
928 feel free to try it out yourself by typing @var{expression} into the
929 REPL and checking that it gives the expected @var{result}.
930
931
932 @node Syntax Summary
933 @subsubsection Summary of Common Syntax
934
935 This subsection lists the most commonly used Scheme syntactic
936 expressions, simply so that you will recognize common special syntax
937 when you see it. For a full description of each of these syntaxes,
938 follow the appropriate reference.
939
940 @code{lambda} (@pxref{Lambda}) is used to construct procedure objects.
941
942 @code{define} (@pxref{Top Level}) is used to create a new variable and
943 set its initial value.
944
945 @code{set!} (@pxref{Top Level}) is used to modify an existing variable's
946 value.
947
948 @code{let}, @code{let*} and @code{letrec} (@pxref{Local Bindings})
949 create an inner lexical environment for the evaluation of a sequence of
950 expressions, in which a specified set of local variables is bound to the
951 values of a corresponding set of expressions. For an introduction to
952 environments, see @xref{About Closure}.
953
954 @code{begin} (@pxref{begin}) executes a sequence of expressions in order
955 and returns the value of the last expression. Note that this is not the
956 same as a procedure which returns its last argument, because the
957 evaluation of a procedure invocation expression does not guarantee to
958 evaluate the arguments in order.
959
960 @code{if} and @code{cond} (@pxref{if cond case}) provide conditional
961 evaluation of argument expressions depending on whether one or more
962 conditions evaluate to ``true'' or ``false''.
963
964 @code{case} (@pxref{if cond case}) provides conditional evaluation of
965 argument expressions depending on whether a variable has one of a
966 specified group of values.
967
968 @code{and} (@pxref{and or}) executes a sequence of expressions in order
969 until either there are no expressions left, or one of them evaluates to
970 ``false''.
971
972 @code{or} (@pxref{and or}) executes a sequence of expressions in order
973 until either there are no expressions left, or one of them evaluates to
974 ``true''.
975
976
977 @node About Closure
978 @subsection The Concept of Closure
979
980 @cindex closure
981
982 The concept of @dfn{closure} is the idea that a lambda expression
983 ``captures'' the variable bindings that are in lexical scope at the
984 point where the lambda expression occurs. The procedure created by the
985 lambda expression can refer to and mutate the captured bindings, and the
986 values of those bindings persist between procedure calls.
987
988 This section explains and explores the various parts of this idea in
989 more detail.
990
991 @menu
992 * About Environments:: Names, locations, values and environments.
993 * Local Variables:: Local variables and local environments.
994 * Chaining:: Environment chaining.
995 * Lexical Scope:: The meaning of lexical scoping.
996 * Closure:: Explaining the concept of closure.
997 * Serial Number:: Example 1: a serial number generator.
998 * Shared Variable:: Example 2: a shared persistent variable.
999 * Callback Closure:: Example 3: the callback closure problem.
1000 * OO Closure:: Example 4: object orientation.
1001 @end menu
1002
1003 @node About Environments
1004 @subsubsection Names, Locations, Values and Environments
1005
1006 @cindex location
1007 @cindex environment
1008 @cindex vcell
1009 @cindex top level environment
1010 @cindex environment, top level
1011
1012 We said earlier that a variable name in a Scheme program is associated
1013 with a location in which any kind of Scheme value may be stored.
1014 (Incidentally, the term ``vcell'' is often used in Lisp and Scheme
1015 circles as an alternative to ``location''.) Thus part of what we mean
1016 when we talk about ``creating a variable'' is in fact establishing an
1017 association between a name, or identifier, that is used by the Scheme
1018 program code, and the variable location to which that name refers.
1019 Although the value that is stored in that location may change, the
1020 location to which a given name refers is always the same.
1021
1022 We can illustrate this by breaking down the operation of the
1023 @code{define} syntax into three parts: @code{define}
1024
1025 @itemize @bullet
1026 @item
1027 creates a new location
1028
1029 @item
1030 establishes an association between that location and the name specified
1031 as the first argument of the @code{define} expression
1032
1033 @item
1034 stores in that location the value obtained by evaluating the second
1035 argument of the @code{define} expression.
1036 @end itemize
1037
1038 A collection of associations between names and locations is called an
1039 @dfn{environment}. When you create a top level variable in a program
1040 using @code{define}, the name-location association for that variable is
1041 added to the ``top level'' environment. The ``top level'' environment
1042 also includes name-location associations for all the procedures that are
1043 supplied by standard Scheme.
1044
1045 It is also possible to create environments other than the top level one,
1046 and to create variable bindings, or name-location associations, in those
1047 environments. This ability is a key ingredient in the concept of
1048 closure; the next subsection shows how it is done.
1049
1050
1051 @node Local Variables
1052 @subsubsection Local Variables and Environments
1053
1054 @cindex local variable
1055 @cindex variable, local
1056 @cindex local environment
1057 @cindex environment, local
1058
1059 We have seen how to create top level variables using the @code{define}
1060 syntax (@pxref{Definition}). It is often useful to create variables
1061 that are more limited in their scope, typically as part of a procedure
1062 body. In Scheme, this is done using the @code{let} syntax, or one of
1063 its modified forms @code{let*} and @code{letrec}. These syntaxes are
1064 described in full later in the manual (@pxref{Local Bindings}). Here
1065 our purpose is to illustrate their use just enough that we can see how
1066 local variables work.
1067
1068 For example, the following code uses a local variable @code{s} to
1069 simplify the computation of the area of a triangle given the lengths of
1070 its three sides.
1071
1072 @lisp
1073 (define a 5.3)
1074 (define b 4.7)
1075 (define c 2.8)
1076
1077 (define area
1078 (let ((s (/ (+ a b c) 2)))
1079 (sqrt (* s (- s a) (- s b) (- s c)))))
1080 @end lisp
1081
1082 The effect of the @code{let} expression is to create a new environment
1083 and, within this environment, an association between the name @code{s}
1084 and a new location whose initial value is obtained by evaluating
1085 @code{(/ (+ a b c) 2)}. The expressions in the body of the @code{let},
1086 namely @code{(sqrt (* s (- s a) (- s b) (- s c)))}, are then evaluated
1087 in the context of the new environment, and the value of the last
1088 expression evaluated becomes the value of the whole @code{let}
1089 expression, and therefore the value of the variable @code{area}.
1090
1091
1092 @node Chaining
1093 @subsubsection Environment Chaining
1094
1095 @cindex shadowing an imported variable binding
1096 @cindex chaining environments
1097
1098 In the example of the previous subsection, we glossed over an important
1099 point. The body of the @code{let} expression in that example refers not
1100 only to the local variable @code{s}, but also to the top level variables
1101 @code{a}, @code{b}, @code{c} and @code{sqrt}. (@code{sqrt} is the
1102 standard Scheme procedure for calculating a square root.) If the body
1103 of the @code{let} expression is evaluated in the context of the
1104 @emph{local} @code{let} environment, how does the evaluation get at the
1105 values of these top level variables?
1106
1107 The answer is that the local environment created by a @code{let}
1108 expression automatically has a reference to its containing environment
1109 --- in this case the top level environment --- and that the Scheme
1110 interpreter automatically looks for a variable binding in the containing
1111 environment if it doesn't find one in the local environment. More
1112 generally, every environment except for the top level one has a
1113 reference to its containing environment, and the interpreter keeps
1114 searching back up the chain of environments --- from most local to top
1115 level --- until it either finds a variable binding for the required
1116 identifier or exhausts the chain.
1117
1118 This description also determines what happens when there is more than
1119 one variable binding with the same name. Suppose, continuing the
1120 example of the previous subsection, that there was also a pre-existing
1121 top level variable @code{s} created by the expression:
1122
1123 @lisp
1124 (define s "Some beans, my lord!")
1125 @end lisp
1126
1127 Then both the top level environment and the local @code{let} environment
1128 would contain bindings for the name @code{s}. When evaluating code
1129 within the @code{let} body, the interpreter looks first in the local
1130 @code{let} environment, and so finds the binding for @code{s} created by
1131 the @code{let} syntax. Even though this environment has a reference to
1132 the top level environment, which also has a binding for @code{s}, the
1133 interpreter doesn't get as far as looking there. When evaluating code
1134 outside the @code{let} body, the interpreter looks up variable names in
1135 the top level environment, so the name @code{s} refers to the top level
1136 variable.
1137
1138 Within the @code{let} body, the binding for @code{s} in the local
1139 environment is said to @dfn{shadow} the binding for @code{s} in the top
1140 level environment.
1141
1142
1143 @node Lexical Scope
1144 @subsubsection Lexical Scope
1145
1146 The rules that we have just been describing are the details of how
1147 Scheme implements ``lexical scoping''. This subsection takes a brief
1148 diversion to explain what lexical scope means in general and to present
1149 an example of non-lexical scoping.
1150
1151 ``Lexical scope'' in general is the idea that
1152
1153 @itemize @bullet
1154 @item
1155 an identifier at a particular place in a program always refers to the
1156 same variable location --- where ``always'' means ``every time that the
1157 containing expression is executed'', and that
1158
1159 @item
1160 the variable location to which it refers can be determined by static
1161 examination of the source code context in which that identifier appears,
1162 without having to consider the flow of execution through the program as
1163 a whole.
1164 @end itemize
1165
1166 In practice, lexical scoping is the norm for most programming languages,
1167 and probably corresponds to what you would intuitively consider to be
1168 ``normal''. You may even be wondering how the situation could possibly
1169 --- and usefully --- be otherwise. To demonstrate that another kind of
1170 scoping is possible, therefore, and to compare it against lexical
1171 scoping, the following subsection presents an example of non-lexical
1172 scoping and examines in detail how its behavior differs from the
1173 corresponding lexically scoped code.
1174
1175 @c @menu
1176 @c * Scoping Example:: An example of non-lexical scoping.
1177 @c @end menu
1178
1179
1180 @c @node Scoping Example
1181 @subsubheading An Example of Non-Lexical Scoping
1182
1183 To demonstrate that non-lexical scoping does exist and can be useful, we
1184 present the following example from Emacs Lisp, which is a ``dynamically
1185 scoped'' language.
1186
1187 @lisp
1188 (defvar currency-abbreviation "USD")
1189
1190 (defun currency-string (units hundredths)
1191 (concat currency-abbreviation
1192 (number-to-string units)
1193 "."
1194 (number-to-string hundredths)))
1195
1196 (defun french-currency-string (units hundredths)
1197 (let ((currency-abbreviation "FRF"))
1198 (currency-string units hundredths)))
1199 @end lisp
1200
1201 The question to focus on here is: what does the identifier
1202 @code{currency-abbreviation} refer to in the @code{currency-string}
1203 function? The answer, in Emacs Lisp, is that all variable bindings go
1204 onto a single stack, and that @code{currency-abbreviation} refers to the
1205 topmost binding from that stack which has the name
1206 ``currency-abbreviation''. The binding that is created by the
1207 @code{defvar} form, to the value @code{"USD"}, is only relevant if none
1208 of the code that calls @code{currency-string} rebinds the name
1209 ``currency-abbreviation'' in the meanwhile.
1210
1211 The second function @code{french-currency-string} works precisely by
1212 taking advantage of this behaviour. It creates a new binding for the
1213 name ``currency-abbreviation'' which overrides the one established by
1214 the @code{defvar} form.
1215
1216 @lisp
1217 ;; Note! This is Emacs Lisp evaluation, not Scheme!
1218 (french-currency-string 33 44)
1219 @result{}
1220 "FRF33.44"
1221 @end lisp
1222
1223 Now let's look at the corresponding, @emph{lexically scoped} Scheme
1224 code:
1225
1226 @lisp
1227 (define currency-abbreviation "USD")
1228
1229 (define (currency-string units hundredths)
1230 (string-append currency-abbreviation
1231 (number->string units)
1232 "."
1233 (number->string hundredths)))
1234
1235 (define (french-currency-string units hundredths)
1236 (let ((currency-abbreviation "FRF"))
1237 (currency-string units hundredths)))
1238 @end lisp
1239
1240 According to the rules of lexical scoping, the
1241 @code{currency-abbreviation} in @code{currency-string} refers to the
1242 variable location in the innermost environment at that point in the code
1243 which has a binding for @code{currency-abbreviation}, which is the
1244 variable location in the top level environment created by the preceding
1245 @code{(define currency-abbreviation @dots{})} expression.
1246
1247 In Scheme, therefore, the @code{french-currency-string} procedure does
1248 not work as intended. The variable binding that it creates for
1249 ``currency-abbreviation'' is purely local to the code that forms the
1250 body of the @code{let} expression. Since this code doesn't directly use
1251 the name ``currency-abbreviation'' at all, the binding is pointless.
1252
1253 @lisp
1254 (french-currency-string 33 44)
1255 @result{}
1256 "USD33.44"
1257 @end lisp
1258
1259 This begs the question of how the Emacs Lisp behaviour can be
1260 implemented in Scheme. In general, this is a design question whose
1261 answer depends upon the problem that is being addressed. In this case,
1262 the best answer may be that @code{currency-string} should be
1263 redesigned so that it can take an optional third argument. This third
1264 argument, if supplied, is interpreted as a currency abbreviation that
1265 overrides the default.
1266
1267 It is possible to change @code{french-currency-string} so that it mostly
1268 works without changing @code{currency-string}, but the fix is inelegant,
1269 and susceptible to interrupts that could leave the
1270 @code{currency-abbreviation} variable in the wrong state:
1271
1272 @lisp
1273 (define (french-currency-string units hundredths)
1274 (set! currency-abbreviation "FRF")
1275 (let ((result (currency-string units hundredths)))
1276 (set! currency-abbreviation "USD")
1277 result))
1278 @end lisp
1279
1280 The key point here is that the code does not create any local binding
1281 for the identifier @code{currency-abbreviation}, so all occurrences of
1282 this identifier refer to the top level variable.
1283
1284
1285 @node Closure
1286 @subsubsection Closure
1287
1288 Consider a @code{let} expression that doesn't contain any
1289 @code{lambda}s:
1290
1291 @lisp
1292 (let ((s (/ (+ a b c) 2)))
1293 (sqrt (* s (- s a) (- s b) (- s c))))
1294 @end lisp
1295
1296 @noindent
1297 When the Scheme interpreter evaluates this, it
1298
1299 @itemize @bullet
1300 @item
1301 creates a new environment with a reference to the environment that was
1302 current when it encountered the @code{let}
1303
1304 @item
1305 creates a variable binding for @code{s} in the new environment, with
1306 value given by @code{(/ (+ a b c) 2)}
1307
1308 @item
1309 evaluates the expression in the body of the @code{let} in the context of
1310 the new local environment, and remembers the value @code{V}
1311
1312 @item
1313 forgets the local environment
1314
1315 @item
1316 continues evaluating the expression that contained the @code{let}, using
1317 the value @code{V} as the value of the @code{let} expression, in the
1318 context of the containing environment.
1319 @end itemize
1320
1321 After the @code{let} expression has been evaluated, the local
1322 environment that was created is simply forgotten, and there is no longer
1323 any way to access the binding that was created in this environment. If
1324 the same code is evaluated again, it will follow the same steps again,
1325 creating a second new local environment that has no connection with the
1326 first, and then forgetting this one as well.
1327
1328 If the @code{let} body contains a @code{lambda} expression, however, the
1329 local environment is @emph{not} forgotten. Instead, it becomes
1330 associated with the procedure that is created by the @code{lambda}
1331 expression, and is reinstated every time that that procedure is called.
1332 In detail, this works as follows.
1333
1334 @itemize @bullet
1335 @item
1336 When the Scheme interpreter evaluates a @code{lambda} expression, to
1337 create a procedure object, it stores the current environment as part of
1338 the procedure definition.
1339
1340 @item
1341 Then, whenever that procedure is called, the interpreter reinstates the
1342 environment that is stored in the procedure definition and evaluates the
1343 procedure body within the context of that environment.
1344 @end itemize
1345
1346 The result is that the procedure body is always evaluated in the context
1347 of the environment that was current when the procedure was created.
1348
1349 This is what is meant by @dfn{closure}. The next few subsections
1350 present examples that explore the usefulness of this concept.
1351
1352
1353 @node Serial Number
1354 @subsubsection Example 1: A Serial Number Generator
1355
1356 This example uses closure to create a procedure with a variable binding
1357 that is private to the procedure, like a local variable, but whose value
1358 persists between procedure calls.
1359
1360 @lisp
1361 (define (make-serial-number-generator)
1362 (let ((current-serial-number 0))
1363 (lambda ()
1364 (set! current-serial-number (+ current-serial-number 1))
1365 current-serial-number)))
1366
1367 (define entry-sn-generator (make-serial-number-generator))
1368
1369 (entry-sn-generator)
1370 @result{}
1371 1
1372
1373 (entry-sn-generator)
1374 @result{}
1375 2
1376 @end lisp
1377
1378 When @code{make-serial-number-generator} is called, it creates a local
1379 environment with a binding for @code{current-serial-number} whose
1380 initial value is 0, then, within this environment, creates a procedure.
1381 The local environment is stored within the created procedure object and
1382 so persists for the lifetime of the created procedure.
1383
1384 Every time the created procedure is invoked, it increments the value of
1385 the @code{current-serial-number} binding in the captured environment and
1386 then returns the current value.
1387
1388 Note that @code{make-serial-number-generator} can be called again to
1389 create a second serial number generator that is independent of the
1390 first. Every new invocation of @code{make-serial-number-generator}
1391 creates a new local @code{let} environment and returns a new procedure
1392 object with an association to this environment.
1393
1394
1395 @node Shared Variable
1396 @subsubsection Example 2: A Shared Persistent Variable
1397
1398 This example uses closure to create two procedures, @code{get-balance}
1399 and @code{deposit}, that both refer to the same captured local
1400 environment so that they can both access the @code{balance} variable
1401 binding inside that environment. The value of this variable binding
1402 persists between calls to either procedure.
1403
1404 Note that the captured @code{balance} variable binding is private to
1405 these two procedures: it is not directly accessible to any other code.
1406 It can only be accessed indirectly via @code{get-balance} or
1407 @code{deposit}, as illustrated by the @code{withdraw} procedure.
1408
1409 @lisp
1410 (define get-balance #f)
1411 (define deposit #f)
1412
1413 (let ((balance 0))
1414 (set! get-balance
1415 (lambda ()
1416 balance))
1417 (set! deposit
1418 (lambda (amount)
1419 (set! balance (+ balance amount))
1420 balance)))
1421
1422 (define (withdraw amount)
1423 (deposit (- amount)))
1424
1425 (get-balance)
1426 @result{}
1427 0
1428
1429 (deposit 50)
1430 @result{}
1431 50
1432
1433 (withdraw 75)
1434 @result{}
1435 -25
1436 @end lisp
1437
1438 An important detail here is that the @code{get-balance} and
1439 @code{deposit} variables must be set up by @code{define}ing them at top
1440 level and then @code{set!}ing their values inside the @code{let} body.
1441 Using @code{define} within the @code{let} body would not work: this
1442 would create variable bindings within the local @code{let} environment
1443 that would not be accessible at top level.
1444
1445
1446 @node Callback Closure
1447 @subsubsection Example 3: The Callback Closure Problem
1448
1449 A frequently used programming model for library code is to allow an
1450 application to register a callback function for the library to call when
1451 some particular event occurs. It is often useful for the application to
1452 make several such registrations using the same callback function, for
1453 example if several similar library events can be handled using the same
1454 application code, but the need then arises to distinguish the callback
1455 function calls that are associated with one callback registration from
1456 those that are associated with different callback registrations.
1457
1458 In languages without the ability to create functions dynamically, this
1459 problem is usually solved by passing a @code{user_data} parameter on the
1460 registration call, and including the value of this parameter as one of
1461 the parameters on the callback function. Here is an example of
1462 declarations using this solution in C:
1463
1464 @example
1465 typedef void (event_handler_t) (int event_type,
1466 void *user_data);
1467
1468 void register_callback (int event_type,
1469 event_handler_t *handler,
1470 void *user_data);
1471 @end example
1472
1473 In Scheme, closure can be used to achieve the same functionality without
1474 requiring the library code to store a @code{user-data} for each callback
1475 registration.
1476
1477 @lisp
1478 ;; In the library:
1479
1480 (define (register-callback event-type handler-proc)
1481 @dots{})
1482
1483 ;; In the application:
1484
1485 (define (make-handler event-type user-data)
1486 (lambda ()
1487 @dots{}
1488 <code referencing event-type and user-data>
1489 @dots{}))
1490
1491 (register-callback event-type
1492 (make-handler event-type @dots{}))
1493 @end lisp
1494
1495 As far as the library is concerned, @code{handler-proc} is a procedure
1496 with no arguments, and all the library has to do is call it when the
1497 appropriate event occurs. From the application's point of view, though,
1498 the handler procedure has used closure to capture an environment that
1499 includes all the context that the handler code needs ---
1500 @code{event-type} and @code{user-data} --- to handle the event
1501 correctly.
1502
1503
1504 @node OO Closure
1505 @subsubsection Example 4: Object Orientation
1506
1507 Closure is the capture of an environment, containing persistent variable
1508 bindings, within the definition of a procedure or a set of related
1509 procedures. This is rather similar to the idea in some object oriented
1510 languages of encapsulating a set of related data variables inside an
1511 ``object'', together with a set of ``methods'' that operate on the
1512 encapsulated data. The following example shows how closure can be used
1513 to emulate the ideas of objects, methods and encapsulation in Scheme.
1514
1515 @lisp
1516 (define (make-account)
1517 (let ((balance 0))
1518 (define (get-balance)
1519 balance)
1520 (define (deposit amount)
1521 (set! balance (+ balance amount))
1522 balance)
1523 (define (withdraw amount)
1524 (deposit (- amount)))
1525
1526 (lambda args
1527 (apply
1528 (case (car args)
1529 ((get-balance) get-balance)
1530 ((deposit) deposit)
1531 ((withdraw) withdraw)
1532 (else (error "Invalid method!")))
1533 (cdr args)))))
1534 @end lisp
1535
1536 Each call to @code{make-account} creates and returns a new procedure,
1537 created by the expression in the example code that begins ``(lambda
1538 args''.
1539
1540 @lisp
1541 (define my-account (make-account))
1542
1543 my-account
1544 @result{}
1545 #<procedure args>
1546 @end lisp
1547
1548 This procedure acts as an account object with methods
1549 @code{get-balance}, @code{deposit} and @code{withdraw}. To apply one of
1550 the methods to the account, you call the procedure with a symbol
1551 indicating the required method as the first parameter, followed by any
1552 other parameters that are required by that method.
1553
1554 @lisp
1555 (my-account 'get-balance)
1556 @result{}
1557 0
1558
1559 (my-account 'withdraw 5)
1560 @result{}
1561 -5
1562
1563 (my-account 'deposit 396)
1564 @result{}
1565 391
1566
1567 (my-account 'get-balance)
1568 @result{}
1569 391
1570 @end lisp
1571
1572 Note how, in this example, both the current balance and the helper
1573 procedures @code{get-balance}, @code{deposit} and @code{withdraw}, used
1574 to implement the guts of the account object's methods, are all stored in
1575 variable bindings within the private local environment captured by the
1576 @code{lambda} expression that creates the account object procedure.
1577
1578
1579 @c Local Variables:
1580 @c TeX-master: "guile.texi"
1581 @c End: