92 lines
7.2 KiB
HTML
92 lines
7.2 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: Section 3.4.1.6</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="03_dae.htm">
|
|
<LINK REL=UP HREF="03_da.htm">
|
|
<LINK REL=NEXT HREF="03_db.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="03_dae.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="03_da.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="03_db.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<H2>
|
|
3.4.1.6 Examples of Ordinary Lambda Lists</H2> <P>
|
|
Here are some examples involving <A REL=DEFINITION HREF="26_glo_o.htm#optional_parameter"><I>optional parameters</I></A> and <A REL=DEFINITION HREF="26_glo_r.htm#rest_parameter"><I>rest parameters</I></A>: <P>
|
|
<PRE>
|
|
((lambda (a b) (+ a (* b 3))) 4 5) => 19
|
|
((lambda (a &optional (b 2)) (+ a (* b 3))) 4 5) => 19
|
|
((lambda (a &optional (b 2)) (+ a (* b 3))) 4) => 10
|
|
((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)))
|
|
=> (2 NIL 3 NIL NIL)
|
|
((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6)
|
|
=> (6 T 3 NIL NIL)
|
|
((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6 3)
|
|
=> (6 T 3 T NIL)
|
|
((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6 3 8)
|
|
=> (6 T 3 T (8))
|
|
((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x))
|
|
6 3 8 9 10 11)
|
|
=> (6 t 3 t (8 9 10 11))
|
|
</PRE>
|
|
</TT> <P>
|
|
Here are some examples involving <A REL=DEFINITION HREF="26_glo_k.htm#keyword_parameter"><I>keyword parameters</I></A>: <P>
|
|
<PRE>
|
|
((lambda (a b &key c d) (list a b c d)) 1 2) => (1 2 NIL NIL)
|
|
((lambda (a b &key c d) (list a b c d)) 1 2 :c 6) => (1 2 6 NIL)
|
|
((lambda (a b &key c d) (list a b c d)) 1 2 :d 8) => (1 2 NIL 8)
|
|
((lambda (a b &key c d) (list a b c d)) 1 2 :c 6 :d 8) => (1 2 6 8)
|
|
((lambda (a b &key c d) (list a b c d)) 1 2 :d 8 :c 6) => (1 2 6 8)
|
|
((lambda (a b &key c d) (list a b c d)) :a 1 :d 8 :c 6) => (:a 1 6 8)
|
|
((lambda (a b &key c d) (list a b c d)) :a :b :c :d) => (:a :b :d NIL)
|
|
((lambda (a b &key ((:sea c)) d) (list a b c d)) 1 2 :sea 6) => (1 2 6 NIL)
|
|
((lambda (a b &key ((c c)) d) (list a b c d)) 1 2 'c 6) => (1 2 6 NIL)
|
|
</PRE>
|
|
</TT> <P>
|
|
Here are some examples involving <A REL=DEFINITION HREF="26_glo_o.htm#optional_parameter"><I>optional parameters</I></A>, <A REL=DEFINITION HREF="26_glo_r.htm#rest_parameter"><I>rest parameters</I></A>, and <A REL=DEFINITION HREF="26_glo_k.htm#keyword_parameter"><I>keyword parameters</I></A> together: <P>
|
|
<PRE>
|
|
((lambda (a &optional (b 3) &rest x &key c (d a))
|
|
(list a b c d x)) 1)
|
|
=> (1 3 NIL 1 ())
|
|
((lambda (a &optional (b 3) &rest x &key c (d a))
|
|
(list a b c d x)) 1 2)
|
|
=> (1 2 NIL 1 ())
|
|
((lambda (a &optional (b 3) &rest x &key c (d a))
|
|
(list a b c d x)) :c 7)
|
|
=> (:c 7 NIL :c ())
|
|
((lambda (a &optional (b 3) &rest x &key c (d a))
|
|
(list a b c d x)) 1 6 :c 7)
|
|
=> (1 6 7 1 (:c 7))
|
|
((lambda (a &optional (b 3) &rest x &key c (d a))
|
|
(list a b c d x)) 1 6 :d 8)
|
|
=> (1 6 NIL 8 (:d 8))
|
|
((lambda (a &optional (b 3) &rest x &key c (d a))
|
|
(list a b c d x)) 1 6 :d 8 :c 9 :d 10)
|
|
=> (1 6 9 8 (:d 8 :c 9 :d 10))
|
|
</PRE>
|
|
</TT> <P>
|
|
As an example of the use of <TT>&allow-other-keys</TT> and <TT>:allow-other-keys</TT>, consider a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> that takes two named arguments of its own and also accepts additional named arguments to be passed to <A REL=DEFINITION HREF="f_mk_ar.htm#make-array"><B>make-array</B></A>: <P>
|
|
<PRE>
|
|
(defun array-of-strings (str dims &rest named-pairs
|
|
&key (start 0) end &allow-other-keys)
|
|
(apply #'make-array dims
|
|
:initial-element (subseq str start end)
|
|
:allow-other-keys t
|
|
named-pairs))
|
|
</PRE>
|
|
</TT> <P>
|
|
This <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> takes a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A> and dimensioning information and returns an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> of the specified dimensions, each of whose elements is the specified <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A>. However, <TT>:start</TT> and <TT>:end</TT> named arguments may be used to specify that a substring of the given <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A> should be used. In addition, the presence of <TT>&allow-other-keys</TT> in the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A> indicates that the caller may supply additional named arguments; the <A REL=DEFINITION HREF="26_glo_r.htm#rest_parameter"><I>rest parameter</I></A> provides access to them. These additional named arguments are passed to <A REL=DEFINITION HREF="f_mk_ar.htm#make-array"><B>make-array</B></A>. The <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> <A REL=DEFINITION HREF="f_mk_ar.htm#make-array"><B>make-array</B></A> normally does not allow the named arguments <TT>:start</TT> and <TT>:end</TT> to be used, and an error should be signaled if such named arguments are supplied to <A REL=DEFINITION HREF="f_mk_ar.htm#make-array"><B>make-array</B></A>. However, the presence in the call to <A REL=DEFINITION HREF="f_mk_ar.htm#make-array"><B>make-array</B></A> of the named argument <TT>:allow-other-keys</TT> with a <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A> value causes any extraneous named arguments, including <TT>:start</TT> and <TT>:end</TT>, to be acceptable and ignored. <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>
|