code.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" lang="English" xml:lang="English">
  5. <head>
  6. <!-- 2023-11-26 Sun 17:13 -->
  7. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  8. <meta name="viewport" content="width=device-width, initial-scale=1" />
  9. <title>The Seasoned Schemer Notes</title>
  10. <meta name="author" content="Zelphir Kaltstahl" />
  11. <meta name="generator" content="Org Mode" />
  12. <style>
  13. #content { max-width: 60em; margin: auto; }
  14. .title { text-align: center;
  15. margin-bottom: .2em; }
  16. .subtitle { text-align: center;
  17. font-size: medium;
  18. font-weight: bold;
  19. margin-top:0; }
  20. .todo { font-family: monospace; color: red; }
  21. .done { font-family: monospace; color: green; }
  22. .priority { font-family: monospace; color: orange; }
  23. .tag { background-color: #eee; font-family: monospace;
  24. padding: 2px; font-size: 80%; font-weight: normal; }
  25. .timestamp { color: #bebebe; }
  26. .timestamp-kwd { color: #5f9ea0; }
  27. .org-right { margin-left: auto; margin-right: 0px; text-align: right; }
  28. .org-left { margin-left: 0px; margin-right: auto; text-align: left; }
  29. .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  30. .underline { text-decoration: underline; }
  31. #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  32. p.verse { margin-left: 3%; }
  33. pre {
  34. border: 1px solid #e6e6e6;
  35. border-radius: 3px;
  36. background-color: #f2f2f2;
  37. padding: 8pt;
  38. font-family: monospace;
  39. overflow: auto;
  40. margin: 1.2em;
  41. }
  42. pre.src {
  43. position: relative;
  44. overflow: auto;
  45. }
  46. pre.src:before {
  47. display: none;
  48. position: absolute;
  49. top: -8px;
  50. right: 12px;
  51. padding: 3px;
  52. color: #555;
  53. background-color: #f2f2f299;
  54. }
  55. pre.src:hover:before { display: inline; margin-top: 14px;}
  56. /* Languages per Org manual */
  57. pre.src-asymptote:before { content: 'Asymptote'; }
  58. pre.src-awk:before { content: 'Awk'; }
  59. pre.src-authinfo::before { content: 'Authinfo'; }
  60. pre.src-C:before { content: 'C'; }
  61. /* pre.src-C++ doesn't work in CSS */
  62. pre.src-clojure:before { content: 'Clojure'; }
  63. pre.src-css:before { content: 'CSS'; }
  64. pre.src-D:before { content: 'D'; }
  65. pre.src-ditaa:before { content: 'ditaa'; }
  66. pre.src-dot:before { content: 'Graphviz'; }
  67. pre.src-calc:before { content: 'Emacs Calc'; }
  68. pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  69. pre.src-fortran:before { content: 'Fortran'; }
  70. pre.src-gnuplot:before { content: 'gnuplot'; }
  71. pre.src-haskell:before { content: 'Haskell'; }
  72. pre.src-hledger:before { content: 'hledger'; }
  73. pre.src-java:before { content: 'Java'; }
  74. pre.src-js:before { content: 'Javascript'; }
  75. pre.src-latex:before { content: 'LaTeX'; }
  76. pre.src-ledger:before { content: 'Ledger'; }
  77. pre.src-lisp:before { content: 'Lisp'; }
  78. pre.src-lilypond:before { content: 'Lilypond'; }
  79. pre.src-lua:before { content: 'Lua'; }
  80. pre.src-matlab:before { content: 'MATLAB'; }
  81. pre.src-mscgen:before { content: 'Mscgen'; }
  82. pre.src-ocaml:before { content: 'Objective Caml'; }
  83. pre.src-octave:before { content: 'Octave'; }
  84. pre.src-org:before { content: 'Org mode'; }
  85. pre.src-oz:before { content: 'OZ'; }
  86. pre.src-plantuml:before { content: 'Plantuml'; }
  87. pre.src-processing:before { content: 'Processing.js'; }
  88. pre.src-python:before { content: 'Python'; }
  89. pre.src-R:before { content: 'R'; }
  90. pre.src-ruby:before { content: 'Ruby'; }
  91. pre.src-sass:before { content: 'Sass'; }
  92. pre.src-scheme:before { content: 'Scheme'; }
  93. pre.src-screen:before { content: 'Gnu Screen'; }
  94. pre.src-sed:before { content: 'Sed'; }
  95. pre.src-sh:before { content: 'shell'; }
  96. pre.src-sql:before { content: 'SQL'; }
  97. pre.src-sqlite:before { content: 'SQLite'; }
  98. /* additional languages in org.el's org-babel-load-languages alist */
  99. pre.src-forth:before { content: 'Forth'; }
  100. pre.src-io:before { content: 'IO'; }
  101. pre.src-J:before { content: 'J'; }
  102. pre.src-makefile:before { content: 'Makefile'; }
  103. pre.src-maxima:before { content: 'Maxima'; }
  104. pre.src-perl:before { content: 'Perl'; }
  105. pre.src-picolisp:before { content: 'Pico Lisp'; }
  106. pre.src-scala:before { content: 'Scala'; }
  107. pre.src-shell:before { content: 'Shell Script'; }
  108. pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  109. /* additional language identifiers per "defun org-babel-execute"
  110. in ob-*.el */
  111. pre.src-cpp:before { content: 'C++'; }
  112. pre.src-abc:before { content: 'ABC'; }
  113. pre.src-coq:before { content: 'Coq'; }
  114. pre.src-groovy:before { content: 'Groovy'; }
  115. /* additional language identifiers from org-babel-shell-names in
  116. ob-shell.el: ob-shell is the only babel language using a lambda to put
  117. the execution function name together. */
  118. pre.src-bash:before { content: 'bash'; }
  119. pre.src-csh:before { content: 'csh'; }
  120. pre.src-ash:before { content: 'ash'; }
  121. pre.src-dash:before { content: 'dash'; }
  122. pre.src-ksh:before { content: 'ksh'; }
  123. pre.src-mksh:before { content: 'mksh'; }
  124. pre.src-posh:before { content: 'posh'; }
  125. /* Additional Emacs modes also supported by the LaTeX listings package */
  126. pre.src-ada:before { content: 'Ada'; }
  127. pre.src-asm:before { content: 'Assembler'; }
  128. pre.src-caml:before { content: 'Caml'; }
  129. pre.src-delphi:before { content: 'Delphi'; }
  130. pre.src-html:before { content: 'HTML'; }
  131. pre.src-idl:before { content: 'IDL'; }
  132. pre.src-mercury:before { content: 'Mercury'; }
  133. pre.src-metapost:before { content: 'MetaPost'; }
  134. pre.src-modula-2:before { content: 'Modula-2'; }
  135. pre.src-pascal:before { content: 'Pascal'; }
  136. pre.src-ps:before { content: 'PostScript'; }
  137. pre.src-prolog:before { content: 'Prolog'; }
  138. pre.src-simula:before { content: 'Simula'; }
  139. pre.src-tcl:before { content: 'tcl'; }
  140. pre.src-tex:before { content: 'TeX'; }
  141. pre.src-plain-tex:before { content: 'Plain TeX'; }
  142. pre.src-verilog:before { content: 'Verilog'; }
  143. pre.src-vhdl:before { content: 'VHDL'; }
  144. pre.src-xml:before { content: 'XML'; }
  145. pre.src-nxml:before { content: 'XML'; }
  146. /* add a generic configuration mode; LaTeX export needs an additional
  147. (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  148. pre.src-conf:before { content: 'Configuration File'; }
  149. table { border-collapse:collapse; }
  150. caption.t-above { caption-side: top; }
  151. caption.t-bottom { caption-side: bottom; }
  152. td, th { vertical-align:top; }
  153. th.org-right { text-align: center; }
  154. th.org-left { text-align: center; }
  155. th.org-center { text-align: center; }
  156. td.org-right { text-align: right; }
  157. td.org-left { text-align: left; }
  158. td.org-center { text-align: center; }
  159. dt { font-weight: bold; }
  160. .footpara { display: inline; }
  161. .footdef { margin-bottom: 1em; }
  162. .figure { padding: 1em; }
  163. .figure p { text-align: center; }
  164. .equation-container {
  165. display: table;
  166. text-align: center;
  167. width: 100%;
  168. }
  169. .equation {
  170. vertical-align: middle;
  171. }
  172. .equation-label {
  173. display: table-cell;
  174. text-align: right;
  175. vertical-align: middle;
  176. }
  177. .inlinetask {
  178. padding: 10px;
  179. border: 2px solid gray;
  180. margin: 10px;
  181. background: #ffffcc;
  182. }
  183. #org-div-home-and-up
  184. { text-align: right; font-size: 70%; white-space: nowrap; }
  185. textarea { overflow-x: auto; }
  186. .linenr { font-size: smaller }
  187. .code-highlighted { background-color: #ffff00; }
  188. .org-info-js_info-navigation { border-style: none; }
  189. #org-info-js_console-label
  190. { font-size: 10px; font-weight: bold; white-space: nowrap; }
  191. .org-info-js_search-highlight
  192. { background-color: #ffff00; color: #000000; font-weight: bold; }
  193. .org-svg { }
  194. </style>
  195. </head>
  196. <body>
  197. <div id="content" class="content">
  198. <h1 class="title">The Seasoned Schemer Notes
  199. <br />
  200. <span class="subtitle">Chapter 14</span>
  201. </h1>
  202. <div id="table-of-contents" role="doc-toc">
  203. <h2>Table of Contents</h2>
  204. <div id="text-table-of-contents" role="doc-toc">
  205. <ul>
  206. <li><a href="#prerequisites">1. Prerequisites</a></li>
  207. <li><a href="#chapter-15">2. Chapter 15</a>
  208. <ul>
  209. <li><a href="#about">2.1. About</a></li>
  210. <li><a href="#expression-value">2.2. About the value of expressions</a></li>
  211. <li><a href="#differences-define-set-bang">2.3. Differences between <code class="src src-scheme">define</code> and <code class="src src-scheme">set!</code></a></li>
  212. <li><a href="#remembering-things">2.4. Making procedures remember things</a>
  213. <ul>
  214. <li><a href="#assignment-warning">2.4.1. A warning</a></li>
  215. </ul>
  216. </li>
  217. </ul>
  218. </li>
  219. </ul>
  220. </div>
  221. </div>
  222. <div id="outline-container-prerequisites" class="outline-2">
  223. <h2 id="prerequisites"><span class="section-number-2">1.</span> Prerequisites</h2>
  224. <div class="outline-text-2" id="text-prerequisites">
  225. <p>
  226. <code class="src src-scheme">atom?</code> checks, whether a thing is a non-compound thing, at least for simple code, that does not make use of vector and such things.
  227. </p>
  228. <div class="org-src-container">
  229. <pre class="src src-scheme" id="org67c376c">(define atom?
  230. (λ (x)
  231. (and (not (pair? x))
  232. (not (null? x)))))
  233. </pre>
  234. </div>
  235. </div>
  236. </div>
  237. <div id="outline-container-chapter-15" class="outline-2">
  238. <h2 id="chapter-15"><span class="section-number-2">2.</span> Chapter 15</h2>
  239. <div class="outline-text-2" id="text-chapter-15">
  240. </div>
  241. <div id="outline-container-about" class="outline-3">
  242. <h3 id="about"><span class="section-number-3">2.1.</span> About</h3>
  243. <div class="outline-text-3" id="text-about">
  244. <p>
  245. Chapter 15 deals with usage of assignment (<code class="src src-scheme">set!</code>). It explains when usage of <code class="src src-scheme">set!</code> can be kind of safe (at least in a single threaded scenario). It arrives at some rules (commandments) for <code class="src src-scheme">set!</code> usage.
  246. </p>
  247. </div>
  248. </div>
  249. <div id="outline-container-expression-value" class="outline-3">
  250. <h3 id="expression-value"><span class="section-number-3">2.2.</span> About the value of expressions</h3>
  251. <div class="outline-text-3" id="text-expression-value">
  252. <p>
  253. Usually expressions have a value. Exempt from this rule are <code class="src src-scheme">define</code> and the assignment <code class="src src-scheme">set!</code>, which makes them special.
  254. </p>
  255. </div>
  256. </div>
  257. <div id="outline-container-differences-define-set-bang" class="outline-3">
  258. <h3 id="differences-define-set-bang"><span class="section-number-3">2.3.</span> Differences between <code class="src src-scheme">define</code> and <code class="src src-scheme">set!</code></h3>
  259. <div class="outline-text-3" id="text-differences-define-set-bang">
  260. <p>
  261. While <code class="src src-scheme">define</code> can be used in many but not all contexts, <code class="src src-scheme">set!</code> can be used in any context (but really should not).
  262. </p>
  263. <p>
  264. One important difference is, that <code class="src src-scheme">set!</code> can change values of bindings outside of the scope of the context it is used in. It depends on the binding it changes the value of to already exist (at least in GNU Guile it does), then changes the value forever. <code class="src src-scheme">define</code> works differently. It introduces a new binding that can shadow bindings from outer scopes, but once the scope the <code class="src src-scheme">define</code> is used in is left, the previously shadowed binding becomes visibile again.
  265. </p>
  266. </div>
  267. </div>
  268. <div id="outline-container-remembering-things" class="outline-3">
  269. <h3 id="remembering-things"><span class="section-number-3">2.4.</span> Making procedures remember things</h3>
  270. <div class="outline-text-3" id="text-remembering-things">
  271. <p>
  272. <code class="src src-scheme">set!</code> can be used to make procedures remember things. For example:
  273. </p>
  274. <div class="org-src-container">
  275. <pre class="src src-scheme" id="orgdef1218">(define memory 'still-empty)
  276. (define rememberer
  277. (λ (. something-to-remember)
  278. (when (not (null? something-to-remember))
  279. (set! memory something-to-remember))
  280. (cons something-to-remember
  281. (cons memory '()))))
  282. </pre>
  283. </div>
  284. <div class="org-src-container">
  285. <pre class="src src-scheme" id="org51a3dac">
  286. (simple-format #t "~a\n" (rememberer 'something-heartwarming))
  287. (simple-format #t "memory contains: ~a\n" memory)
  288. (newline)
  289. (simple-format #t "~a\n" (rememberer 'another-great-recollection))
  290. (simple-format #t "memory contains: ~a\n" memory)
  291. (newline)
  292. (simple-format #t "~a\n" (rememberer))
  293. (simple-format #t "memory still contains: ~a\n" memory)
  294. </pre>
  295. </div>
  296. <pre class="example" id="org108b01a">
  297. ((something-heartwarming) (something-heartwarming))
  298. memory contains: (something-heartwarming)
  299. ((another-great-recollection) (another-great-recollection))
  300. memory contains: (another-great-recollection)
  301. (() (another-great-recollection))
  302. memory still contains: (another-great-recollection)
  303. </pre>
  304. </div>
  305. <div id="outline-container-assignment-warning" class="outline-4">
  306. <h4 id="assignment-warning"><span class="section-number-4">2.4.1.</span> A warning</h4>
  307. <div class="outline-text-4" id="text-assignment-warning">
  308. <p>
  309. Note however, that this kind of usage of <code class="src src-scheme">set!</code> changes the fundamental character of our definitions. Instead of always returning the same result for the same input, our procedure definitions can now — depending on their memorized things — return different results even though the input is the same. As such they are no longer akin to mathematical functions, which has hugely negative implications for their usage.
  310. </p>
  311. <p>
  312. For example there can be complications, when a binding is changed from more than 1 place in the code, especially when concurrency is involved. Procedures might return unexpected results, due to some other part of the code assigning a new value, changing the value a binding is associated with. This might not seem so dramatic initially, but wait until you want to run code concurrently &#x2026; Then suddenly code cannot simply run concurrently, but needs to be changed to be still correct. Either the whole algorithm implemented has to be changed, or some kind of mutex needs to be introduced, to avoid concurrent code executions messing up each others results. If possible one should avoid the need to change the code.
  313. </p>
  314. </div>
  315. </div>
  316. </div>
  317. </div>
  318. </div>
  319. <div id="postamble" class="status">
  320. <p class="date">Date: 2023-02-26 Sun 00:00</p>
  321. <p class="author">Author: Zelphir Kaltstahl</p>
  322. <p class="date">Created: 2023-11-26 Sun 17:13</p>
  323. <p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
  324. </div>
  325. </body>
  326. </html>