Parameter-objects.xhtml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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>Parameter objects</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="Eval-and-Environments.xhtml" title="Eval and Environments"/>
  10. <link rel="next" href="Debugging.xhtml" title="Debugging"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Parameter objects" epub:type="subchapter" id="Parameter-objects">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Parameter objects</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>A parameter object is a procedure that is bound to a location,
  23. and may optionally have a conversion procedure.
  24. The procedure accepts zero or one argument.
  25. When the procedure is called with zero arguments,
  26. the content of the location is returned.
  27. On a call with one argument the content of the location
  28. is updated with the result of applying the parameter object’s conversion
  29. procedure to the argument.
  30. </p>
  31. <p>Parameter objects are created with the <code class="literal">make-parameter</code> procedure
  32. which takes one or two arguments. The second argument is a one
  33. argument conversion procedure. If only one argument is passed to
  34. make-parameter the identity function is used as a conversion
  35. procedure.
  36. A new location is created and asociated with the
  37. parameter object. The initial content of the location is the
  38. result of applying the conversion procedure to the first argument of
  39. make-parameter.
  40. </p>
  41. <p>Note that the conversion procedure can be used for guaranteeing the
  42. type of the parameter object’s binding and/or to perform some
  43. conversion of the value.
  44. </p>
  45. <p>The <code class="literal">parameterize</code> special form, when given a parameter object
  46. and a value, binds the parameter
  47. object to a new location for the dynamic extent of its body.
  48. The initial content of the location is the result of
  49. applying the parameter object’s conversion procedure to the value. The
  50. <code class="literal">parameterize</code> special form behaves analogously to <code class="literal">let</code>
  51. when binding more than one parameter object (that is the order of
  52. evaluation is unspecified and the new bindings are only visible in the
  53. body of the parameterize special form).
  54. </p>
  55. <p>When a new thread is created using <code class="literal">future</code> or <code class="literal">runnable</code>
  56. then the child thread inherits initial values from its parent.
  57. Once the child is running, changing the value in the child does not
  58. affect the value in the parent or vice versa.
  59. (In the past this was not the case: The child would share a location
  60. with the parent except within a <code class="literal">parameterize</code>.
  61. This was changed to avoid unsafe and inefficient coupling between threads.)
  62. </p>
  63. <p>Note that <code class="literal">parameterize</code> and <code class="literal">fluid-let</code> have similar
  64. binding and sharing behavior.
  65. The difference is that <code class="literal">fluid-let</code> modifies locations
  66. accessed by name, while <code class="literal">make-parameter</code> and <code class="literal">parameterize</code>
  67. create anonymous locations accessed by calling a parameter procedure.
  68. </p>
  69. <p>The R5RS procedures <code class="literal">current-input-port</code> and <code class="literal">current-output-port</code>
  70. are parameter objects.
  71. </p>
  72. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667873083616" class="indexterm"/> <code class="function">make-parameter</code> <em class="replaceable"><code>init</code></em> [<em class="replaceable"><code>converter</code></em>]</p>
  73. <div class="blockquote">
  74. <blockquote class="blockquote">
  75. <p>Returns a new parameter object which is bound in the global dynamic
  76. environment to a location containing the value returned by the call
  77. <code class="literal">(<em class="replaceable"><code>converter</code></em> <em class="replaceable"><code>init</code></em>)</code>. If the conversion procedure
  78. converter is not specified the identity function is used instead.
  79. </p>
  80. <p>The parameter object is a procedure which accepts zero or one
  81. argument. When it is called with no argument, the content of the
  82. location bound to this parameter object in the current dynamic
  83. environment is returned. When it is called with one argument, the
  84. content of the location is set to the result of the call
  85. <code class="literal">(<em class="replaceable"><code>converter</code></em> <em class="replaceable"><code>arg</code></em>)</code>, where <em class="replaceable"><code>arg</code></em> is the argument
  86. passed to the parameter object, and an unspecified value is returned.
  87. </p>
  88. <pre class="screen">(define radix
  89. (make-parameter 10))
  90. (define write-shared
  91. (make-parameter
  92. #f
  93. (lambda (x)
  94. (if (boolean? x)
  95. x
  96. (error "only booleans are accepted by write-shared")))))
  97. (radix) ⇒ 10
  98. (radix 2)
  99. (radix) ⇒ 2
  100. (write-shared 0) gives an error
  101. (define prompt
  102. (make-parameter
  103. 123
  104. (lambda (x)
  105. (if (string? x)
  106. x
  107. (with-output-to-string (lambda () (write x)))))))
  108. (prompt) ⇒ "123"
  109. (prompt "&gt;")
  110. (prompt) ⇒ "&gt;"
  111. </pre>
  112. </blockquote>
  113. </div>
  114. <span id="parameterize-syntax"/>
  115. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667873073872" class="indexterm"/> <code class="function">parameterize</code> ((<em class="replaceable"><code>expr1</code></em> <em class="replaceable"><code>expr2</code></em>) <em class="replaceable"><code>...</code></em>) <em class="replaceable"><code><a class="link" href="Bodies.xhtml#meta-body"><em class="replaceable"><code>body</code></em></a></code></em></p>
  116. <div class="blockquote">
  117. <blockquote class="blockquote">
  118. <p>The expressions <em class="replaceable"><code>expr1</code></em> and <em class="replaceable"><code>expr2</code></em> are evaluated in an
  119. unspecified order. The value of the <em class="replaceable"><code>expr1</code></em> expressions must be
  120. parameter objects. For each <em class="replaceable"><code>expr1</code></em> expression and in an
  121. unspecified order, the local dynamic environment is extended with a
  122. binding of the parameter object <em class="replaceable"><code>expr1</code></em> to a new location whose
  123. content is the result of the call <code class="literal">(<em class="replaceable"><code>converter</code></em> <em class="replaceable"><code>val</code></em>)</code>,
  124. where <em class="replaceable"><code>val</code></em> is the value of <em class="replaceable"><code>expr2</code></em> and <em class="replaceable"><code>converter</code></em> is the
  125. conversion procedure of the parameter object. The resulting dynamic
  126. environment is then used for the evaluation of <em class="replaceable"><code>body</code></em> (which
  127. refers to the R5RS grammar nonterminal of that name). The result(s) of
  128. the parameterize form are the result(s) of the <em class="replaceable"><code>body</code></em>.
  129. </p>
  130. <pre class="screen">(radix) ⇒ 2
  131. (parameterize ((radix 16)) (radix)) ⇒ 16
  132. (radix) ⇒ 2
  133. (define (f n) (number-&gt;string n (radix)))
  134. (f 10) ⇒ "1010"
  135. (parameterize ((radix 8)) (f 10)) ⇒ "12"
  136. (parameterize ((radix 8) (prompt (f 10))) (prompt)) ⇒ "1010"
  137. </pre>
  138. </blockquote>
  139. </div>
  140. </section>
  141. <footer>
  142. <div class="navfooter">
  143. <p>
  144. Up: <a accesskey="u" href="Eval-and-Environments.xhtml">Eval and Environments</a></p>
  145. <p>
  146. Previous: <a accesskey="p" href="Locations.xhtml">Locations</a></p>
  147. </div>
  148. </footer>
  149. </body>
  150. </html>