File: lambda-keyword-key.htm

package info (click to toggle)
nyquist 3.20%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 58,008 kB
  • sloc: ansic: 74,743; lisp: 17,929; java: 10,723; cpp: 6,690; sh: 171; xml: 58; makefile: 40; python: 15
file content (132 lines) | stat: -rw-r--r-- 5,432 bytes parent folder | download | duplicates (7)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<html><head><title>XLISP &amp;key</title>

<link rel="stylesheet" type="text/css" href="reference.css">

</head>

<body>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>

<hr>

<h1>&amp;key</h1>

<hr>

<p><table cellpadding="0" cellspacing="0" style="margin-left:10px"><tbody>
<tr valign="top">
  <td><nobr>Type:</nobr></td>
  <td><nobr>&nbsp;&nbsp;-&nbsp;&nbsp;</nobr></td>
  <td width="100%"><nobr>keyword</nobr></td>
</tr>
<tr valign="top">
  <td><nobr>Source:</nobr></td>
  <td><nobr>&nbsp;&nbsp;-&nbsp;&nbsp;</nobr></td>
  <td width="100%"><nobr>xleval.c</nobr></td>
</tr>
</tbody></table></p>

<h2>Syntax</h2>

<dl>
<dt>&amp;key <i>key-arg</i> ...<br>
&amp;key (<i>key-arg</i> [<i>key-value</i> [<i>supplied-p-var</i>]]) ...<br>
&amp;key ((<i>key-symbol key-arg</i>) [<i>key-value</i> [<i>supplied-p-var</i>]]) ...</dt>
<dd><i>key-arg</i> - keyword argument<br>
<i>key-symbol</i> - keyword argument symbol<br>
<i>key-value</i> - keyword argument initialization<br>
<i>supplied-p-var</i> - argument existence variable</dd>
</dl>

<h2>Description</h2>

<p>In XLISP, there are several times that you define a formal argument list
for a body of code [like
<nobr><a href="defun.htm">defun</a> ,</nobr>
<nobr><a href="defmacro.htm">defmacro</a> ,</nobr>
<a href="keyword-answer.htm">:answer</a> and
<a href="lambda.htm">lambda</a>].
All of the formal arguments that are defined are required to appear in the
invocation of the defined function or operation. If there are any
<a href="lambda-keyword-optional.htm">&amp;optional</a> arguments defined, they will
be filled in order.</p>

<p>There are other optional arguments called 'keyword' arguments. These
arguments are not position dependent but can be specified in any order by a
preceding keyword [a symbol with a leading colon ':']. If there is no
'key-symbol' specified in the argument list, the keyword will be constructed
from the 'key-arg' name by adding a leading colon ':'. For example a
'key-arg' of 'furter' will generate a keyword symbol of ':furter'.</p>

<p>Like the <a href="lambda-keyword-optional.htm">&amp;optional</a> arguments, there
can be initialization values provided via the 'key-value' argument. If there
is no 'key-value' argument and no value is provided by the function call,
the 'key-arg' value will be <a href="nil.htm">NIL</a>.</p>

<p>The 'supplied-p-var', if it is specified, will contain a
<a href="t.htm">&nbsp;T&nbsp;</a> if the 'key-arg' value was
supplied by the function call and a <a href="nil.htm">NIL</a> if
it was not supplied by the function call. This 'supplied-p-var' allows the
programmer to test for an argument's existence. At the end of the function
or operation execution, these local symbols and their values are are
removed.</p>

<h2>Examples</h2>

<pre class="example">
(defun foo (a &amp;key b c)
  (print a) (print b) (print c))

(foo)                           <font color="#008844">; error: too few arguments</font>
(foo 1)                         <font color="#008844">; prints 1 NIL NIL</font>
(foo 1 2)                       <font color="#008844">; prints 1 NIL NIL</font>
(foo 1 :b 2 :c 3)               <font color="#008844">; prints 1 2 3</font>
(foo 1 :c 3 :b 2)               <font color="#008844">; prints 1 2 3</font>
(foo 1 :b 3 :b 2)               <font color="#008844">; prints 1 3 NIL</font>

(defun fee (a &amp;key (b 9 b-passed))
  (print a) (print b)
  (if b-passed (print "b was passed")
               (print "b not passed")))

(fee)                           <font color="#008844">; error: too few arguments</font>
(fee 1)                         <font color="#008844">; prints 1 9 "b not passed"</font>
(fee 1 2)                       <font color="#008844">; prints 1 9 "b not passed"</font>
(fee 1 :b 2)                    <font color="#008844">; prints 1 2 "b was passed"</font>

(defun fi (a &amp;key ((:mykey b) 9 b-passed))
  (print a) (print b)
  (if b-passed (print "b was passed")
               (print "b not passed")))

(fi)                            <font color="#008844">; error: too few arguments</font>
(fi 1)                          <font color="#008844">; prints 1 9 "b not passed"</font>
(fi 1 2)                        <font color="#008844">; prints 1 9 "b not passed"</font>
(fi 1 :b 2)                     <font color="#008844">; prints 1 9 "b not passed"</font>
(fi 1 :mykey 2)                 <font color="#008844">; prints 1 2 "b was passed"</font>
</pre>

<p><b>Note:</b> There is a '&amp;allow-other-keys' keyword in XLISP and
Common Lisp. In the case of XLISP, this keyword is extraneous since the
default for keyword arguments is to allow other keys (without errors).</p>

<p>See the
<a href="../manual/xlisp-man-009.htm#9-1-4">&amp;key</a>
keyword in the <nobr>XLISP 2.0</nobr> manual.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</nobr></a></p>

<hr>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>

</body></html>