1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Some Useful List Functions</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-23.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-25.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc24"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s24">24. Some Useful List Functions</A></H2>
<P>These functions all manipulate lists.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (append '(1 2 3) '(4 5 6)) ;concatenate lists
(1 2 3 4 5 6)
> (reverse '(1 2 3)) ;reverse the elements of a list
(3 2 1)
> (member 'a '(b d a c)) ;set membership -- returns the first tail
(A C) ;whose car is the desired element
> (find 'a '(b d a c)) ;another way to do set membership
A
> (find '(a b) '((a d) (a d e) (a b d e) ()) :test #'subsetp)
(A B D E) ;find is more flexible though
> (subsetp '(a b) '(a d e)) ;set containment
NIL
> (intersection '(a b c) '(b)) ;set intersection
(B)
> (union '(a) '(b)) ;set union
(A B)
> (set-difference '(a b) '(a)) ;set difference
(B)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P><CODE>Subsetp</CODE>, <CODE>intersection</CODE>, <CODE>union</CODE>, and <CODE>set-difference</CODE>
all assume that each
argument contains no duplicate elements -- <CODE>(subsetp '(a a) '(a b b))</CODE> is
allowed to fail, for example.</P>
<P><CODE>Find</CODE>, <CODE>subsetp</CODE>, <CODE>intersection</CODE>, <CODE>union</CODE>, and
<CODE>set-difference</CODE> can all take a
<CODE>:test</CODE> keyword argument; by default, they all use <CODE>eql</CODE>.</P>
<HR>
<A HREF="LISP-tutorial-23.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-25.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc24"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>
|