comparison man/lispref/lists.texi @ 5359:f5a5501814f5

Document the CL set functions and #'eql in the Lispref, not just cl.texi man/ChangeLog addition: 2011-02-19 Aidan Kehoe <kehoea@parhasard.net> * lispref/lists.texi (Sets And Lists): Document #'member*, #'remove*, #'delete* in this file. Document #'memq, #'member, #'remq, #'remove, #'delq, #'delete in terms of the former functions. Document #'subsetp, #'union, #'intersection, #'set-difference, #'set-exclusive-or and their destructive analogues in this file. * lispref/lists.texi (Association Lists): Document #'assoc*, #'rassoc* in this file. Document #'assq, #'assoc, #'rassq, #'rassoc in terms of the first two functions. * lispref/objects.texi (Equality Predicates): Document #'eql here, don't leave it to cl.texi. src/ChangeLog addition: 2011-02-19 Aidan Kehoe <kehoea@parhasard.net> * fns.c (Fset_exclusive_or): This function accepts the :stable keyword too, document this in its arglist.
author Aidan Kehoe <kehoea@parhasard.net>
date Sat, 19 Feb 2011 11:03:46 +0000
parents 9f738305f80f
children 62b9ef1ed4ac
comparison
equal deleted inserted replaced
5358:31475de17064 5359:f5a5501814f5
1144 @cindex lists as sets 1144 @cindex lists as sets
1145 @cindex sets 1145 @cindex sets
1146 1146
1147 A list can represent an unordered mathematical set---simply consider a 1147 A list can represent an unordered mathematical set---simply consider a
1148 value an element of a set if it appears in the list, and ignore the 1148 value an element of a set if it appears in the list, and ignore the
1149 order of the list. To form the union of two sets, use @code{append} (as 1149 order of the list. XEmacs provides set operations inherited from Common
1150 long as you don't mind having duplicate elements). Other useful 1150 Lisp.
1151 functions for sets include @code{memq} and @code{delq}, and their 1151
1152 @code{equal} versions, @code{member} and @code{delete}. 1152 @defun member* item list @t{&key :test :test-not :key}
1153 1153 This function tests to see whether @var{item} is a member of @var{list},
1154 @cindex CL note---lack @code{union}, @code{set} 1154 comparing with @code{eql}. If it is, @code{member*} returns the tail of
1155 @quotation 1155 @var{list} starting with the first occurrence of @var{item}. Otherwise,
1156 @b{Common Lisp note:} Common Lisp has functions @code{union} (which 1156 it returns @code{nil}.
1157 avoids duplicate elements) and @code{intersection} for set operations, 1157
1158 but XEmacs Lisp does not have them. You can write them in Lisp if 1158 This is equivalent to the Common Lisp @code{member} function, but that
1159 you wish. 1159 name was already taken in Emacs Lisp, whence the asterisk at the end of
1160 @end quotation 1160 @code{member*}.
1161 1161
1162 @defun memq object list 1162 The @code{:test} keyword argument allows you to specify the test used to
1163 @cindex membership in a list 1163 decide whether @var{item} is equivalent to a given element of
1164 This function tests to see whether @var{object} is a member of 1164 @var{list}. The function should return non-@code{nil} if the items
1165 @var{list}. If it is, @code{memq} returns a list starting with the 1165 match, @code{nil} if they do not. The @code{:test-not} keyword is
1166 first occurrence of @var{object}. Otherwise, it returns @code{nil}. 1166 similar, but the meaning of @code{nil} and non-@code{nil} results are
1167 The letter @samp{q} in @code{memq} says that it uses @code{eq} to 1167 reversed. The @code{:key} keyword allows you to examine a component of
1168 compare @var{object} against the elements of the list. For example: 1168 each object in @var{list}, rather than the object itself.
1169 1169
1170 @example 1170 @example
1171 @group 1171 @group
1172 (memq 'b '(a b c b a)) 1172 (member* 'b '(a b c b a))
1173 @result{} (b c b a) 1173 @result{} (b c b a)
1174 @end group 1174 @end group
1175 @group 1175 @group
1176 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.} 1176 (member* '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eql}.}
1177 @result{} nil 1177 @result{} nil
1178 @end group 1178 @end group
1179 @end example 1179 @group
1180 @end defun 1180 (member* '(2) '((1) (2)) :test #'equal) ; @r{but they are @code{equal}.}
1181 1181 @result{} ((2))
1182 @defun delq object list 1182 @end group
1183 @cindex deletion of elements 1183 @group
1184 This function destructively removes all elements @code{eq} to 1184 (member* 3 '((1) (2) (3) (4)) :key 'car) ; @r{key not applied to @var{item}}
1185 @var{object} from @var{list}. The letter @samp{q} in @code{delq} says 1185 @result{} ((3) (4))
1186 that it uses @code{eq} to compare @var{object} against the elements of 1186 @end group
1187 the list, like @code{memq}. 1187 @end example
1188 @end defun 1188 @end defun
1189 1189
1190 When @code{delq} deletes elements from the front of the list, it does so 1190 @defun memq item list
1191 simply by advancing down the list and returning a sublist that starts 1191 This is equivalent to calling @code{(member* item list :test 'eq)}, but
1192 after those elements: 1192 for historical reasons is more common in the XEmacs code base. Both
1193 1193 expressions compile to the same byte-code.
1194 @example 1194 @end defun
1195 @group 1195
1196 (delq 'a '(a b c)) @equiv{} (cdr '(a b c)) 1196 @defun member item list
1197 This is equivalent to calling @code{(member* item list :test 'equal)}.
1198 @end defun
1199
1200 @defun remove* item sequence @t{&key (test #'eql) (key #'identity) (start 0) (end (length sequence)) from-end count test-not}
1201 @cindex removal of elements
1202
1203 This function removes all occurrences of @var{object} from
1204 @var{sequence}, which can be a list, vector, or bit-vector.
1205
1206 The @code{:test} keyword argument allows you to specify the test used to
1207 decide whether @var{item} is equivalent to a given element of
1208 @var{sequence}. The function should return non-@code{nil} if the items
1209 match, @code{nil} if they do not. The @code{:test-not} keyword is
1210 similar, but the meaning of @code{nil} and non-@code{nil} results are
1211 reversed. The @code{:key} keyword allows you to examine a component of
1212 each object in @var{sequence}, rather than the object itself.
1213
1214 The @code{:start} and @code{:end} keywords allow you to specify a
1215 zero-based subrange of @var{sequence} to operate on, @code{remove*} will
1216 call the test function on all items of @var{sequence} between the index
1217 specified by @code{:start}, inclusive, and @code{:end},
1218 exclusive. @code{:count} gives a maximum number of items to remove, and
1219 @code{:from-end}, most useful in combination with @code{:count},
1220 specifies that the removal should start from the end of @var{sequence}.
1221
1222 As with @code{member*}, this function is equivalent to the Common Lisp
1223 function of almost the same name (the Common Lisp function has no
1224 asterisk.)
1225
1226 When @code{remove*} removes elements from the front of a list
1227 @var{sequence}, it does so simply by advancing down the list and
1228 returning a sublist that starts after those elements:
1229
1230 @example
1231 @group
1232 (remove* 'a '(a b c)) @equiv{} (cdr '(a b c))
1197 @end group 1233 @end group
1198 @end example 1234 @end example
1199 1235
1200 When an element to be deleted appears in the middle of the list, 1236 When an element to be deleted appears in the middle of the list,
1201 removing it involves changing the @sc{cdr}s (@pxref{Setcdr}). 1237 removing it involves copying the list conses up to that point, and
1238 setting the tail of the copied list to the tail of the original list
1239 past that point.
1202 1240
1203 @example 1241 @example
1204 @group 1242 @group
1205 (setq sample-list '(a b c (4))) 1243 (setq sample-list '(a b c (4)))
1206 @result{} (a b c (4)) 1244 @result{} (a b c (4))
1207 @end group 1245 @end group
1208 @group 1246 @group
1209 (delq 'a sample-list) 1247 (remove* 'a sample-list)
1210 @result{} (b c (4)) 1248 @result{} (b c (4))
1211 @end group 1249 @end group
1212 @group 1250 @group
1213 sample-list 1251 sample-list
1214 @result{} (a b c (4)) 1252 @result{} (a b c (4))
1215 @end group 1253 @end group
1216 @group 1254 @group
1217 (delq 'c sample-list) 1255 (remove* 'c sample-list)
1256 @result{} (a b (4))
1257 @end group
1258 @group
1259 sample-list
1260 @result{} (a b c (4))
1261 @end group
1262 @end example
1263
1264 Don't assume that a variable which formerly held the argument @var{list}
1265 now has fewer elements, or that it still holds the original list!
1266 Instead, save the result of @code{remove*} and use that. Most often we
1267 store the result back into the variable that held the original list:
1268
1269 @example
1270 (setq flowers (remove* 'rose flowers))
1271 @end example
1272
1273 In the following example, the @code{(4)} that @code{remove*} attempts to match
1274 and the @code{(4)} in the @code{sample-list} are not @code{eq}:
1275
1276 @example
1277 @group
1278 (remove* '(4) sample-list)
1279 @result{} (a b c (4))
1280 @end group
1281 @end example
1282 @end defun
1283
1284 @defun remq item sequence
1285 This is equivalent to calling @code{(remove* item sequence :test #'eq)}.
1286 @end defun
1287
1288 @defun remove item sequence
1289 This is equivalent to calling @code{(remove* item sequence :test #'equal)}.
1290 @end defun
1291
1292 @defun delete* item sequence @t{&key (test #'eql) (key #'identity) (start 0) (end (length sequence)) from-end count test-not}
1293 This is like @code{remove*}, but a list @var{sequence} is modified
1294 in-place (`destructively', in Lisp parlance). So some of the examples
1295 above change:
1296
1297 @example
1298 @group
1299 (setq sample-list '(a b c (4)))
1300 @result{} (a b c (4))
1301 @end group
1302 @group
1303 (delete* 'c sample-list)
1218 @result{} (a b (4)) 1304 @result{} (a b (4))
1219 @end group 1305 @end group
1220 @group 1306 @group
1221 sample-list 1307 sample-list
1222 @result{} (a b (4)) 1308 @result{} (a b (4))
1223 @end group 1309 @end group
1224 @end example 1310 @end example
1225 1311 @end defun
1226 Note that @code{(delq 'c sample-list)} modifies @code{sample-list} to 1312
1227 splice out the third element, but @code{(delq 'a sample-list)} does not 1313 @defun delq item sequence
1228 splice anything---it just returns a shorter list. Don't assume that a 1314 This is equivalent to calling @code{(delete* item sequence :test #'eq)}.
1229 variable which formerly held the argument @var{list} now has fewer 1315 @end defun
1230 elements, or that it still holds the original list! Instead, save the 1316
1231 result of @code{delq} and use that. Most often we store the result back 1317 @defun delete item list
1232 into the variable that held the original list: 1318 This is equivalent to calling @code{(delete* item sequence :test #'equal)}.
1233 1319 @end defun
1234 @example 1320
1235 (setq flowers (delq 'rose flowers)) 1321 @defun subsetp list1 list2 @t{&key :test :test-not :key}
1236 @end example 1322 This function returns non-@code{nil} if every item in @var{list1} is
1237 1323 present in @var{list2}.
1238 In the following example, the @code{(4)} that @code{delq} attempts to match 1324 @end defun
1239 and the @code{(4)} in the @code{sample-list} are not @code{eq}: 1325
1240 1326 @defun union list1 list2 @t{&key :test :test-not :key :stable}
1241 @example 1327 This function calculates the union of two lists, returning a list
1242 @group 1328 containing all those items that appear in either list. It doesn't
1243 (delq '(4) sample-list) 1329 guarantee that duplicates in @var{list1} or @var{list2} will be
1244 @result{} (a c (4)) 1330 eliminated; see @code{remove-duplicates} if this is important to you.
1245 @end group 1331
1246 @end example 1332 A non-nil value for the @code{:stable} keyword, not specified by Common
1247 1333 Lisp, means return the items in the order they appear in @var{list1},
1248 The following two functions are like @code{memq} and @code{delq} but use 1334 followed by the remaining items in the order they appear in @var{list2}.
1249 @code{equal} rather than @code{eq} to compare elements. They are new in 1335 The other keywords are as in @code{member*}.
1250 Emacs 19. 1336
1251 1337 @code{union} does not modify @var{list1} or @var{list2}.
1252 @defun member object list 1338 @end defun
1253 The function @code{member} tests to see whether @var{object} is a member 1339
1254 of @var{list}, comparing members with @var{object} using @code{equal}. 1340 @defun intersection list1 list2 @t{&key :test :test-not :key :stable}
1255 If @var{object} is a member, @code{member} returns a list starting with 1341 This function calculates the intersection of two lists, returning a list
1256 its first occurrence in @var{list}. Otherwise, it returns @code{nil}. 1342 containing all those items that appear in both lists. It doesn't
1257 1343 guarantee that duplicates in @var{list1} or @var{list2} will be
1258 Compare this with @code{memq}: 1344 eliminated; see @code{remove-duplicates} if this is important to
1259 1345 you. @code{intersection} does not modify either list.
1260 @example 1346
1261 @group 1347 A non-nil value for the @code{:stable} keyword, not specified by Common
1262 (member '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are @code{equal}.} 1348 Lisp, means return the items in the order they appear in @var{list1}.
1263 @result{} ((2)) 1349 The other keywords are as in @code{member*}.
1264 @end group 1350 @end defun
1265 @group 1351
1266 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.} 1352 @defun set-difference list1 list2 @t{&key :test :test-not :key :stable}
1267 @result{} nil 1353 This function returns those items that are in @var{list1} but not in
1268 @end group 1354 @var{list2}. It does not modify either list.
1269 @group 1355
1270 ;; @r{Two strings with the same contents are @code{equal}.} 1356 A non-nil value for the @code{:stable} keyword, not specified by Common
1271 (member "foo" '("foo" "bar")) 1357 Lisp, means return the items in the order they appear in @var{list1}.
1272 @result{} ("foo" "bar") 1358 The other keywords are as in @code{member*}.
1273 @end group 1359 @end defun
1274 @end example 1360
1275 @end defun 1361 @defun set-exclusive-or list1 list2 @t{&key :test :test-not :key :stable}
1276 1362 This function returns those items that are in @var{list1} but not in
1277 @defun delete object list 1363 @var{list2}, together with those in @var{list2} but not in @var{list1}.
1278 This function destructively removes all elements @code{equal} to 1364 It does not modify either list.
1279 @var{object} from @var{list}. It is to @code{delq} as @code{member} is 1365
1280 to @code{memq}: it uses @code{equal} to compare elements with 1366 A non-nil value for the @code{:stable} keyword, not specified by Common
1281 @var{object}, like @code{member}; when it finds an element that matches, 1367 Lisp, means return the items in the order they appear in @var{list1},
1282 it removes the element just as @code{delq} would. For example: 1368 followed by the remaining items in the order they appear in @var{list2}.
1283 1369 The other keywords are as in @code{member*}.
1284 @example 1370 @end defun
1285 @group 1371
1286 (delete '(2) '((2) (1) (2))) 1372 The following functions are equivalent to the previous four functions,
1287 @result{} '((1)) 1373 but with two important differences; they do not accept the
1288 @end group 1374 @code{:stable} keyword, and they modify one or both list arguments in
1289 @end example 1375 the same way @code{delete*} does.
1290 @end defun 1376
1291 1377 @defun nintersection list1 list2 @t{&key :test :test-not :key}
1292 @quotation 1378 @end defun
1293 @b{Common Lisp note:} The functions @code{member} and @code{delete} in 1379 @defun nset-difference list1 list2 @t{&key :test :test-not :key}
1294 XEmacs Lisp are derived from Maclisp, not Common Lisp. The Common 1380 @end defun
1295 Lisp versions do not use @code{equal} to compare elements. 1381 @defun nset-exclusive-or list1 list2 @t{&key :test :test-not :key}
1296 @end quotation 1382 @end defun
1383 @defun nunion list1 list2 @t{&key :test :test-not :key}
1384 @end defun
1297 1385
1298 See also the function @code{add-to-list}, in @ref{Setting Variables}, 1386 See also the function @code{add-to-list}, in @ref{Setting Variables},
1299 for another way to add an element to a list stored in a variable. 1387 for another way to add an element to a list stored in a variable.
1300 1388
1301 @node Association Lists 1389 @node Association Lists
1368 Note that property lists are similar to association lists in several 1456 Note that property lists are similar to association lists in several
1369 respects. A property list behaves like an association list in which 1457 respects. A property list behaves like an association list in which
1370 each key can occur only once. @xref{Property Lists}, for a comparison 1458 each key can occur only once. @xref{Property Lists}, for a comparison
1371 of property lists and association lists. 1459 of property lists and association lists.
1372 1460
1373 @defun assoc key alist 1461 @defun assoc* key alist @t{&key :test :test-not :key}
1374 This function returns the first association for @var{key} in 1462 This function returns the first association for @var{key} in
1375 @var{alist}. It compares @var{key} against the alist elements using 1463 @var{alist}. It compares @var{key} against the alist elements using
1376 @code{equal} (@pxref{Equality Predicates}). It returns @code{nil} if no 1464 @code{eql} (@pxref{Equality Predicates}), or the test specified with the
1377 association in @var{alist} has a @sc{car} @code{equal} to @var{key}. 1465 @code{:test} keyword. It returns @code{nil} if no association in
1378 For example: 1466 @var{alist} has a @sc{car} @code{equal} to @var{key}. For example:
1379 1467
1380 @smallexample 1468 @smallexample
1381 (setq trees '((pine . cones) (oak . acorns) (maple . seeds))) 1469 (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
1382 @result{} ((pine . cones) (oak . acorns) (maple . seeds)) 1470 @result{} ((pine . cones) (oak . acorns) (maple . seeds))
1383 (assoc 'oak trees) 1471 (assoc* 'oak trees)
1384 @result{} (oak . acorns) 1472 @result{} (oak . acorns)
1385 (cdr (assoc 'oak trees)) 1473 (cdr (assoc* 'oak trees))
1386 @result{} acorns 1474 @result{} acorns
1387 (assoc 'birch trees) 1475 (assoc* 'birch trees)
1388 @result{} nil 1476 @result{} nil
1389 @end smallexample 1477 @end smallexample
1390 1478
1391 Here is another example, in which the keys and values are not symbols: 1479 Here is another example, in which the keys and values are not symbols:
1392 1480
1394 (setq needles-per-cluster 1482 (setq needles-per-cluster
1395 '((2 "Austrian Pine" "Red Pine") 1483 '((2 "Austrian Pine" "Red Pine")
1396 (3 "Pitch Pine") 1484 (3 "Pitch Pine")
1397 (5 "White Pine"))) 1485 (5 "White Pine")))
1398 1486
1399 (cdr (assoc 3 needles-per-cluster)) 1487 (cdr (assoc* 3 needles-per-cluster))
1400 @result{} ("Pitch Pine") 1488 @result{} ("Pitch Pine")
1401 (cdr (assoc 2 needles-per-cluster)) 1489 (cdr (assoc* 2 needles-per-cluster))
1402 @result{} ("Austrian Pine" "Red Pine") 1490 @result{} ("Austrian Pine" "Red Pine")
1403 @end smallexample 1491 @end smallexample
1404 @end defun 1492
1405 1493 The @code{:test} keyword argument allows you to specify the test used to
1406 @defun rassoc value alist 1494 decide whether @var{key} is equivalent to a given element of
1495 @var{alist}. The function should return non-@code{nil} if the items
1496 match, @code{nil} if they do not. The @code{:test-not} keyword is
1497 similar, but the meaning of @code{nil} and non-@code{nil} results are
1498 reversed. The @code{:key} keyword allows you to examine a component of
1499 each @sc{car} in @var{alist}, rather than the @sc{car} itself.
1500 @end defun
1501
1502 @defun rassoc* value alist @t{&key :test :test-not :key}
1407 This function returns the first association with value @var{value} in 1503 This function returns the first association with value @var{value} in
1408 @var{alist}. It returns @code{nil} if no association in @var{alist} has 1504 @var{alist}. It returns @code{nil} if no association in @var{alist} has
1409 a @sc{cdr} @code{equal} to @var{value}. 1505 a @sc{cdr} @code{eql} to @var{value}.
1410 1506
1411 @code{rassoc} is like @code{assoc} except that it compares the @sc{cdr} of 1507 @code{rassoc*} is like @code{assoc*} except that it compares the @sc{cdr} of
1412 each @var{alist} association instead of the @sc{car}. You can think of 1508 each @var{alist} association instead of the @sc{car}. You can think of
1413 this as ``reverse @code{assoc}'', finding the key for a given value. 1509 this as ``reverse @code{assoc*}'', finding the key for a given value.
1510
1511 The keywords work similarly to @code{assoc*}.
1414 @end defun 1512 @end defun
1415 1513
1416 @defun assq key alist 1514 @defun assq key alist
1417 This function is like @code{assoc} in that it returns the first 1515 This is equivalent to calling @code{(assoc* key alist :test 'eq)}, and
1418 association for @var{key} in @var{alist}, but it makes the comparison 1516 compiles to the same byte code.
1419 using @code{eq} instead of @code{equal}. @code{assq} returns @code{nil}
1420 if no association in @var{alist} has a @sc{car} @code{eq} to @var{key}.
1421 This function is used more often than @code{assoc}, since @code{eq} is
1422 faster than @code{equal} and most alists use symbols as keys.
1423 @xref{Equality Predicates}.
1424 1517
1425 @smallexample 1518 @smallexample
1426 (setq trees '((pine . cones) (oak . acorns) (maple . seeds))) 1519 (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
1427 @result{} ((pine . cones) (oak . acorns) (maple . seeds)) 1520 @result{} ((pine . cones) (oak . acorns) (maple . seeds))
1428 (assq 'pine trees) 1521 (assq 'pine trees)
1429 @result{} (pine . cones) 1522 @result{} (pine . cones)
1430 @end smallexample 1523 @end smallexample
1431 1524
1432 On the other hand, @code{assq} is not usually useful in alists where the 1525 @code{assq} is not usually useful in alists where the keys may not be
1433 keys may not be symbols: 1526 symbols:
1434 1527
1435 @smallexample 1528 @smallexample
1436 (setq leaves 1529 (setq leaves
1437 '(("simple leaves" . oak) 1530 '(("simple leaves" . oak)
1438 ("compound leaves" . horsechestnut))) 1531 ("compound leaves" . horsechestnut)))
1443 @result{} ("simple leaves" . oak) 1536 @result{} ("simple leaves" . oak)
1444 @end smallexample 1537 @end smallexample
1445 @end defun 1538 @end defun
1446 1539
1447 @defun rassq value alist 1540 @defun rassq value alist
1448 This function returns the first association with value @var{value} in 1541 This is equivalent to calling @code{(rassoc* value alist :test 'eq)}, and
1449 @var{alist}. It returns @code{nil} if no association in @var{alist} has 1542 compiles to the same byte code. For example:
1450 a @sc{cdr} @code{eq} to @var{value}.
1451
1452 @code{rassq} is like @code{assq} except that it compares the @sc{cdr} of
1453 each @var{alist} association instead of the @sc{car}. You can think of
1454 this as ``reverse @code{assq}'', finding the key for a given value.
1455
1456 For example:
1457 1543
1458 @smallexample 1544 @smallexample
1459 (setq trees '((pine . cones) (oak . acorns) (maple . seeds))) 1545 (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
1460 1546
1461 (rassq 'acorns trees) 1547 (rassq 'acorns trees)