1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/f_list_l.htm
2024-04-01 10:24:07 +02:00

86 lines
4.8 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Function LIST-LENGTH</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="f_list_.htm">
<LINK REL=UP HREF="c_conses.htm">
<LINK REL=NEXT HREF="f_listp.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_list_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_conses.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_listp.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="list-length"><I>Function</I> <B>LIST-LENGTH</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>list-length</B> <I>list</I> =&gt; <I>length</I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>list</I>---a <A REL=DEFINITION HREF="26_glo_p.htm#proper_list"><I>proper list</I></A> or a <A REL=DEFINITION HREF="26_glo_c.htm#circular_list"><I>circular list</I></A>. <P>
<I>length</I>---a non-negative <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>, or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
<P><B>Description:</B><P>
<P>
Returns the <A REL=DEFINITION HREF="26_glo_l.htm#length"><I>length</I></A> of <I>list</I> if <I>list</I> is a <A REL=DEFINITION HREF="26_glo_p.htm#proper_list"><I>proper list</I></A>. Returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> if <I>list</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#circular_list"><I>circular list</I></A>. <P>
<P><B>Examples:</B><P>
<P>
<PRE>
(list-length '(a b c d)) =&gt; 4
(list-length '(a (b c) d)) =&gt; 3
(list-length '()) =&gt; 0
(list-length nil) =&gt; 0
(defun circular-list (&amp;rest elements)
(let ((cycle (copy-list elements)))
(nconc cycle cycle)))
(list-length (circular-list 'a 'b)) =&gt; NIL
(list-length (circular-list 'a)) =&gt; NIL
(list-length (circular-list)) =&gt; 0
</PRE>
</TT> <P>
<P><B>Side Effects:</B> None.
<P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B><P>
<P>
Should signal an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> if <I>list</I> is not a <A REL=DEFINITION HREF="26_glo_p.htm#proper_list"><I>proper list</I></A> or a <A REL=DEFINITION HREF="26_glo_c.htm#circular_list"><I>circular list</I></A>. <P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="f_length.htm#length"><B>length</B></A> <P>
<P><B>Notes:</B><P>
<P>
<A REL=DEFINITION HREF="#list-length"><B>list-length</B></A> could be implemented as follows: <P>
<PRE>
(defun list-length (x)
(do ((n 0 (+ n 2)) ;Counter.
(fast x (cddr fast)) ;Fast pointer: leaps by 2.
(slow x (cdr slow))) ;Slow pointer: leaps by 1.
(nil)
;; If fast pointer hits the end, return the count.
(when (endp fast) (return n))
(when (endp (cdr fast)) (return (+ n 1)))
;; If fast pointer eventually equals slow pointer,
;; then we must be stuck in a circular list.
;; (A deeper property is the converse: if we are
;; stuck in a circular list, then eventually the
;; fast pointer will equal the slow pointer.
;; That fact justifies this implementation.)
(when (and (eq fast slow) (&gt; n 0)) (return nil))))
</PRE>
</TT> <P>
<HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>