Extended-formals.xhtml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Extended Formal Arguments List</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Generic-procedures.xhtml" title="Generic (dynamically overloaded) procedures"/>
  10. <link rel="next" href="Partial-application.xhtml" title="Partial application"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Extended Formal Arguments List" epub:type="subchapter" id="Extended-formals">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Extended Formal Arguments List</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The formal arguments list of a lambda expression has some
  23. extensions over standard Scheme:
  24. Kawa borrows the extended formal argument list of DSSSL,
  25. and Kawa allows you to declare the type of the parameter.
  26. More generally, you can use <a class="link" href="Variables-and-Patterns.xhtml" title="Variables and Patterns">patterns</a>.
  27. </p>
  28. <div class="literallayout">
  29. <p><a id="idm139667877350272" class="indexterm"/><span id="meta-lambda-expression"/><em class="replaceable"><code>lambda-expression</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(lambda</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-formals"><em class="replaceable"><code>formals</code></em></a> <a class="link" href="Defining-new-classes.xhtml#meta-option-pair"><em class="replaceable"><code>option-pair</code></em></a><sup>*</sup> <a class="link" href="Extended-formals.xhtml#meta-opt-return-type"><em class="replaceable"><code>opt-return-type</code></em></a> <a class="link" href="Bodies.xhtml#meta-body"><em class="replaceable"><code>body</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  30. <a id="idm139667877343296" class="indexterm"/><span id="meta-return-type"/><em class="replaceable"><code>return-type</code></em> <code class="literal">::=</code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a><br/>
  31. <a id="idm139667877340496" class="indexterm"/><span id="meta-opt-return-type"/><em class="replaceable"><code>opt-return-type</code></em> <code class="literal">::=</code> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  32. </p>
  33. </div>
  34. <p>where
  35. </p>
  36. <div class="literallayout">
  37. <p><a id="idm139667877336496" class="indexterm"/><span id="meta-formals"/><em class="replaceable"><code>formals</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(</strong></span></code><a class="link" href="Extended-formals.xhtml#meta-formal-arguments"><em class="replaceable"><code>formal-arguments</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code> | <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a><br/>
  38. </p>
  39. </div>
  40. <p>You can of course also use the extended format in a <a class="link" href="Definitions.xhtml" title="Definitions"><code class="literal">define</code></a>:
  41. </p>
  42. <pre class="screen"><span class="bold"><strong>(define (</strong></span><em class="replaceable"><code>name</code></em> <em class="replaceable"><code>formal-arguments</code></em><span class="bold"><strong>)</strong></span> [<em class="replaceable"><code>rtype</code></em>] <em class="replaceable"><code>body</code></em><span class="bold"><strong>)</strong></span>
  43. </pre>
  44. <div class="literallayout">
  45. <p><a id="idm139667877326896" class="indexterm"/><span id="meta-formal-arguments"/><em class="replaceable"><code>formal-arguments</code></em> <code class="literal">::=</code><br/>
  46.     <a class="link" href="Extended-formals.xhtml#meta-req-opt-args"><em class="replaceable"><code>req-opt-args</code></em></a> (<a class="link" href="Extended-formals.xhtml#meta-rest-key-args"><em class="replaceable"><code>rest-key-args</code></em></a> | <code class="literal"><span class="bold"><strong>.</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a>)<br/>
  47. </p>
  48. </div>
  49. <div class="literallayout">
  50. <p><a id="idm139667877321552" class="indexterm"/><span id="meta-req-opt-args"/><em class="replaceable"><code>req-opt-args</code></em> <code class="literal">::=</code> (<a class="link" href="Extended-formals.xhtml#meta-required-arg"><em class="replaceable"><code>required-arg</code></em></a>|<a class="link" href="Variables-and-Patterns.xhtml#meta-guard"><em class="replaceable"><code>guard</code></em></a>)<sup>*</sup> [<code class="literal"><span class="bold"><strong>#!optional</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-optional-arg"><em class="replaceable"><code>optional-arg</code></em></a> ...]<br/>
  51. <a id="idm139667877315888" class="indexterm"/><span id="meta-rest-key-args"/><em class="replaceable"><code>rest-key-args</code></em> <code class="literal">::=</code> [<code class="literal"><span class="bold"><strong>#!rest</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a>] [<code class="literal"><span class="bold"><strong>#!key</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-key-arg"><em class="replaceable"><code>key-arg</code></em></a> ...]<br/>
  52.   | [<code class="literal"><span class="bold"><strong>#!key</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-key-arg"><em class="replaceable"><code>key-arg</code></em></a> ...] [<code class="literal"><span class="bold"><strong>#!rest</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a>]<br/>
  53. <a id="idm139667877307312" class="indexterm"/><span id="meta-required-arg"/><em class="replaceable"><code>required-arg</code></em> <code class="literal">::=</code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  54.   | <code class="literal"><span class="bold"><strong>(</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> <code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  55. <a id="idm139667877299024" class="indexterm"/><span id="meta-optional-arg"/><em class="replaceable"><code>optional-arg</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  56.   | <code class="literal"><span class="bold"><strong>(</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>] [<em class="replaceable"><code>initializer</code></em> [<a class="link" href="Extended-formals.xhtml#meta-supplied-var"><em class="replaceable"><code>supplied-var</code></em></a>]]<code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  57. <a id="idm139667877289776" class="indexterm"/><span id="meta-supplied-var"/><em class="replaceable"><code>supplied-var</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em><br/>
  58. <a id="idm139667877287376" class="indexterm"/><span id="meta-key-arg"/><em class="replaceable"><code>key-arg</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  59.     | <code class="literal"><span class="bold"><strong>(</strong></span></code> <em class="replaceable"><code>variable</code></em> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>] [<em class="replaceable"><code>initializer</code></em> [<a class="link" href="Extended-formals.xhtml#meta-supplied-var"><em class="replaceable"><code>supplied-var</code></em></a>]] <code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  60. <a id="idm139667877278528" class="indexterm"/><span id="meta-rest-arg"/><em class="replaceable"><code>rest-arg</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em><br/>
  61. </p>
  62. </div>
  63. <p>When the procedure is applied to a list of actual arguments, the formal and
  64. actual arguments are processed from left to right as follows:
  65. </p>
  66. <div class="itemizedlist" epub:type="list">
  67. <ul class="itemizedlist" style="list-style-type: disc; ">
  68. <li class="listitem" epub:type="list-item">
  69. <p>The <em class="replaceable"><code>required-arg</code></em>s are matched against actual (pre-keyword) arguments
  70. in order, starting with the first actual argument.
  71. A <em class="replaceable"><code>guard</code></em> is evaluated when it appears:
  72. If it evaluates to false, then matching fails.
  73. It shall be an error if there are fewer pre-keyword
  74. arguments then there are <em class="replaceable"><code>req-arg</code></em>s.
  75. </p>
  76. </li>
  77. <li class="listitem" epub:type="list-item">
  78. <p>Next the <em class="replaceable"><code>optional-arg</code></em>s are bound to remaining pre-keyword arguments.
  79. If there are fewer remaining pre-keyword arguments than there are
  80. <em class="replaceable"><code>optional-arg</code></em>s, then the remaining <em class="replaceable"><code>variable</code></em>s are bound
  81. to the corresponding <em class="replaceable"><code>initializer</code></em>.
  82. If no <em class="replaceable"><code>initializer</code></em> was specified, it defaults to <code class="literal">#f</code>.
  83. (TODO: If a <em class="replaceable"><code>type</code></em> is specified the default for <em class="replaceable"><code>initializer</code></em>
  84. is the default value of the <em class="replaceable"><code>type</code></em>.)
  85. The <em class="replaceable"><code>initializer</code></em> is evaluated in an
  86. environment in which all the previous formal parameters have been bound.
  87. If a <em class="replaceable"><code>supplied-var</code></em> is specified, it has type boolean,
  88. and is set to true if there was an actual corresponding argument,
  89. and false if the initializer was evaluated.
  90. </p>
  91. </li>
  92. <li class="listitem" epub:type="list-item">
  93. <p>If there is a <em class="replaceable"><code>rest-arg</code></em>, it is bound to a list of all the
  94. remaining actual arguments. These remaining actual arguments are also
  95. eligible to be bound to keyword arguments. If there is no
  96. <em class="replaceable"><code>rest-arg</code></em> and there are no <em class="replaceable"><code>key-arg</code></em>s, then it shall
  97. be an error if there are any remaining actual arguments.
  98. </p>
  99. </li>
  100. <li class="listitem" epub:type="list-item">
  101. <p>If <code class="literal">#!key</code> was specified, then there shall be an even number of
  102. remaining actual arguments. These are interpreted as a series of pairs,
  103. where the first member of each pair is a keyword specifying the argument name,
  104. and the second is the corresponding value. It shall be an error if the first
  105. member of a pair is not a keyword. It shall be an error if the argument name
  106. is not the same as a variable in a <em class="replaceable"><code>key-arg</code></em>s, unless there
  107. is a <em class="replaceable"><code>rest-arg</code></em>. If the same argument name occurs more than once
  108. in the list of actual arguments, then the first value is used.
  109. If there is no actual argument for a particular <em class="replaceable"><code>key-arg</code></em>,
  110. then the variable is bound
  111. to the corresponding <em class="replaceable"><code>initializer</code></em>, if one was specified, and
  112. otherwise to <code class="literal">#f</code>. The <em class="replaceable"><code>initializer</code></em> is evaluated in an
  113. environment in which all the previous formal parameters have been bound.
  114. </p>
  115. </li>
  116. </ul>
  117. </div>
  118. <p>If a <em class="replaceable"><code>type</code></em> is specified, the corresponding actual argument (or
  119. the <em class="replaceable"><code>initializer</code></em> default value) is coerced to the specified <em class="replaceable"><code>type</code></em>.
  120. In the function body, the parameter has the specified type.
  121. </p>
  122. <p>If <em class="replaceable"><code>rtype</code></em> (the first form of the function body) is an unbound
  123. identifier of the form <code class="literal">&lt;TYPE&gt;</code> (that is the first character
  124. is ‘<code class="literal">&lt;</code>’ and the last is ‘<code class="literal">&gt;</code>’), then that specifies the
  125. function’s return type. It is syntactic sugar for
  126. <code class="literal">(as &lt;TYPE&gt; (begin BODY))</code>.
  127. </p>
  128. <p>You can set the <a class="link" href="Procedure-properties.xhtml" title="Procedure properties">properties</a>
  129. of the resulting procedure using an <em class="replaceable"><code>option-pair</code></em>. For example,
  130. to set the <code class="literal">setter</code> property of a procedure
  131. to <code class="literal">my-set-car</code> do the following:
  132. </p>
  133. <pre class="screen">(define my-car
  134. (lambda (arg) setter: my-set-car (primitive-car arg)))
  135. </pre>
  136. </section>
  137. <footer>
  138. <div class="navfooter">
  139. <p>
  140. Up: <a accesskey="u" href="Procedures.xhtml">Procedures</a></p>
  141. <p>
  142. Previous: <a accesskey="p" href="Generic-procedures.xhtml">Generic (dynamically overloaded) procedures</a></p>
  143. <p>
  144. Next: <a accesskey="n" href="Partial-application.xhtml">Partial application</a></p>
  145. </div>
  146. </footer>
  147. </body>
  148. </html>