emacs.d/clones/lisp/colinallen.dnsalias.org/lp/node89.html

94 lines
2.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> defun (MACRO)</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" defun (MACRO)">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node90.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node72.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node88.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node90.html"> do (SPECIAL FORM)</A>
<B>Up:</B>
<A HREF="node72.html"> Appendix: Selected Lisp </A>
<B> Previous:</B>
<A HREF="node88.html"> cons (FUNCTION)</A>
<BR> <HR> <P>
<H1> defun (MACRO)</H1>
<P>
<b> Format:</b>
<tt> (defun &lt;name&gt; (&lt;par1&gt; &lt;par2&gt; ... &lt;parN&gt; )</tt>
<tt> &lt;body&gt; )</tt>
<P>
<b> Required arguments:</b>
2
<P>
<tt> &lt;name&gt; </tt>: a symbol which is appropriate as the name of function;
<tt> (&lt;par1&gt; &lt;par2&gt; ...)</tt>: a list of zero or more symbols which
are appropriate as parameter names
<P>
<b> Optional arguments:</b>
1
<P>
<tt> &lt;body&gt; </tt>: a sequence of zero or more Lisp expressions
<P>
The arguments to defun are not evaluated---they are used to establish a procedure definition. The first argument is a
symbol which specifies the name of the function. This name can
later be used to execute the <tt> &lt;body&gt; </tt> of the function. The
parameter-list follows the name. This list specifies the number and order of
arguments in a function call. Each <tt> &lt;par&gt; </tt> is symbol which may appear
in the <tt> &lt;body&gt; </tt>. The value of each parameter, <tt> &lt;par&gt; </tt>, is
determined by the value of corresponding argument in the function call.
defun returns the name of the function.
<P>
<b> Examples:</b>
<P>
<BLOCKQUOTE>
<PRE>&gt; (defun square (x)
(* x x))
SQUARE
&gt; (square 4)
16
&gt; (square (+ 7 -2))
25
&gt; (defun equal-length (lst1 lst2)
(cond ((= (length lst1) (length lst2)) t)
(t nil)))
EQUAL-LENGTH
&gt; (equal-length '(a b c) '((d e) f g))
T
&gt; (equal-length '() (rest '(a b)))
NIL
&gt; (defun side-effect (x y)
(setq x (* x x x))
(+ x y))
SIDE-EFFECT
&gt; (side-effect 2 3)
11
&gt; (side-effect -4 14)
-50
</PRE>
</BLOCKQUOTE>
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>