mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-30 15:00:21 +02:00
doc: Fix use of literals in alist example
Fixes https://bugs.gnu.org/32841. * doc/ref/srfi-modules.texi (alist-copy): Add anchor. * doc/ref/api-data.texi (Alist Example): Fix use of literals.
This commit is contained in:
parent
faa8ab8a88
commit
818b879b2e
2 changed files with 101 additions and 76 deletions
|
@ -9513,6 +9513,8 @@ independent from the list that results from modification by
|
||||||
use @code{list-copy} to copy the old association list before modifying
|
use @code{list-copy} to copy the old association list before modifying
|
||||||
it.
|
it.
|
||||||
|
|
||||||
|
@rnindex acons
|
||||||
|
@anchor{x-acons}
|
||||||
@deffn {Scheme Procedure} acons key value alist
|
@deffn {Scheme Procedure} acons key value alist
|
||||||
@deffnx {C Function} scm_acons (key, value, alist)
|
@deffnx {C Function} scm_acons (key, value, alist)
|
||||||
Add a new key-value pair to @var{alist}. A new pair is
|
Add a new key-value pair to @var{alist}. A new pair is
|
||||||
|
@ -9719,13 +9721,34 @@ Recommended only for use in Guile internals.
|
||||||
@node Alist Example
|
@node Alist Example
|
||||||
@subsubsection Alist Example
|
@subsubsection Alist Example
|
||||||
|
|
||||||
Here is a longer example of how alists may be used in practice.
|
The following example shows how alists may be used in practice.
|
||||||
|
|
||||||
@lisp
|
@lisp
|
||||||
(define capitals '(("New York" . "Albany")
|
(define capitals (list (cons "New York" "Albany")
|
||||||
("Oregon" . "Salem")
|
(cons "Oregon" "Salem")
|
||||||
("Florida" . "Miami")))
|
(cons "Florida" "Miami")))
|
||||||
|
@end lisp
|
||||||
|
|
||||||
|
Other ways to create an alist are
|
||||||
|
@lisp
|
||||||
|
(define capitals (@ref{x-acons,@code{acons}} "New York" "Albany"
|
||||||
|
(acons "Oregon" "Salem"
|
||||||
|
(acons "Florida" "Miami" '()))))
|
||||||
|
@end lisp
|
||||||
|
or
|
||||||
|
@lisp
|
||||||
|
(use-modules (srfi srfi-1)) ; for alist-copy
|
||||||
|
(define capitals (@ref{x-alist-copy,@code{alist-copy}}
|
||||||
|
'(("New York" . "Albany")
|
||||||
|
("Oregon" . "Salem")
|
||||||
|
("Florida" . "Miami"))))
|
||||||
|
@end lisp
|
||||||
|
|
||||||
|
Here @code{alist-copy} is necessary if we intend to modify the alist, because a literal like @code{'(("New York" . "Albany") ...)} cannot be modified.
|
||||||
|
|
||||||
|
We can now operate on the alist.
|
||||||
|
|
||||||
|
@lisp
|
||||||
;; What's the capital of Oregon?
|
;; What's the capital of Oregon?
|
||||||
(assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
|
(assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
|
||||||
(assoc-ref capitals "Oregon") @result{} "Salem"
|
(assoc-ref capitals "Oregon") @result{} "Salem"
|
||||||
|
|
|
@ -1038,6 +1038,8 @@ return the result. This is equivalent to
|
||||||
core does the same thing.
|
core does the same thing.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
@rnindex alist-copy
|
||||||
|
@anchor{x-alist-copy}
|
||||||
@deffn {Scheme Procedure} alist-copy alist
|
@deffn {Scheme Procedure} alist-copy alist
|
||||||
Return a newly allocated copy of @var{alist}, that means that the
|
Return a newly allocated copy of @var{alist}, that means that the
|
||||||
spine of the list as well as the pairs are copied.
|
spine of the list as well as the pairs are copied.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue