diff options
| author | Stefan Monnier | 2021-09-13 09:14:05 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2021-09-13 09:14:05 -0400 |
| commit | e4a187ca5908f5e151fd42b9ab7b72bc5a9bfc2a (patch) | |
| tree | 80bb3ee65430899d1eb4c5998d74a2f055aaaf67 | |
| parent | feadcae139fbf34add3f957bde4aa44da529cb6f (diff) | |
| download | emacs-e4a187ca5908f5e151fd42b9ab7b72bc5a9bfc2a.tar.gz emacs-e4a187ca5908f5e151fd42b9ab7b72bc5a9bfc2a.zip | |
* doc/lispref/variables.texi (named-let): Document TCO
| -rw-r--r-- | doc/lispref/variables.texi | 34 |
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 |
| 306 | This special form is like @code{let}: It binds the variables in | 306 | This special form is a looping construct inspired from the |
| 307 | Scheme 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 |
| 310 | local function whose formal arguments are the variables in @var{bindings} | ||
| 311 | and whose body is @var{body}. This allows @var{body} to call itself | ||
| 312 | recursively 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 |
| 310 | new values of the bound variables in the recursive invocation. | 314 | new values of the bound variables in the recursive invocation. |
| 311 | 315 | ||
| 312 | Here's a trivial example: | 316 | Example 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} | ||
| 328 | Recursive calls to @var{name} that occur in @emph{tail | ||
| 329 | positions} in @var{body} are guaranteed to be optimised as @emph{tail | ||
| 330 | calls}, which means that they will not consume any additional stack | ||
| 331 | space no matter how deeply the recursion runs. Such recursive calls | ||
| 332 | will effectively jump to the top of the loop with new values for the | ||
| 333 | variables. | ||
| 334 | |||
| 335 | A function call is in the tail position if it's the very last thing | ||
| 336 | done so that the value returned by the call is the value of @var{body} | ||
| 337 | itself, 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 |