tutorial-Functions.xhtml 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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>Functions</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="tutorial-Numbers.xhtml" title="Numbers"/>
  10. <link rel="next" href="tutorial-Variables.xhtml" title="Variables"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Functions" epub:type="subchapter" id="Tutorial---Functions">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Functions</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>To declare a new function use <code class="literal">define</code>,
  23. which has the following form:
  24. </p>
  25. <pre class="screen">(define (<em class="replaceable"><code>function-name</code></em> <em class="replaceable"><code>parameter-names</code></em>) <em class="replaceable"><code>body</code></em>)
  26. </pre>
  27. <p>This creates a new function named <em class="replaceable"><code>function-name</code></em>,
  28. which takes <em class="replaceable"><code>parameter-names</code></em> as parameters.
  29. When the function is called, the <em class="replaceable"><code>parameter-names</code></em>
  30. are initialized with the actual arguments. Then <em class="replaceable"><code>body</code></em>
  31. is evaluated, and its value becomes the result of the call.
  32. </p>
  33. <p>For example, in the <code class="literal">factorial</code> function we looked at recently,
  34. the <em class="replaceable"><code>function-name</code></em> is <code class="literal">factorial</code>,
  35. and the <em class="replaceable"><code>parameter-names</code></em> is <code class="literal">x</code>:
  36. </p>
  37. <pre class="screen">(define (factorial x)
  38. (if (&lt; x 1) 1
  39. (* x (factorial (- x 1)))))
  40. </pre>
  41. <h3 id="idm139667880574592">Anonymous functions</h3>
  42. <p>An <span class="emphasis"><em>anonymous</em></span> function is simply a function which does not have a name.
  43. We define an anonymous function using a <em class="firstterm">lambda expression</em>, which has
  44. the following form:
  45. </p>
  46. <pre class="screen">(lambda (<em class="replaceable"><code>parameter-names</code></em>) <em class="replaceable"><code>body</code></em>)
  47. </pre>
  48. <p>The lambda expression has the <em class="replaceable"><code>parameter-names</code></em> and <em class="replaceable"><code>body</code></em> of a
  49. function, but it has no name. What is the point of this?
  50. </p>
  51. <p>An important example is creating a function to act on a list, perhaps using
  52. <code class="literal">map</code>. The <code class="literal">map</code> function takes two parameters: the first is a
  53. function which takes a value and returns a value; the second is a list. Here,
  54. we want to double every number in the list.
  55. </p>
  56. <p>The usual way of doing this is to create a named function, called
  57. <code class="literal">double</code>, and then apply it to a list:
  58. </p>
  59. <pre class="screen">#|kawa:1|# (define (double x)
  60. #|.....2|# (* 2 x))
  61. #|kawa:3|# (map double (list 1 2 3 4 5))
  62. (2 4 6 8 10)
  63. </pre>
  64. <p>Instead, anonymous functions make it easy to create a function to work on a
  65. list, without having to define it in advance:
  66. </p>
  67. <pre class="screen">#|kawa:4|# (map (lambda (x) (* 2 x)) (list 1 2 3 4 5))
  68. (2 4 6 8 10)
  69. #|kawa:5|# (define y 3)
  70. #|kawa:6|# (map (lambda (x) (* x y)) (list 1 2 3 4 5))
  71. (3 6 9 12 15)
  72. </pre>
  73. <p>The first example shows the double example rewritten as an anonymous function.
  74. The second example shows how the anonymous function can be changed to
  75. fit the place in which it is used: here, the value of <em class="replaceable"><code>y</code></em> determines the
  76. value by which the list values are multiplied.
  77. </p>
  78. <p>Notice that we can name our anonymous functions, in just the same way we
  79. name any value in Kawa, using <code class="literal">define</code>:
  80. </p>
  81. <pre class="screen">(define double
  82. (lambda (n)
  83. (* 2 n)))
  84. </pre>
  85. <p>although more frequently we use the short-hand for defining functions, which we
  86. have already met:
  87. </p>
  88. <pre class="screen">(define (double n)
  89. (* 2 n))
  90. </pre>
  91. <p>Anonymous functions are “first-class values” in Kawa, and can be passed to
  92. other functions as arguments (like we did with <code class="literal">map</code>), and they can even
  93. be created and returned by functions as results.
  94. </p>
  95. <h3 id="idm139667880561200">Optional, rest and keyword parameters</h3>
  96. <p>You can declare a function that takes optional arguments,
  97. or a variable number of arguments. You can also use keyword parameters.
  98. </p>
  99. <p>The following function illustrates the use of <span class="emphasis"><em>optional</em></span> arguments. The function
  100. identifies an optional argument <code class="literal">z</code>: if the function is called with 3 arguments, <code class="literal">z</code>
  101. will be bound to the third value, otherwise it will be <code class="literal">#f</code>.
  102. </p>
  103. <pre class="screen">(define (addup x y #!optional z)
  104. (if z
  105. (+ x y z)
  106. (+ x y)))
  107. </pre>
  108. <p>The following examples show <code class="literal">addup</code> applied to 2, 3 and invalid arguments. It is an error to
  109. pass just one argument or more than three: <code class="literal">x</code> and <code class="literal">y</code> are compulsory, but <code class="literal">z</code> is
  110. optional.
  111. </p>
  112. <pre class="screen">#|kawa:12|# (addup 1 2)
  113. 3
  114. #|kawa:13|# (addup 1 2 3)
  115. 6
  116. #|kawa:14|# (addup 1)
  117. /dev/stdin:14:1: call to 'addup' has too few arguments (1; min=2, max=3)
  118. #|kawa:15|# (addup 1 2 3 4)
  119. /dev/stdin:15:1: call to 'addup' has too many arguments (4; min=2, max=3)
  120. </pre>
  121. <p>In this example, a better way to define the function would be to include a
  122. default value for <code class="literal">z</code>, for when its value is not given by the caller.
  123. This is done as follows, with the same behavior as above:
  124. </p>
  125. <pre class="screen">(define (addup x y #!optional (z 0))
  126. (+ x y z))
  127. </pre>
  128. <p>You can include as many optional parameters as you wish, after the <code class="literal">#!optional</code>.
  129. </p>
  130. <p><span class="emphasis"><em>Rest</em></span> arguments are an alternative way to pass an undefined number of
  131. arguments to a function. Here is <code class="literal">addup</code> written with rest arguments,
  132. notice the variable name after the . (dot):
  133. </p>
  134. <pre class="screen">(define (addup x y . args)
  135. (+ x y (apply + args)))
  136. </pre>
  137. <p>The <code class="literal">args</code> are simply a list of all remaining values. The following now all work, as the
  138. function only requires a minimum of two numbers:
  139. </p>
  140. <pre class="screen">#|kawa:4|# (addup 1 2)
  141. 3
  142. #|kawa:5|# (addup 1 2 3)
  143. 6
  144. #|kawa:6|# (addup 1 2 3 4 5 6 7 8)
  145. 36
  146. </pre>
  147. <p>An alternative way to identify the rest args is with <code class="literal">#!rest</code>:
  148. </p>
  149. <pre class="screen">(define (addup x y #!rest args)
  150. (+ x y (apply + args)))
  151. </pre>
  152. <p>Finally, it can be useful to identify parameters by name and, for this, Kawa
  153. provides <span class="emphasis"><em>keyword</em></span> arguments. Consider the following function:
  154. </p>
  155. <pre class="screen">(define (vector-3d #!key x y z)
  156. (make-vector x y z))
  157. #|kawa:40|# (vector-3d #:x 2 #:z 3 #:y 4)
  158. #(2 4 3)
  159. </pre>
  160. <p><code class="literal">vector-3d</code> is defined with three keyword arguments: <code class="literal">x</code>, <code class="literal">y</code>, and <code class="literal">z</code>. When the
  161. function is called, we identify the name for each value by writing <code class="literal">#:</code> at the start of the name.
  162. This allows us to write the arguments in any order. Keyword parameters can also be given default
  163. values, as with optional parameters. Keyword parameters with no default value, and no value in the caller,
  164. will get the value <code class="literal">#f</code>.
  165. </p>
  166. <p>In the caller, keywords are symbols with <code class="literal">#:</code> at the front (or <code class="literal">:</code> at
  167. the end): <a class="link" href="Keywords.xhtml" title="Keywords">read more here</a>.
  168. </p>
  169. <p>All these extended types of arguments are available both for “named” and for “anonymous” functions.
  170. Optional, rest and keyword arguments can be mixed together, along with the usual arguments.
  171. For details <a class="link" href="Extended-formals.xhtml" title="Extended Formal Arguments List">read more here.</a>
  172. </p>
  173. </section>
  174. <footer>
  175. <div class="navfooter">
  176. <p>
  177. Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
  178. <p>
  179. Previous: <a accesskey="p" href="tutorial-Numbers.xhtml">Numbers</a></p>
  180. <p>
  181. Next: <a accesskey="n" href="tutorial-Variables.xhtml">Variables</a></p>
  182. </div>
  183. </footer>
  184. </body>
  185. </html>