diff options
| author | Paul Eggert | 2020-04-18 12:59:17 -0700 |
|---|---|---|
| committer | Paul Eggert | 2020-04-18 13:01:08 -0700 |
| commit | eebfb72c906755c0a80d92c11deee7ac9faf5f4b (patch) | |
| tree | 01337499496e5024a1d0bfd52ae6f6574812559e /doc | |
| parent | 6c187ed6b0a2b103ebb55a5f037073c8c31492b3 (diff) | |
| download | emacs-eebfb72c906755c0a80d92c11deee7ac9faf5f4b.tar.gz emacs-eebfb72c906755c0a80d92c11deee7ac9faf5f4b.zip | |
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias EngdegÄrd
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispintro/emacs-lisp-intro.texi | 32 | ||||
| -rw-r--r-- | doc/lispref/edebug.texi | 2 | ||||
| -rw-r--r-- | doc/lispref/keymaps.texi | 8 | ||||
| -rw-r--r-- | doc/lispref/lists.texi | 60 | ||||
| -rw-r--r-- | doc/lispref/sequences.texi | 31 | ||||
| -rw-r--r-- | doc/lispref/strings.texi | 17 |
6 files changed, 91 insertions, 59 deletions
diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi index bd688070a3a..630676d9786 100644 --- a/doc/lispintro/emacs-lisp-intro.texi +++ b/doc/lispintro/emacs-lisp-intro.texi | |||
| @@ -2329,7 +2329,7 @@ area. | |||
| 2329 | 2329 | ||
| 2330 | @cindex @samp{bind} defined | 2330 | @cindex @samp{bind} defined |
| 2331 | There are several ways by which a variable can be given a value. One of | 2331 | There are several ways by which a variable can be given a value. One of |
| 2332 | the ways is to use either the function @code{set} or the function | 2332 | the ways is to use either the function @code{set} or the special form |
| 2333 | @code{setq}. Another way is to use @code{let} (@pxref{let}). (The | 2333 | @code{setq}. Another way is to use @code{let} (@pxref{let}). (The |
| 2334 | jargon for this process is to @dfn{bind} a variable to a value.) | 2334 | jargon for this process is to @dfn{bind} a variable to a value.) |
| 2335 | 2335 | ||
| @@ -4517,7 +4517,7 @@ number; it will be printed as the character with that @sc{ascii} code. | |||
| 4517 | 4517 | ||
| 4518 | @item setq | 4518 | @item setq |
| 4519 | @itemx set | 4519 | @itemx set |
| 4520 | The @code{setq} function sets the value of its first argument to the | 4520 | The @code{setq} special form sets the value of its first argument to the |
| 4521 | value of the second argument. The first argument is automatically | 4521 | value of the second argument. The first argument is automatically |
| 4522 | quoted by @code{setq}. It does the same for succeeding pairs of | 4522 | quoted by @code{setq}. It does the same for succeeding pairs of |
| 4523 | arguments. Another function, @code{set}, takes only two arguments and | 4523 | arguments. Another function, @code{set}, takes only two arguments and |
| @@ -7317,11 +7317,21 @@ which leave the original list as it was. One way to find out how this | |||
| 7317 | works is to experiment. We will start with the @code{setcar} function. | 7317 | works is to experiment. We will start with the @code{setcar} function. |
| 7318 | 7318 | ||
| 7319 | @need 1200 | 7319 | @need 1200 |
| 7320 | @cindex constant lists | ||
| 7321 | @cindex mutable lists | ||
| 7320 | First, we can make a list and then set the value of a variable to the | 7322 | First, we can make a list and then set the value of a variable to the |
| 7321 | list, using the @code{setq} function. Here is a list of animals: | 7323 | list, using the @code{setq} special form. Because we intend to use |
| 7324 | @code{setcar} to change the list, this @code{setq} should not use the | ||
| 7325 | quoted form @code{'(antelope giraffe lion tiger)}, as that would yield | ||
| 7326 | a list that is part of the program and bad things could happen if we | ||
| 7327 | tried to change part of the program while running it. Generally | ||
| 7328 | speaking an Emacs Lisp program's components should be constant (or | ||
| 7329 | unchanged) while the program is running. So we instead construct an | ||
| 7330 | animal list that is @dfn{mutable} (or changeable) by using the | ||
| 7331 | @code{list} function, as follows: | ||
| 7322 | 7332 | ||
| 7323 | @smallexample | 7333 | @smallexample |
| 7324 | (setq animals '(antelope giraffe lion tiger)) | 7334 | (setq animals (list 'antelope 'giraffe 'lion 'tiger)) |
| 7325 | @end smallexample | 7335 | @end smallexample |
| 7326 | 7336 | ||
| 7327 | @noindent | 7337 | @noindent |
| @@ -7398,7 +7408,7 @@ To see how this works, set the value of the variable to a list of | |||
| 7398 | domesticated animals by evaluating the following expression: | 7408 | domesticated animals by evaluating the following expression: |
| 7399 | 7409 | ||
| 7400 | @smallexample | 7410 | @smallexample |
| 7401 | (setq domesticated-animals '(horse cow sheep goat)) | 7411 | (setq domesticated-animals (list 'horse 'cow 'sheep 'goat)) |
| 7402 | @end smallexample | 7412 | @end smallexample |
| 7403 | 7413 | ||
| 7404 | @need 1200 | 7414 | @need 1200 |
| @@ -8846,7 +8856,7 @@ and then find the value of @code{trees}: | |||
| 8846 | 8856 | ||
| 8847 | @smallexample | 8857 | @smallexample |
| 8848 | @group | 8858 | @group |
| 8849 | (setq trees '(maple oak pine birch)) | 8859 | (setq trees (list 'maple 'oak 'pine 'birch)) |
| 8850 | @result{} (maple oak pine birch) | 8860 | @result{} (maple oak pine birch) |
| 8851 | @end group | 8861 | @end group |
| 8852 | 8862 | ||
| @@ -9366,7 +9376,7 @@ For example: | |||
| 9366 | 9376 | ||
| 9367 | @smallexample | 9377 | @smallexample |
| 9368 | @group | 9378 | @group |
| 9369 | (setq triple '(1 2 3)) | 9379 | (setq triple (list 1 2 3)) |
| 9370 | 9380 | ||
| 9371 | (setcar triple '37) | 9381 | (setcar triple '37) |
| 9372 | 9382 | ||
| @@ -9547,7 +9557,7 @@ part of which is the address of the next pair. The very last box | |||
| 9547 | points to the symbol @code{nil}, which marks the end of the list. | 9557 | points to the symbol @code{nil}, which marks the end of the list. |
| 9548 | 9558 | ||
| 9549 | @need 1200 | 9559 | @need 1200 |
| 9550 | When a variable is set to a list with a function such as @code{setq}, | 9560 | When a variable is set to a list via @code{setq}, |
| 9551 | it stores the address of the first box in the variable. Thus, | 9561 | it stores the address of the first box in the variable. Thus, |
| 9552 | evaluation of the expression | 9562 | evaluation of the expression |
| 9553 | 9563 | ||
| @@ -17092,7 +17102,7 @@ reminders. | |||
| 17092 | 17102 | ||
| 17093 | @cindex Mail aliases | 17103 | @cindex Mail aliases |
| 17094 | @noindent | 17104 | @noindent |
| 17095 | This @code{setq} command sets the value of the variable | 17105 | This @code{setq} sets the value of the variable |
| 17096 | @code{mail-aliases} to @code{t}. Since @code{t} means true, the line | 17106 | @code{mail-aliases} to @code{t}. Since @code{t} means true, the line |
| 17097 | says, in effect, ``Yes, use mail aliases.'' | 17107 | says, in effect, ``Yes, use mail aliases.'' |
| 17098 | 17108 | ||
| @@ -17130,8 +17140,8 @@ The following turns off Indent Tabs mode: | |||
| 17130 | @end smallexample | 17140 | @end smallexample |
| 17131 | 17141 | ||
| 17132 | Note that this line uses @code{setq-default} rather than the | 17142 | Note that this line uses @code{setq-default} rather than the |
| 17133 | @code{setq} command that we have seen before. The @code{setq-default} | 17143 | @code{setq} that we have seen before. The @code{setq-default} |
| 17134 | command sets values only in buffers that do not have their own local | 17144 | sets values only in buffers that do not have their own local |
| 17135 | values for the variable. | 17145 | values for the variable. |
| 17136 | 17146 | ||
| 17137 | @ifinfo | 17147 | @ifinfo |
diff --git a/doc/lispref/edebug.texi b/doc/lispref/edebug.texi index 8be8307c75f..ec76e83db1c 100644 --- a/doc/lispref/edebug.texi +++ b/doc/lispref/edebug.texi | |||
| @@ -858,7 +858,7 @@ to a non-@code{nil} value. | |||
| 858 | Here is an example of code that creates a circular structure: | 858 | Here is an example of code that creates a circular structure: |
| 859 | 859 | ||
| 860 | @example | 860 | @example |
| 861 | (setq a '(x y)) | 861 | (setq a (list 'x 'y)) |
| 862 | (setcar a a) | 862 | (setcar a a) |
| 863 | @end example | 863 | @end example |
| 864 | 864 | ||
diff --git a/doc/lispref/keymaps.texi b/doc/lispref/keymaps.texi index c6a02d721f0..4db9969767c 100644 --- a/doc/lispref/keymaps.texi +++ b/doc/lispref/keymaps.texi | |||
| @@ -1441,10 +1441,10 @@ Here is an example showing a keymap before and after substitution: | |||
| 1441 | 1441 | ||
| 1442 | @smallexample | 1442 | @smallexample |
| 1443 | @group | 1443 | @group |
| 1444 | (setq map '(keymap | 1444 | (setq map (list 'keymap |
| 1445 | (?1 . olddef-1) | 1445 | (cons ?1 olddef-1) |
| 1446 | (?2 . olddef-2) | 1446 | (cons ?2 olddef-2) |
| 1447 | (?3 . olddef-1))) | 1447 | (cons ?3 olddef-1))) |
| 1448 | @result{} (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1)) | 1448 | @result{} (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1)) |
| 1449 | @end group | 1449 | @end group |
| 1450 | 1450 | ||
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index 27fa5385e35..c2771b01652 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi | |||
| @@ -866,10 +866,16 @@ foo ;; @r{@code{foo} was changed.} | |||
| 866 | @node Modifying Lists | 866 | @node Modifying Lists |
| 867 | @section Modifying Existing List Structure | 867 | @section Modifying Existing List Structure |
| 868 | @cindex destructive list operations | 868 | @cindex destructive list operations |
| 869 | @cindex constant lists | ||
| 870 | @cindex mutable lists | ||
| 869 | 871 | ||
| 870 | You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the | 872 | You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the |
| 871 | primitives @code{setcar} and @code{setcdr}. These are destructive | 873 | primitives @code{setcar} and @code{setcdr}. These are destructive |
| 872 | operations because they change existing list structure. | 874 | operations because they change existing list structure. |
| 875 | Destructive operations should be applied only to @dfn{mutable} lists, | ||
| 876 | that is, lists constructed via @code{cons}, @code{list} or similar | ||
| 877 | operations. Lists created by quoting are constants and should not be | ||
| 878 | changed by destructive operations. | ||
| 873 | 879 | ||
| 874 | @cindex CL note---@code{rplaca} vs @code{setcar} | 880 | @cindex CL note---@code{rplaca} vs @code{setcar} |
| 875 | @quotation | 881 | @quotation |
| @@ -906,7 +912,7 @@ value @var{object}. For example: | |||
| 906 | 912 | ||
| 907 | @example | 913 | @example |
| 908 | @group | 914 | @group |
| 909 | (setq x '(1 2)) | 915 | (setq x (list 1 2)) |
| 910 | @result{} (1 2) | 916 | @result{} (1 2) |
| 911 | @end group | 917 | @end group |
| 912 | @group | 918 | @group |
| @@ -927,7 +933,7 @@ these lists. Here is an example: | |||
| 927 | @example | 933 | @example |
| 928 | @group | 934 | @group |
| 929 | ;; @r{Create two lists that are partly shared.} | 935 | ;; @r{Create two lists that are partly shared.} |
| 930 | (setq x1 '(a b c)) | 936 | (setq x1 (list 'a 'b 'c)) |
| 931 | @result{} (a b c) | 937 | @result{} (a b c) |
| 932 | (setq x2 (cons 'z (cdr x1))) | 938 | (setq x2 (cons 'z (cdr x1))) |
| 933 | @result{} (z b c) | 939 | @result{} (z b c) |
| @@ -1017,7 +1023,7 @@ reached via the @sc{cdr}. | |||
| 1017 | 1023 | ||
| 1018 | @example | 1024 | @example |
| 1019 | @group | 1025 | @group |
| 1020 | (setq x '(1 2 3)) | 1026 | (setq x (list 1 2 3)) |
| 1021 | @result{} (1 2 3) | 1027 | @result{} (1 2 3) |
| 1022 | @end group | 1028 | @end group |
| 1023 | @group | 1029 | @group |
| @@ -1037,7 +1043,7 @@ the @sc{cdr} of the first cons cell: | |||
| 1037 | 1043 | ||
| 1038 | @example | 1044 | @example |
| 1039 | @group | 1045 | @group |
| 1040 | (setq x1 '(a b c)) | 1046 | (setq x1 (list 'a 'b 'c)) |
| 1041 | @result{} (a b c) | 1047 | @result{} (a b c) |
| 1042 | (setcdr x1 (cdr (cdr x1))) | 1048 | (setcdr x1 (cdr (cdr x1))) |
| 1043 | @result{} (c) | 1049 | @result{} (c) |
| @@ -1069,7 +1075,7 @@ of this list. | |||
| 1069 | 1075 | ||
| 1070 | @example | 1076 | @example |
| 1071 | @group | 1077 | @group |
| 1072 | (setq x1 '(a b c)) | 1078 | (setq x1 (list 'a 'b 'c)) |
| 1073 | @result{} (a b c) | 1079 | @result{} (a b c) |
| 1074 | (setcdr x1 (cons 'd (cdr x1))) | 1080 | (setcdr x1 (cons 'd (cdr x1))) |
| 1075 | @result{} (d b c) | 1081 | @result{} (d b c) |
| @@ -1130,7 +1136,7 @@ Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are | |||
| 1130 | 1136 | ||
| 1131 | @example | 1137 | @example |
| 1132 | @group | 1138 | @group |
| 1133 | (setq x '(1 2 3)) | 1139 | (setq x (list 1 2 3)) |
| 1134 | @result{} (1 2 3) | 1140 | @result{} (1 2 3) |
| 1135 | @end group | 1141 | @end group |
| 1136 | @group | 1142 | @group |
| @@ -1150,7 +1156,7 @@ list: | |||
| 1150 | 1156 | ||
| 1151 | @example | 1157 | @example |
| 1152 | @group | 1158 | @group |
| 1153 | (setq x '(1 2 3)) | 1159 | (setq x (list 1 2 3)) |
| 1154 | @result{} (1 2 3) | 1160 | @result{} (1 2 3) |
| 1155 | @end group | 1161 | @end group |
| 1156 | @group | 1162 | @group |
| @@ -1163,11 +1169,13 @@ x | |||
| 1163 | @end group | 1169 | @end group |
| 1164 | @end example | 1170 | @end example |
| 1165 | 1171 | ||
| 1166 | However, the other arguments (all but the last) must be lists. | 1172 | However, the other arguments (all but the last) must be mutable lists. |
| 1167 | 1173 | ||
| 1168 | A common pitfall is to use a quoted constant list as a non-last | 1174 | A common pitfall is to use a quoted constant list as a non-last |
| 1169 | argument to @code{nconc}. If you do this, your program will change | 1175 | argument to @code{nconc}. If you do this, the resulting behavior |
| 1170 | each time you run it! Here is what happens: | 1176 | is undefined. It is possible that your program will change |
| 1177 | each time you run it! Here is what might happen (though this | ||
| 1178 | is not guaranteed to happen): | ||
| 1171 | 1179 | ||
| 1172 | @smallexample | 1180 | @smallexample |
| 1173 | @group | 1181 | @group |
| @@ -1260,7 +1268,9 @@ after those elements. For example: | |||
| 1260 | 1268 | ||
| 1261 | @example | 1269 | @example |
| 1262 | @group | 1270 | @group |
| 1263 | (delq 'a '(a b c)) @equiv{} (cdr '(a b c)) | 1271 | (equal |
| 1272 | (delq 'a (list 'a 'b 'c)) | ||
| 1273 | (cdr (list 'a 'b 'c))) | ||
| 1264 | @end group | 1274 | @end group |
| 1265 | @end example | 1275 | @end example |
| 1266 | 1276 | ||
| @@ -1270,7 +1280,7 @@ removing it involves changing the @sc{cdr}s (@pxref{Setcdr}). | |||
| 1270 | 1280 | ||
| 1271 | @example | 1281 | @example |
| 1272 | @group | 1282 | @group |
| 1273 | (setq sample-list '(a b c (4))) | 1283 | (setq sample-list (list 'a 'b 'c '(4))) |
| 1274 | @result{} (a b c (4)) | 1284 | @result{} (a b c (4)) |
| 1275 | @end group | 1285 | @end group |
| 1276 | @group | 1286 | @group |
| @@ -1303,12 +1313,12 @@ into the variable that held the original list: | |||
| 1303 | (setq flowers (delq 'rose flowers)) | 1313 | (setq flowers (delq 'rose flowers)) |
| 1304 | @end example | 1314 | @end example |
| 1305 | 1315 | ||
| 1306 | In the following example, the @code{(4)} that @code{delq} attempts to match | 1316 | In the following example, the @code{(list 4)} that @code{delq} attempts to match |
| 1307 | and the @code{(4)} in the @code{sample-list} are not @code{eq}: | 1317 | and the @code{(4)} in the @code{sample-list} are @code{equal} but not @code{eq}: |
| 1308 | 1318 | ||
| 1309 | @example | 1319 | @example |
| 1310 | @group | 1320 | @group |
| 1311 | (delq '(4) sample-list) | 1321 | (delq (list 4) sample-list) |
| 1312 | @result{} (a c (4)) | 1322 | @result{} (a c (4)) |
| 1313 | @end group | 1323 | @end group |
| 1314 | @end example | 1324 | @end example |
| @@ -1324,7 +1334,7 @@ of @code{list}. | |||
| 1324 | 1334 | ||
| 1325 | @example | 1335 | @example |
| 1326 | @group | 1336 | @group |
| 1327 | (setq sample-list '(a b c a b c)) | 1337 | (setq sample-list (list 'a 'b 'c 'a 'b 'c)) |
| 1328 | @result{} (a b c a b c) | 1338 | @result{} (a b c a b c) |
| 1329 | @end group | 1339 | @end group |
| 1330 | @group | 1340 | @group |
| @@ -1353,7 +1363,7 @@ Compare this with @code{memq}: | |||
| 1353 | @result{} (1.2 1.3) | 1363 | @result{} (1.2 1.3) |
| 1354 | @end group | 1364 | @end group |
| 1355 | @group | 1365 | @group |
| 1356 | (memq 1.2 '(1.1 1.2 1.3)) ; @r{@code{1.2} and @code{1.2} are not @code{eq}.} | 1366 | (memq (list 2) '((1) (2))) ; @r{@code{(list 2)} and @code{(2)} are not @code{eq}.} |
| 1357 | @result{} nil | 1367 | @result{} nil |
| 1358 | @end group | 1368 | @end group |
| 1359 | @end example | 1369 | @end example |
| @@ -1373,11 +1383,11 @@ Compare this with @code{memq}: | |||
| 1373 | 1383 | ||
| 1374 | @example | 1384 | @example |
| 1375 | @group | 1385 | @group |
| 1376 | (member '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are @code{equal}.} | 1386 | (member (list 2) '((1) (2))) ; @r{@code{(list 2)} and @code{(2)} are @code{equal}.} |
| 1377 | @result{} ((2)) | 1387 | @result{} ((2)) |
| 1378 | @end group | 1388 | @end group |
| 1379 | @group | 1389 | @group |
| 1380 | (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.} | 1390 | (memq (list 2) '((1) (2))) ; @r{@code{(list 2)} and @code{(2)} are not @code{eq}.} |
| 1381 | @result{} nil | 1391 | @result{} nil |
| 1382 | @end group | 1392 | @end group |
| 1383 | @group | 1393 | @group |
| @@ -1407,7 +1417,7 @@ For example: | |||
| 1407 | 1417 | ||
| 1408 | @example | 1418 | @example |
| 1409 | @group | 1419 | @group |
| 1410 | (setq l '((2) (1) (2))) | 1420 | (setq l (list '(2) '(1) '(2))) |
| 1411 | (delete '(2) l) | 1421 | (delete '(2) l) |
| 1412 | @result{} ((1)) | 1422 | @result{} ((1)) |
| 1413 | l | 1423 | l |
| @@ -1416,7 +1426,7 @@ l | |||
| 1416 | ;; @r{write @code{(setq l (delete '(2) l))}.} | 1426 | ;; @r{write @code{(setq l (delete '(2) l))}.} |
| 1417 | @end group | 1427 | @end group |
| 1418 | @group | 1428 | @group |
| 1419 | (setq l '((2) (1) (2))) | 1429 | (setq l (list '(2) '(1) '(2))) |
| 1420 | (delete '(1) l) | 1430 | (delete '(1) l) |
| 1421 | @result{} ((2) (2)) | 1431 | @result{} ((2) (2)) |
| 1422 | l | 1432 | l |
| @@ -1618,9 +1628,9 @@ keys may not be symbols: | |||
| 1618 | '(("simple leaves" . oak) | 1628 | '(("simple leaves" . oak) |
| 1619 | ("compound leaves" . horsechestnut))) | 1629 | ("compound leaves" . horsechestnut))) |
| 1620 | 1630 | ||
| 1621 | (assq "simple leaves" leaves) | 1631 | (assq (copy-sequence "simple leaves") leaves) |
| 1622 | @result{} nil | 1632 | @result{} nil |
| 1623 | (assoc "simple leaves" leaves) | 1633 | (assoc (copy-sequence "simple leaves") leaves) |
| 1624 | @result{} ("simple leaves" . oak) | 1634 | @result{} ("simple leaves" . oak) |
| 1625 | @end smallexample | 1635 | @end smallexample |
| 1626 | @end defun | 1636 | @end defun |
| @@ -1759,7 +1769,7 @@ correct results, use the return value of @code{assq-delete-all} rather | |||
| 1759 | than looking at the saved value of @var{alist}. | 1769 | than looking at the saved value of @var{alist}. |
| 1760 | 1770 | ||
| 1761 | @example | 1771 | @example |
| 1762 | (setq alist '((foo 1) (bar 2) (foo 3) (lose 4))) | 1772 | (setq alist (list '(foo 1) '(bar 2) '(foo 3) '(lose 4))) |
| 1763 | @result{} ((foo 1) (bar 2) (foo 3) (lose 4)) | 1773 | @result{} ((foo 1) (bar 2) (foo 3) (lose 4)) |
| 1764 | (assq-delete-all 'foo alist) | 1774 | (assq-delete-all 'foo alist) |
| 1765 | @result{} ((bar 2) (lose 4)) | 1775 | @result{} ((bar 2) (lose 4)) |
| @@ -1926,7 +1936,7 @@ function returns the modified property list, so you can store that back | |||
| 1926 | in the place where you got @var{plist}. For example, | 1936 | in the place where you got @var{plist}. For example, |
| 1927 | 1937 | ||
| 1928 | @example | 1938 | @example |
| 1929 | (setq my-plist '(bar t foo 4)) | 1939 | (setq my-plist (list 'bar t 'foo 4)) |
| 1930 | @result{} (bar t foo 4) | 1940 | @result{} (bar t foo 4) |
| 1931 | (setq my-plist (plist-put my-plist 'foo 69)) | 1941 | (setq my-plist (plist-put my-plist 'foo 69)) |
| 1932 | @result{} (bar t foo 69) | 1942 | @result{} (bar t foo 69) |
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 1a3a04f680b..f6faf9448c2 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -183,7 +183,7 @@ for other ways to copy sequences. | |||
| 183 | 183 | ||
| 184 | @example | 184 | @example |
| 185 | @group | 185 | @group |
| 186 | (setq bar '(1 2)) | 186 | (setq bar (list 1 2)) |
| 187 | @result{} (1 2) | 187 | @result{} (1 2) |
| 188 | @end group | 188 | @end group |
| 189 | @group | 189 | @group |
| @@ -278,7 +278,7 @@ Unlike @code{reverse} the original @var{sequence} may be modified. | |||
| 278 | 278 | ||
| 279 | @example | 279 | @example |
| 280 | @group | 280 | @group |
| 281 | (setq x '(a b c)) | 281 | (setq x (list 'a 'b 'c)) |
| 282 | @result{} (a b c) | 282 | @result{} (a b c) |
| 283 | @end group | 283 | @end group |
| 284 | @group | 284 | @group |
| @@ -320,7 +320,7 @@ presented graphically: | |||
| 320 | For the vector, it is even simpler because you don't need setq: | 320 | For the vector, it is even simpler because you don't need setq: |
| 321 | 321 | ||
| 322 | @example | 322 | @example |
| 323 | (setq x [1 2 3 4]) | 323 | (setq x (copy-sequence [1 2 3 4])) |
| 324 | @result{} [1 2 3 4] | 324 | @result{} [1 2 3 4] |
| 325 | (nreverse x) | 325 | (nreverse x) |
| 326 | @result{} [4 3 2 1] | 326 | @result{} [4 3 2 1] |
| @@ -330,7 +330,7 @@ x | |||
| 330 | 330 | ||
| 331 | Note that unlike @code{reverse}, this function doesn't work with strings. | 331 | Note that unlike @code{reverse}, this function doesn't work with strings. |
| 332 | Although you can alter string data by using @code{aset}, it is strongly | 332 | Although you can alter string data by using @code{aset}, it is strongly |
| 333 | encouraged to treat strings as immutable. | 333 | encouraged to treat strings as immutable even when they are mutable. |
| 334 | 334 | ||
| 335 | @end defun | 335 | @end defun |
| 336 | 336 | ||
| @@ -374,11 +374,11 @@ appears in a different position in the list due to the change of | |||
| 374 | 374 | ||
| 375 | @example | 375 | @example |
| 376 | @group | 376 | @group |
| 377 | (setq nums '(1 3 2 6 5 4 0)) | 377 | (setq nums (list 1 3 2 6 5 4 0)) |
| 378 | @result{} (1 3 2 6 5 4 0) | 378 | @result{} (1 3 2 6 5 4 0) |
| 379 | @end group | 379 | @end group |
| 380 | @group | 380 | @group |
| 381 | (sort nums '<) | 381 | (sort nums #'<) |
| 382 | @result{} (0 1 2 3 4 5 6) | 382 | @result{} (0 1 2 3 4 5 6) |
| 383 | @end group | 383 | @end group |
| 384 | @group | 384 | @group |
| @@ -396,7 +396,7 @@ of @code{sort} and use that. Most often we store the result back into | |||
| 396 | the variable that held the original list: | 396 | the variable that held the original list: |
| 397 | 397 | ||
| 398 | @example | 398 | @example |
| 399 | (setq nums (sort nums '<)) | 399 | (setq nums (sort nums #'<)) |
| 400 | @end example | 400 | @end example |
| 401 | 401 | ||
| 402 | For the better understanding of what stable sort is, consider the following | 402 | For the better understanding of what stable sort is, consider the following |
| @@ -1228,7 +1228,7 @@ This function sets the @var{index}th element of @var{array} to be | |||
| 1228 | 1228 | ||
| 1229 | @example | 1229 | @example |
| 1230 | @group | 1230 | @group |
| 1231 | (setq w [foo bar baz]) | 1231 | (setq w (vector 'foo 'bar 'baz)) |
| 1232 | @result{} [foo bar baz] | 1232 | @result{} [foo bar baz] |
| 1233 | (aset w 0 'fu) | 1233 | (aset w 0 'fu) |
| 1234 | @result{} fu | 1234 | @result{} fu |
| @@ -1237,7 +1237,8 @@ w | |||
| 1237 | @end group | 1237 | @end group |
| 1238 | 1238 | ||
| 1239 | @group | 1239 | @group |
| 1240 | (setq x "asdfasfd") | 1240 | ;; @r{@code{copy-sequence} creates a mutable string.} |
| 1241 | (setq x (copy-sequence "asdfasfd")) | ||
| 1241 | @result{} "asdfasfd" | 1242 | @result{} "asdfasfd" |
| 1242 | (aset x 3 ?Z) | 1243 | (aset x 3 ?Z) |
| 1243 | @result{} 90 | 1244 | @result{} 90 |
| @@ -1246,6 +1247,10 @@ x | |||
| 1246 | @end group | 1247 | @end group |
| 1247 | @end example | 1248 | @end example |
| 1248 | 1249 | ||
| 1250 | The @var{array} should be mutable; that is, it should not be a constant, | ||
| 1251 | such as the constants created via quoting or via self-evaluating forms. | ||
| 1252 | @xref{Self-Evaluating Forms}. | ||
| 1253 | |||
| 1249 | If @var{array} is a string and @var{object} is not a character, a | 1254 | If @var{array} is a string and @var{object} is not a character, a |
| 1250 | @code{wrong-type-argument} error results. The function converts a | 1255 | @code{wrong-type-argument} error results. The function converts a |
| 1251 | unibyte string to multibyte if necessary to insert a character. | 1256 | unibyte string to multibyte if necessary to insert a character. |
| @@ -1257,7 +1262,7 @@ each element of @var{array} is @var{object}. It returns @var{array}. | |||
| 1257 | 1262 | ||
| 1258 | @example | 1263 | @example |
| 1259 | @group | 1264 | @group |
| 1260 | (setq a [a b c d e f g]) | 1265 | (setq a (copy-sequence [a b c d e f g])) |
| 1261 | @result{} [a b c d e f g] | 1266 | @result{} [a b c d e f g] |
| 1262 | (fillarray a 0) | 1267 | (fillarray a 0) |
| 1263 | @result{} [0 0 0 0 0 0 0] | 1268 | @result{} [0 0 0 0 0 0 0] |
| @@ -1265,7 +1270,7 @@ a | |||
| 1265 | @result{} [0 0 0 0 0 0 0] | 1270 | @result{} [0 0 0 0 0 0 0] |
| 1266 | @end group | 1271 | @end group |
| 1267 | @group | 1272 | @group |
| 1268 | (setq s "When in the course") | 1273 | (setq s (copy-sequence "When in the course")) |
| 1269 | @result{} "When in the course" | 1274 | @result{} "When in the course" |
| 1270 | (fillarray s ?-) | 1275 | (fillarray s ?-) |
| 1271 | @result{} "------------------" | 1276 | @result{} "------------------" |
| @@ -1301,7 +1306,9 @@ same way in Lisp input. | |||
| 1301 | 1306 | ||
| 1302 | A vector, like a string or a number, is considered a constant for | 1307 | A vector, like a string or a number, is considered a constant for |
| 1303 | evaluation: the result of evaluating it is the same vector. This does | 1308 | evaluation: the result of evaluating it is the same vector. This does |
| 1304 | not evaluate or even examine the elements of the vector. | 1309 | not evaluate or even examine the elements of the vector. Vectors |
| 1310 | written with square brackets are constants and should not be modified | ||
| 1311 | via @code{aset} or other destructive operations. | ||
| 1305 | @xref{Self-Evaluating Forms}. | 1312 | @xref{Self-Evaluating Forms}. |
| 1306 | 1313 | ||
| 1307 | Here are examples illustrating these principles: | 1314 | Here are examples illustrating these principles: |
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 14cabc5d79d..3acbf538dce 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -51,10 +51,8 @@ by a distinguished character code. | |||
| 51 | operate on them with the general array and sequence functions documented | 51 | operate on them with the general array and sequence functions documented |
| 52 | in @ref{Sequences Arrays Vectors}. For example, you can access or | 52 | in @ref{Sequences Arrays Vectors}. For example, you can access or |
| 53 | change individual characters in a string using the functions @code{aref} | 53 | change individual characters in a string using the functions @code{aref} |
| 54 | and @code{aset} (@pxref{Array Functions}). However, note that | 54 | and @code{aset} (@pxref{Array Functions}). However, you should not |
| 55 | @code{length} should @emph{not} be used for computing the width of a | 55 | try to change the contents of constant strings (@pxref{Modifying Strings}). |
| 56 | string on display; use @code{string-width} (@pxref{Size of Displayed | ||
| 57 | Text}) instead. | ||
| 58 | 56 | ||
| 59 | There are two text representations for non-@acronym{ASCII} | 57 | There are two text representations for non-@acronym{ASCII} |
| 60 | characters in Emacs strings (and in buffers): unibyte and multibyte. | 58 | characters in Emacs strings (and in buffers): unibyte and multibyte. |
| @@ -89,6 +87,9 @@ copy them into buffers. @xref{Character Type}, and @ref{String Type}, | |||
| 89 | for information about the syntax of characters and strings. | 87 | for information about the syntax of characters and strings. |
| 90 | @xref{Non-ASCII Characters}, for functions to convert between text | 88 | @xref{Non-ASCII Characters}, for functions to convert between text |
| 91 | representations and to encode and decode character codes. | 89 | representations and to encode and decode character codes. |
| 90 | Also, note that @code{length} should @emph{not} be used for computing | ||
| 91 | the width of a string on display; use @code{string-width} (@pxref{Size | ||
| 92 | of Displayed Text}) instead. | ||
| 92 | 93 | ||
| 93 | @node Predicates for Strings | 94 | @node Predicates for Strings |
| 94 | @section Predicates for Strings | 95 | @section Predicates for Strings |
| @@ -380,6 +381,10 @@ usual value is @w{@code{"[ \f\t\n\r\v]+"}}. | |||
| 380 | @cindex modifying strings | 381 | @cindex modifying strings |
| 381 | @cindex string modification | 382 | @cindex string modification |
| 382 | 383 | ||
| 384 | You can alter the contents of a mutable string via operations | ||
| 385 | described in this section. However, you should not try to use these | ||
| 386 | operations to alter the contents of a constant string. | ||
| 387 | |||
| 383 | The most basic way to alter the contents of an existing string is with | 388 | The most basic way to alter the contents of an existing string is with |
| 384 | @code{aset} (@pxref{Array Functions}). @code{(aset @var{string} | 389 | @code{aset} (@pxref{Array Functions}). @code{(aset @var{string} |
| 385 | @var{idx} @var{char})} stores @var{char} into @var{string} at index | 390 | @var{idx} @var{char})} stores @var{char} into @var{string} at index |
| @@ -591,7 +596,7 @@ for sorting (@pxref{Sequence Functions}): | |||
| 591 | 596 | ||
| 592 | @example | 597 | @example |
| 593 | @group | 598 | @group |
| 594 | (sort '("11" "12" "1 1" "1 2" "1.1" "1.2") 'string-collate-lessp) | 599 | (sort (list "11" "12" "1 1" "1 2" "1.1" "1.2") 'string-collate-lessp) |
| 595 | @result{} ("11" "1 1" "1.1" "12" "1 2" "1.2") | 600 | @result{} ("11" "1 1" "1.1" "12" "1 2" "1.2") |
| 596 | @end group | 601 | @end group |
| 597 | @end example | 602 | @end example |
| @@ -608,7 +613,7 @@ systems. The @var{locale} value of @code{"POSIX"} or @code{"C"} lets | |||
| 608 | 613 | ||
| 609 | @example | 614 | @example |
| 610 | @group | 615 | @group |
| 611 | (sort '("11" "12" "1 1" "1 2" "1.1" "1.2") | 616 | (sort (list "11" "12" "1 1" "1 2" "1.1" "1.2") |
| 612 | (lambda (s1 s2) (string-collate-lessp s1 s2 "POSIX"))) | 617 | (lambda (s1 s2) (string-collate-lessp s1 s2 "POSIX"))) |
| 613 | @result{} ("1 1" "1 2" "1.1" "1.2" "11" "12") | 618 | @result{} ("1 1" "1 2" "1.1" "1.2" "11" "12") |
| 614 | @end group | 619 | @end group |