emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node69.html

85 lines
5.2 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>6. Predicates</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Predicates">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html2368 HREF="node70.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2366 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2360 HREF="node68.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2370 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2371 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2369 HREF="node70.html"> Logical Values</A>
<B>Up:</B> <A NAME=tex2html2367 HREF="clm.html">Common Lisp the Language</A>
<B> Previous:</B> <A NAME=tex2html2361 HREF="node68.html"> Control of Time </A>
<HR> <P>
<H1><A NAME=SECTION001000000000000000000>6. Predicates</A></H1>
<P>
<A NAME=PREDS>A</A>
<i>predicate</i> is a function that tests for some condition involving
its arguments and returns <tt>nil</tt> if the condition is false, or some
non-<tt>nil</tt> value if the condition is true. One may think of a predicate as
producing a Boolean value, where <tt>nil</tt> stands for <i>false</i> and anything
else stands for <i>true</i>. Conditional control structures such as
<tt>cond</tt>,
<tt>if</tt>, <tt>when</tt>, and <tt>unless</tt> test such Boolean values.
We say that a predicate <i>is true</i> when it returns a non-<tt>nil</tt> value,
and <i>is false</i> when it returns <tt>nil</tt>; that is, it is true or false
according to whether the condition being tested is true or false.
<P>
By convention, the names of predicates usually end in the letter
<tt>p</tt> (which stands for ``predicate'').
Common Lisp uses a uniform convention in hyphenating names of predicates.
If the name of the predicate is formed by adding a <tt>p</tt> to
an existing name, such as the name of a data type,
a hyphen is placed before the final <tt>p</tt> if and only if there is
a hyphen in the existing name. For example, <tt>number</tt> begets <tt>numberp</tt>
but <tt>standard-char</tt> begets <tt>standard-char-p</tt>.
On the other hand, if the name of a predicate is formed by adding
a prefixing qualifier to the front of an existing predicate name,
the two names are joined with a hyphen and the presence or absence
of a hyphen before the final <tt>p</tt> is not changed. For example,
the predicate <tt>string-lessp</tt> has no hyphen before the <tt>p</tt>
because it is the string version of <tt>lessp</tt> (a MacLisp function
that has been renamed <tt>&lt;</tt> in Common Lisp). The name <tt>string-less-p</tt>
would incorrectly imply that it is a predicate that tests for a kind
of object called a <tt>string-less</tt>, and the name <tt>stringlessp</tt>
would connote a predicate that tests whether something has no strings
(is ``stringless'')!
<P>
The control structures that test Boolean values only test for
whether or not the value is <tt>nil</tt>, which is considered to be false. Any
other value is considered to be true. Often a predicate will return <tt>nil</tt> if
it ``fails'' and some <i>useful</i> value if it ``succeeds'';
such a function can be used not only as a test but
also for the useful value provided in case of success. An example
is <tt>member</tt>.
<P>
If no better non-<tt>nil</tt> value is available for the purpose of indicating
success, by convention the symbol <tt>t</tt> is used as the ``standard''
true value.
<P>
<HR>
<UL>
<LI> <A NAME=tex2html2372 HREF="node70.html#SECTION001010000000000000000"> Logical Values</A>
<LI> <A NAME=tex2html2373 HREF="node71.html#SECTION001020000000000000000"> Data Type Predicates</A>
<UL>
<LI> <A NAME=tex2html2374 HREF="node72.html#SECTION001021000000000000000"> General Type Predicates</A>
<LI> <A NAME=tex2html2375 HREF="node73.html#SECTION001022000000000000000"> Specific Data Type Predicates</A>
</UL>
<LI> <A NAME=tex2html2376 HREF="node74.html#SECTION001030000000000000000"> Equality Predicates</A>
<LI> <A NAME=tex2html2377 HREF="node75.html#SECTION001040000000000000000"> Logical Operators</A>
</UL>
<BR> <HR><A NAME=tex2html2368 HREF="node70.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2366 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2360 HREF="node68.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2370 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2371 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2369 HREF="node70.html"> Logical Values</A>
<B>Up:</B> <A NAME=tex2html2367 HREF="clm.html">Common Lisp the Language</A>
<B> Previous:</B> <A NAME=tex2html2361 HREF="node68.html"> Control of Time </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>