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