elisp @@ macro
[bpt/guile.git] / doc / ref / scheme-ideas.texi
index 98ed953..15cf664 100644 (file)
@@ -1,6 +1,11 @@
-@page
-@node Basic Ideas
-@chapter Basic Ideas in Scheme
+@c -*-texinfo-*-
+@c This is part of the GNU Guile Reference Manual.
+@c Copyright (C)  1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2012
+@c   Free Software Foundation, Inc.
+@c See the file guile.texi for copying conditions.
+
+@node Hello Scheme!
+@chapter Hello Scheme!
 
 In this chapter, we introduce the basic concepts that underpin the
 elegance and power of the Scheme language.
@@ -9,17 +14,19 @@ Readers who already possess a background knowledge of Scheme may happily
 skip this chapter.  For the reader who is new to the language, however,
 the following discussions on data, procedures, expressions and closure
 are designed to provide a minimum level of Scheme understanding that is
-more or less assumed by the reference chapters that follow.
+more or less assumed by the chapters that follow.
 
-The style of this introductory material aims about halfway between the
-terse precision of R5RS and the discursive randomness of a Scheme
-tutorial.
+The style of this introductory material aims about halfway between the terse
+precision of R5RS and the discursiveness of existing Scheme tutorials.  For
+pointers to useful Scheme resources on the web, please see @ref{Further
+Reading}.
 
 @menu
 * About Data::                  Latent typing, types, values and variables.
 * About Procedures::            The representation and use of procedures.
 * About Expressions::           All kinds of expressions and their meaning.
 * About Closure::               Closure, scoping and environments.
+* Further Reading::             Where to find out more about Scheme.
 @end menu
 
 
@@ -385,9 +392,13 @@ this:
 
 @noindent
 This is a valid procedure invocation expression, and its result is the
-string @code{"Name=FSF:Address=Cambridge"}.
+string:
+
+@lisp
+"Name=FSF:Address=Cambridge"
+@end lisp
 
-It it more common, though, to store the procedure value in a variable ---
+It is more common, though, to store the procedure value in a variable ---
 
 @lisp
 (define make-combined-string
@@ -465,6 +476,11 @@ The corresponding forms of the alternative @code{define} syntax are:
 @noindent
 For details on how these forms work, see @xref{Lambda}.
 
+Prior to Guile 2.0, Guile provided an extension to @code{define} syntax
+that allowed you to nest the previous extension up to an arbitrary
+depth. These are no longer provided by default, and instead have been
+moved to @ref{Curried Definitions}
+
 (It could be argued that the alternative @code{define} forms are rather
 confusing, especially for newcomers to the Scheme language, as they hide
 both the role of @code{lambda} and the fact that procedures are values
@@ -503,6 +519,7 @@ expressions.
 
 @menu
 * Evaluating::                  How a Scheme program is executed.
+* Tail Calls::                  Space-safe recursion.
 * The REPL::                    Interacting with the Guile interpreter.
 * Syntax Summary::              Common syntactic expressions -- in brief.
 @end menu
@@ -539,9 +556,10 @@ and then by describing the value and side effects of evaluation for each
 type of expression individually.
 
 @noindent
-So, some@footnote{These definitions are approximate.  For the whole and
-detailed truth, see @xref{Formal syntax and semantics,R5RS
-syntax,,r5rs}.} definitions@dots{}
+So, some@footnote{These definitions are approximate.  For the whole
+and detailed truth, see @ref{Formal syntax and semantics,R5RS
+syntax,,r5rs,The Revised(5) Report on the Algorithmic Language
+Scheme}.} definitions@dots{}
 
 @itemize @bullet
 
@@ -778,6 +796,122 @@ individually for each special syntax.  For a summary of standard special
 syntax, see @xref{Syntax Summary}.
 
 
+@node Tail Calls
+@subsection Tail calls
+@cindex tail calls
+@cindex recursion
+
+Scheme is ``properly tail recursive'', meaning that tail calls or
+recursions from certain contexts do not consume stack space or other
+resources and can therefore be used on arbitrarily large data or for
+an arbitrarily long calculation.  Consider for example,
+
+@example
+(define (foo n)
+  (display n)
+  (newline)
+  (foo (1+ n)))
+
+(foo 1)
+@print{}
+1
+2
+3
+@dots{}
+@end example
+
+@code{foo} prints numbers infinitely, starting from the given @var{n}.
+It's implemented by printing @var{n} then recursing to itself to print
+@math{@var{n}+1} and so on.  This recursion is a tail call, it's the
+last thing done, and in Scheme such tail calls can be made without
+limit.
+
+Or consider a case where a value is returned, a version of the SRFI-1
+@code{last} function (@pxref{SRFI-1 Selectors}) returning the last
+element of a list,
+
+@example
+(define (my-last lst)
+  (if (null? (cdr lst))
+      (car lst)
+      (my-last (cdr lst))))
+
+(my-last '(1 2 3)) @result{} 3      
+@end example
+
+If the list has more than one element, @code{my-last} applies itself
+to the @code{cdr}.  This recursion is a tail call, there's no code
+after it, and the return value is the return value from that call.  In
+Scheme this can be used on an arbitrarily long list argument.
+
+@sp 1
+A proper tail call is only available from certain contexts, namely the
+following special form positions,
+
+@itemize @bullet
+@item
+@code{and} --- last expression
+
+@item
+@code{begin} --- last expression
+     
+@item
+@code{case} --- last expression in each clause
+
+@item
+@code{cond} --- last expression in each clause, and the call to a
+@code{=>} procedure is a tail call
+
+@item
+@code{do} --- last result expression
+
+@item
+@code{if} --- ``true'' and ``false'' leg expressions
+
+@item
+@code{lambda} --- last expression in body
+
+@item
+@code{let}, @code{let*}, @code{letrec}, @code{let-syntax},
+@code{letrec-syntax} --- last expression in body
+
+@item
+@code{or} --- last expression
+@end itemize
+
+@noindent
+The following core functions make tail calls,
+
+@itemize @bullet
+@item
+@code{apply} --- tail call to given procedure
+
+@item
+@code{call-with-current-continuation} --- tail call to the procedure
+receiving the new continuation
+
+@item
+@code{call-with-values} --- tail call to the values-receiving
+procedure
+
+@item
+@code{eval} --- tail call to evaluate the form
+
+@item
+@code{string-any}, @code{string-every} --- tail call to predicate on
+the last character (if that point is reached)
+@end itemize
+
+@sp 1
+The above are just core functions and special forms.  Tail calls in
+other modules are described with the relevant documentation, for
+example SRFI-1 @code{any} and @code{every} (@pxref{SRFI-1 Searching}).
+
+It will be noted there are a lot of places which could potentially be
+tail calls, for instance the last call in a @code{for-each}, but only
+those explicitly described are guaranteed.
+
+
 @node The REPL
 @subsection Using the Guile REPL
 
@@ -835,11 +969,11 @@ same as a procedure which returns its last argument, because the
 evaluation of a procedure invocation expression does not guarantee to
 evaluate the arguments in order.
 
-@code{if} and @code{cond} (@pxref{if cond case}) provide conditional
+@code{if} and @code{cond} (@pxref{Conditionals}) provide conditional
 evaluation of argument expressions depending on whether one or more
 conditions evaluate to ``true'' or ``false''.
 
-@code{case} (@pxref{if cond case}) provides conditional evaluation of
+@code{case} (@pxref{Conditionals}) provides conditional evaluation of
 argument expressions depending on whether a variable has one of a
 specified group of values.
 
@@ -1053,7 +1187,7 @@ corresponding lexically scoped code.
 @menu
 * Scoping Example::             An example of non-lexical scoping.
 @end menu
-
+                                   
 
 @node Scoping Example
 @subsubsection An Example of Non-Lexical Scoping