aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2021-09-13 09:14:05 -0400
committerStefan Monnier2021-09-13 09:14:05 -0400
commite4a187ca5908f5e151fd42b9ab7b72bc5a9bfc2a (patch)
tree80bb3ee65430899d1eb4c5998d74a2f055aaaf67
parentfeadcae139fbf34add3f957bde4aa44da529cb6f (diff)
downloademacs-e4a187ca5908f5e151fd42b9ab7b72bc5a9bfc2a.tar.gz
emacs-e4a187ca5908f5e151fd42b9ab7b72bc5a9bfc2a.zip
* doc/lispref/variables.texi (named-let): Document TCO
-rw-r--r--doc/lispref/variables.texi34
1 files changed, 25 insertions, 9 deletions
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index 9646daab7ad..a1d1919b4bf 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -303,22 +303,38 @@ the forms, and then make the variables non-special again.
303@end defspec 303@end defspec
304 304
305@defspec named-let name bindings &rest body 305@defspec named-let name bindings &rest body
306This special form is like @code{let}: It binds the variables in 306This special form is a looping construct inspired from the
307Scheme language. It is similar to @code{let}: It binds the variables in
307@var{bindings}, and then evaluates @var{body}. However, 308@var{bindings}, and then evaluates @var{body}. However,
308@code{named-let} allows you to call @var{body} recursively by calling 309@code{named-let} also binds @var{name} to a
310local function whose formal arguments are the variables in @var{bindings}
311and whose body is @var{body}. This allows @var{body} to call itself
312recursively by calling
309@var{name}, where the arguments passed to @var{name} are used as the 313@var{name}, where the arguments passed to @var{name} are used as the
310new values of the bound variables in the recursive invocation. 314new values of the bound variables in the recursive invocation.
311 315
312Here's a trivial example: 316Example of a loop summing a list of numbers:
313 317
314@lisp 318@lisp
315(named-let foo 319(named-let sum ((numbers '(1 2 3 4))
316 ((a 1) 320 (running-sum 0))
317 (b 2)) 321 (if numbers
318 (nconc (list a b) 322 (sum (cdr numbers) (+ running-sum (car numbers)))
319 (and (= a 1) (foo 3 4)))) 323 running-sum))
320 @result{} (1 2 3 4) 324@result{} 10
321@end lisp 325@end lisp
326
327@anchor{Tail recursion}
328Recursive calls to @var{name} that occur in @emph{tail
329positions} in @var{body} are guaranteed to be optimised as @emph{tail
330calls}, which means that they will not consume any additional stack
331space no matter how deeply the recursion runs. Such recursive calls
332will effectively jump to the top of the loop with new values for the
333variables.
334
335A function call is in the tail position if it's the very last thing
336done so that the value returned by the call is the value of @var{body}
337itself, as is the case in the recursive call to @code{sum} above.
322@end defspec 338@end defspec
323 339
324 Here is a complete list of the other facilities that create local 340 Here is a complete list of the other facilities that create local