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