comparison lispref/lists.texi @ 21007:66d807bdc5b4

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Sat, 28 Feb 1998 01:53:53 +0000
parents 909eb45b146d
children 90da2489c498
comparison
equal deleted inserted replaced
21006:00022857f529 21007:66d807bdc5b4
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual. 2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions. 4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/lists 5 @setfilename ../info/lists
6 @node Lists, Sequences Arrays Vectors, Strings and Characters, Top 6 @node Lists, Sequences Arrays Vectors, Strings and Characters, Top
7 @chapter Lists 7 @chapter Lists
8 @cindex list 8 @cindex list
325 @end example 325 @end example
326 @end defun 326 @end defun
327 327
328 @defun nthcdr n list 328 @defun nthcdr n list
329 This function returns the @var{n}th @sc{cdr} of @var{list}. In other 329 This function returns the @var{n}th @sc{cdr} of @var{list}. In other
330 words, it removes the first @var{n} links of @var{list} and returns 330 words, it skips past the first @var{n} links of @var{list} and returns
331 what follows. 331 what follows.
332 332
333 If @var{n} is zero or negative, @code{nthcdr} returns all of 333 If @var{n} is zero or negative, @code{nthcdr} returns all of
334 @var{list}. If the length of @var{list} is @var{n} or less, 334 @var{list}. If the length of @var{list} is @var{n} or less,
335 @code{nthcdr} returns @code{nil}. 335 @code{nthcdr} returns @code{nil}.
346 @group 346 @group
347 (nthcdr -3 '(1 2 3 4)) 347 (nthcdr -3 '(1 2 3 4))
348 @result{} (1 2 3 4) 348 @result{} (1 2 3 4)
349 @end group 349 @end group
350 @end example 350 @end example
351 @end defun
352
353 @tindex safe-length
354 @defun safe-length sequence
355 This function returns the length of @var{list}, with no risk
356 of either an error or an infinite loop.
357
358 If @var{list} is not really a list, @code{safe-length} returns 0. If
359 @var{list} is circular, it returns a finite value which is at least the
360 number of distinct elements.
361 @end defun
362
363 @tindex caar
364 @defun caar list
365 This is the same as @code{(car (car @var{list}))}.
366 @end defun
367
368 @tindex cadr
369 @defun cadr list
370 This is the same as @code{(car (cdr @var{list}))}
371 or @code{(nth 1 @var{list})}.
372 @end defun
373
374 @tindex cdar
375 @defun cdar list
376 This is the same as @code{(cdr (car @var{list}))}.
377 @end defun
378
379 @tindex cddr
380 @defun cddr list
381 This is the same as @code{(cdr (cdr @var{list}))}
382 or @code{(nthcdr 2 @var{list})}.
351 @end defun 383 @end defun
352 384
353 @node Building Lists 385 @node Building Lists
354 @comment node-name, next, previous, up 386 @comment node-name, next, previous, up
355 @section Building Cons Cells and Lists 387 @section Building Cons Cells and Lists
437 469
438 @defun append &rest sequences 470 @defun append &rest sequences
439 @cindex copying lists 471 @cindex copying lists
440 This function returns a list containing all the elements of 472 This function returns a list containing all the elements of
441 @var{sequences}. The @var{sequences} may be lists, vectors, or strings, 473 @var{sequences}. The @var{sequences} may be lists, vectors, or strings,
442 but the last one should be a list. All arguments except the last one 474 but the last one should usually be a list. All arguments except the
443 are copied, so none of them are altered. 475 last one are copied, so none of the arguments is altered.
444 476
445 More generally, the final argument to @code{append} may be any Lisp 477 More generally, the final argument to @code{append} may be any Lisp
446 object. The final argument is not copied or converted; it becomes the 478 object. The final argument is not copied or converted; it becomes the
447 @sc{cdr} of the last cons cell in the new list. If the final argument 479 @sc{cdr} of the last cons cell in the new list. If the final argument
448 is itself a list, then its elements become in effect elements of the 480 is itself a list, then its elements become in effect elements of the
1000 @result{} (1 2 3 4 5 6) 1032 @result{} (1 2 3 4 5 6)
1001 @end group 1033 @end group
1002 @end example 1034 @end example
1003 1035
1004 @noindent 1036 @noindent
1005 Note that the list in @code{nums} no longer contains 0; this is the same 1037 @strong{Warning}: Note that the list in @code{nums} no longer contains
1006 cons cell that it was before, but it is no longer the first one in the 1038 0; this is the same cons cell that it was before, but it is no longer
1007 list. Don't assume a variable that formerly held the argument now holds 1039 the first one in the list. Don't assume a variable that formerly held
1008 the entire sorted list! Instead, save the result of @code{sort} and use 1040 the argument now holds the entire sorted list! Instead, save the result
1009 that. Most often we store the result back into the variable that held 1041 of @code{sort} and use that. Most often we store the result back into
1010 the original list: 1042 the variable that held the original list:
1011 1043
1012 @example 1044 @example
1013 (setq nums (sort nums '<)) 1045 (setq nums (sort nums '<))
1014 @end example 1046 @end example
1015 1047
1123 @result{} (a c (4)) 1155 @result{} (a c (4))
1124 @end group 1156 @end group
1125 @end example 1157 @end example
1126 1158
1127 The following two functions are like @code{memq} and @code{delq} but use 1159 The following two functions are like @code{memq} and @code{delq} but use
1128 @code{equal} rather than @code{eq} to compare elements. They are new in 1160 @code{equal} rather than @code{eq} to compare elements.
1129 Emacs 19.
1130 1161
1131 @defun member object list 1162 @defun member object list
1132 The function @code{member} tests to see whether @var{object} is a member 1163 The function @code{member} tests to see whether @var{object} is a member
1133 of @var{list}, comparing members with @var{object} using @code{equal}. 1164 of @var{list}, comparing members with @var{object} using @code{equal}.
1134 If @var{object} is a member, @code{member} returns a list starting with 1165 If @var{object} is a member, @code{member} returns a list starting with
1220 '((rose red) (lily white) (buttercup yellow)) 1251 '((rose red) (lily white) (buttercup yellow))
1221 @end example 1252 @end example
1222 1253
1223 @noindent 1254 @noindent
1224 Here we regard @code{red} as the value associated with @code{rose}. One 1255 Here we regard @code{red} as the value associated with @code{rose}. One
1225 advantage of this method is that you can store other related 1256 advantage of this kind of alist is that you can store other related
1226 information---even a list of other items---in the @sc{cdr} of the 1257 information---even a list of other items---in the @sc{cdr} of the
1227 @sc{cdr}. One disadvantage is that you cannot use @code{rassq} (see 1258 @sc{cdr}. One disadvantage is that you cannot use @code{rassq} (see
1228 below) to find the element containing a given value. When neither of 1259 below) to find the element containing a given value. When neither of
1229 these considerations is important, the choice is a matter of taste, as 1260 these considerations is important, the choice is a matter of taste, as
1230 long as you are consistent about it for any given alist. 1261 long as you are consistent about it for any given alist.