code.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. <!-- 2019-09-13 Fri 03:26 -->
  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 Little Schemer Notes</title>
  10. <meta name="generator" content="Org mode" />
  11. <meta name="author" content="Zelphir Kaltstahl" />
  12. <style type="text/css">
  13. <!--/*--><![CDATA[/*><!--*/
  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 #ccc;
  35. box-shadow: 3px 3px 3px #eee;
  36. padding: 8pt;
  37. font-family: monospace;
  38. overflow: auto;
  39. margin: 1.2em;
  40. }
  41. pre.src {
  42. position: relative;
  43. overflow: visible;
  44. padding-top: 1.2em;
  45. }
  46. pre.src:before {
  47. display: none;
  48. position: absolute;
  49. background-color: white;
  50. top: -10px;
  51. right: 10px;
  52. padding: 3px;
  53. border: 1px solid black;
  54. }
  55. pre.src:hover:before { display: inline;}
  56. /* Languages per Org manual */
  57. pre.src-asymptote:before { content: 'Asymptote'; }
  58. pre.src-awk:before { content: 'Awk'; }
  59. pre.src-C:before { content: 'C'; }
  60. /* pre.src-C++ doesn't work in CSS */
  61. pre.src-clojure:before { content: 'Clojure'; }
  62. pre.src-css:before { content: 'CSS'; }
  63. pre.src-D:before { content: 'D'; }
  64. pre.src-ditaa:before { content: 'ditaa'; }
  65. pre.src-dot:before { content: 'Graphviz'; }
  66. pre.src-calc:before { content: 'Emacs Calc'; }
  67. pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  68. pre.src-fortran:before { content: 'Fortran'; }
  69. pre.src-gnuplot:before { content: 'gnuplot'; }
  70. pre.src-haskell:before { content: 'Haskell'; }
  71. pre.src-hledger:before { content: 'hledger'; }
  72. pre.src-java:before { content: 'Java'; }
  73. pre.src-js:before { content: 'Javascript'; }
  74. pre.src-latex:before { content: 'LaTeX'; }
  75. pre.src-ledger:before { content: 'Ledger'; }
  76. pre.src-lisp:before { content: 'Lisp'; }
  77. pre.src-lilypond:before { content: 'Lilypond'; }
  78. pre.src-lua:before { content: 'Lua'; }
  79. pre.src-matlab:before { content: 'MATLAB'; }
  80. pre.src-mscgen:before { content: 'Mscgen'; }
  81. pre.src-ocaml:before { content: 'Objective Caml'; }
  82. pre.src-octave:before { content: 'Octave'; }
  83. pre.src-org:before { content: 'Org mode'; }
  84. pre.src-oz:before { content: 'OZ'; }
  85. pre.src-plantuml:before { content: 'Plantuml'; }
  86. pre.src-processing:before { content: 'Processing.js'; }
  87. pre.src-python:before { content: 'Python'; }
  88. pre.src-R:before { content: 'R'; }
  89. pre.src-ruby:before { content: 'Ruby'; }
  90. pre.src-sass:before { content: 'Sass'; }
  91. pre.src-scheme:before { content: 'Scheme'; }
  92. pre.src-screen:before { content: 'Gnu Screen'; }
  93. pre.src-sed:before { content: 'Sed'; }
  94. pre.src-sh:before { content: 'shell'; }
  95. pre.src-sql:before { content: 'SQL'; }
  96. pre.src-sqlite:before { content: 'SQLite'; }
  97. /* additional languages in org.el's org-babel-load-languages alist */
  98. pre.src-forth:before { content: 'Forth'; }
  99. pre.src-io:before { content: 'IO'; }
  100. pre.src-J:before { content: 'J'; }
  101. pre.src-makefile:before { content: 'Makefile'; }
  102. pre.src-maxima:before { content: 'Maxima'; }
  103. pre.src-perl:before { content: 'Perl'; }
  104. pre.src-picolisp:before { content: 'Pico Lisp'; }
  105. pre.src-scala:before { content: 'Scala'; }
  106. pre.src-shell:before { content: 'Shell Script'; }
  107. pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  108. /* additional language identifiers per "defun org-babel-execute"
  109. in ob-*.el */
  110. pre.src-cpp:before { content: 'C++'; }
  111. pre.src-abc:before { content: 'ABC'; }
  112. pre.src-coq:before { content: 'Coq'; }
  113. pre.src-groovy:before { content: 'Groovy'; }
  114. /* additional language identifiers from org-babel-shell-names in
  115. ob-shell.el: ob-shell is the only babel language using a lambda to put
  116. the execution function name together. */
  117. pre.src-bash:before { content: 'bash'; }
  118. pre.src-csh:before { content: 'csh'; }
  119. pre.src-ash:before { content: 'ash'; }
  120. pre.src-dash:before { content: 'dash'; }
  121. pre.src-ksh:before { content: 'ksh'; }
  122. pre.src-mksh:before { content: 'mksh'; }
  123. pre.src-posh:before { content: 'posh'; }
  124. /* Additional Emacs modes also supported by the LaTeX listings package */
  125. pre.src-ada:before { content: 'Ada'; }
  126. pre.src-asm:before { content: 'Assembler'; }
  127. pre.src-caml:before { content: 'Caml'; }
  128. pre.src-delphi:before { content: 'Delphi'; }
  129. pre.src-html:before { content: 'HTML'; }
  130. pre.src-idl:before { content: 'IDL'; }
  131. pre.src-mercury:before { content: 'Mercury'; }
  132. pre.src-metapost:before { content: 'MetaPost'; }
  133. pre.src-modula-2:before { content: 'Modula-2'; }
  134. pre.src-pascal:before { content: 'Pascal'; }
  135. pre.src-ps:before { content: 'PostScript'; }
  136. pre.src-prolog:before { content: 'Prolog'; }
  137. pre.src-simula:before { content: 'Simula'; }
  138. pre.src-tcl:before { content: 'tcl'; }
  139. pre.src-tex:before { content: 'TeX'; }
  140. pre.src-plain-tex:before { content: 'Plain TeX'; }
  141. pre.src-verilog:before { content: 'Verilog'; }
  142. pre.src-vhdl:before { content: 'VHDL'; }
  143. pre.src-xml:before { content: 'XML'; }
  144. pre.src-nxml:before { content: 'XML'; }
  145. /* add a generic configuration mode; LaTeX export needs an additional
  146. (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  147. pre.src-conf:before { content: 'Configuration File'; }
  148. table { border-collapse:collapse; }
  149. caption.t-above { caption-side: top; }
  150. caption.t-bottom { caption-side: bottom; }
  151. td, th { vertical-align:top; }
  152. th.org-right { text-align: center; }
  153. th.org-left { text-align: center; }
  154. th.org-center { text-align: center; }
  155. td.org-right { text-align: right; }
  156. td.org-left { text-align: left; }
  157. td.org-center { text-align: center; }
  158. dt { font-weight: bold; }
  159. .footpara { display: inline; }
  160. .footdef { margin-bottom: 1em; }
  161. .figure { padding: 1em; }
  162. .figure p { text-align: center; }
  163. .inlinetask {
  164. padding: 10px;
  165. border: 2px solid gray;
  166. margin: 10px;
  167. background: #ffffcc;
  168. }
  169. #org-div-home-and-up
  170. { text-align: right; font-size: 70%; white-space: nowrap; }
  171. textarea { overflow-x: auto; }
  172. .linenr { font-size: smaller }
  173. .code-highlighted { background-color: #ffff00; }
  174. .org-info-js_info-navigation { border-style: none; }
  175. #org-info-js_console-label
  176. { font-size: 10px; font-weight: bold; white-space: nowrap; }
  177. .org-info-js_search-highlight
  178. { background-color: #ffff00; color: #000000; font-weight: bold; }
  179. .org-svg { width: 90%; }
  180. /*]]>*/-->
  181. </style>
  182. <script type="text/javascript">
  183. /*
  184. @licstart The following is the entire license notice for the
  185. JavaScript code in this tag.
  186. Copyright (C) 2012-2018 Free Software Foundation, Inc.
  187. The JavaScript code in this tag is free software: you can
  188. redistribute it and/or modify it under the terms of the GNU
  189. General Public License (GNU GPL) as published by the Free Software
  190. Foundation, either version 3 of the License, or (at your option)
  191. any later version. The code is distributed WITHOUT ANY WARRANTY;
  192. without even the implied warranty of MERCHANTABILITY or FITNESS
  193. FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  194. As additional permission under GNU GPL version 3 section 7, you
  195. may distribute non-source (e.g., minimized or compacted) forms of
  196. that code without the copy of the GNU GPL normally required by
  197. section 4, provided you include this license notice and a URL
  198. through which recipients can access the Corresponding Source.
  199. @licend The above is the entire license notice
  200. for the JavaScript code in this tag.
  201. */
  202. <!--/*--><![CDATA[/*><!--*/
  203. function CodeHighlightOn(elem, id)
  204. {
  205. var target = document.getElementById(id);
  206. if(null != target) {
  207. elem.cacheClassElem = elem.className;
  208. elem.cacheClassTarget = target.className;
  209. target.className = "code-highlighted";
  210. elem.className = "code-highlighted";
  211. }
  212. }
  213. function CodeHighlightOff(elem, id)
  214. {
  215. var target = document.getElementById(id);
  216. if(elem.cacheClassElem)
  217. elem.className = elem.cacheClassElem;
  218. if(elem.cacheClassTarget)
  219. target.className = elem.cacheClassTarget;
  220. }
  221. /*]]>*///-->
  222. </script>
  223. </head>
  224. <body>
  225. <div id="content">
  226. <h1 class="title">The Little Schemer Notes</h1>
  227. <div id="table-of-contents">
  228. <h2>Table of Contents</h2>
  229. <div id="text-table-of-contents">
  230. <ul>
  231. <li><a href="#org480bee2">1. Prerequisites</a></li>
  232. <li><a href="#orgca02e4d">2. Chapter code</a>
  233. <ul>
  234. <li><a href="#orgbdf1635">2.1. looking</a></li>
  235. <li><a href="#org1ee8a71">2.2. eternity</a></li>
  236. <li><a href="#org9b60f0d">2.3. shift</a></li>
  237. <li><a href="#orgfcefeb8">2.4. align</a></li>
  238. <li><a href="#org7adaff1">2.5. Weighting function, length* and weight*</a></li>
  239. <li><a href="#org3bd5465">2.6. shuffle</a></li>
  240. <li><a href="#org3e34d1c">2.7. Collatz function</a></li>
  241. <li><a href="#orgacb1bf1">2.8. Ackermann function</a></li>
  242. <li><a href="#org3e1215e">2.9. Halting Problem</a></li>
  243. <li><a href="#org64aa20f">2.10. Y-combinator</a></li>
  244. </ul>
  245. </li>
  246. </ul>
  247. </div>
  248. </div>
  249. <div id="outline-container-org480bee2" class="outline-2">
  250. <h2 id="org480bee2"><span class="section-number-2">1</span> Prerequisites</h2>
  251. <div class="outline-text-2" id="text-1">
  252. <p>
  253. Prerequisites contain code, which is needed to run examples, but is not given in the same chapter.
  254. </p>
  255. <div class="org-src-container">
  256. <pre class="src src-scheme" id="org4be1e6e">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  257. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  258. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  259. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  260. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  261. (<span style="color: #F92672;">cond</span>
  262. [(null? lst)
  263. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  264. [(= one-based-index 1)
  265. (car lst)]
  266. [<span style="color: #F92672;">else</span>
  267. (pick (- one-based-index 1) (cdr lst))])))
  268. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  269. (<span style="color: #F92672;">lambda</span> (something)
  270. (<span style="color: #F92672;">and</span> (not (pair? something))
  271. (not (null? something)))))
  272. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  273. (<span style="color: #F92672;">lambda</span> (pair)
  274. (build (second pair)
  275. (first pair))))
  276. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  277. (<span style="color: #F92672;">lambda</span> (num)
  278. (= (remainder num 2) 0)))
  279. </pre>
  280. </div>
  281. </div>
  282. </div>
  283. <div id="outline-container-orgca02e4d" class="outline-2">
  284. <h2 id="orgca02e4d"><span class="section-number-2">2</span> Chapter code</h2>
  285. <div class="outline-text-2" id="text-2">
  286. </div>
  287. <div id="outline-container-orgbdf1635" class="outline-3">
  288. <h3 id="orgbdf1635"><span class="section-number-3">2.1</span> looking</h3>
  289. <div class="outline-text-3" id="text-2-1">
  290. <p>
  291. Examples where <code>looking</code> finds the symbol:
  292. </p>
  293. <div class="org-src-container">
  294. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  295. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  296. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  297. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  298. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  299. (<span style="color: #F92672;">cond</span>
  300. [(null? lst)
  301. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  302. [(= one-based-index 1)
  303. (car lst)]
  304. [<span style="color: #F92672;">else</span>
  305. (pick (- one-based-index 1) (cdr lst))])))
  306. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  307. (<span style="color: #F92672;">lambda</span> (something)
  308. (<span style="color: #F92672;">and</span> (not (pair? something))
  309. (not (null? something)))))
  310. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  311. (<span style="color: #F92672;">lambda</span> (pair)
  312. (build (second pair)
  313. (first pair))))
  314. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  315. (<span style="color: #F92672;">lambda</span> (num)
  316. (= (remainder num 2) 0)))
  317. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">looking</span>
  318. (<span style="color: #F92672;">lambda</span> (elem list-of-atoms)
  319. (keep-looking elem (pick 1 list-of-atoms) list-of-atoms)))
  320. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">keep-looking</span>
  321. (<span style="color: #F92672;">lambda</span> (searched-symbol current-list-elem list-of-atoms)
  322. (<span style="color: #F92672;">cond</span>
  323. [(number? current-list-elem)
  324. (keep-looking searched-symbol
  325. (pick current-list-elem list-of-atoms)
  326. list-of-atoms)]
  327. [<span style="color: #F92672;">else</span>
  328. (eq? searched-symbol current-list-elem)])))
  329. (looking 'apple '(2 3 4 5 6 apple))
  330. (looking 'apple '(2 10 5 7 6 4 apple 1 1 3))
  331. </pre>
  332. </div>
  333. <pre class="example">
  334. #t
  335. </pre>
  336. <p>
  337. Examples where <code>looking</code> does not find the symbol:
  338. </p>
  339. <div class="org-src-container">
  340. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  341. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  342. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  343. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  344. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  345. (<span style="color: #F92672;">cond</span>
  346. [(null? lst)
  347. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  348. [(= one-based-index 1)
  349. (car lst)]
  350. [<span style="color: #F92672;">else</span>
  351. (pick (- one-based-index 1) (cdr lst))])))
  352. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  353. (<span style="color: #F92672;">lambda</span> (something)
  354. (<span style="color: #F92672;">and</span> (not (pair? something))
  355. (not (null? something)))))
  356. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  357. (<span style="color: #F92672;">lambda</span> (pair)
  358. (build (second pair)
  359. (first pair))))
  360. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  361. (<span style="color: #F92672;">lambda</span> (num)
  362. (= (remainder num 2) 0)))
  363. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">looking</span>
  364. (<span style="color: #F92672;">lambda</span> (elem list-of-atoms)
  365. (keep-looking elem (pick 1 list-of-atoms) list-of-atoms)))
  366. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">keep-looking</span>
  367. (<span style="color: #F92672;">lambda</span> (searched-symbol current-list-elem list-of-atoms)
  368. (<span style="color: #F92672;">cond</span>
  369. [(number? current-list-elem)
  370. (keep-looking searched-symbol
  371. (pick current-list-elem list-of-atoms)
  372. list-of-atoms)]
  373. [<span style="color: #F92672;">else</span>
  374. (eq? searched-symbol current-list-elem)])))
  375. (looking 'apple '(2 3 4 5 6 tea))
  376. (looking 'apple '(2 10 5 7 6 4 orange 1 1 3))
  377. </pre>
  378. </div>
  379. <pre class="example">
  380. #f
  381. </pre>
  382. <p>
  383. Examples that run forever:
  384. </p>
  385. <div class="org-src-container">
  386. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  387. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  388. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  389. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  390. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  391. (<span style="color: #F92672;">cond</span>
  392. [(null? lst)
  393. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  394. [(= one-based-index 1)
  395. (car lst)]
  396. [<span style="color: #F92672;">else</span>
  397. (pick (- one-based-index 1) (cdr lst))])))
  398. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  399. (<span style="color: #F92672;">lambda</span> (something)
  400. (<span style="color: #F92672;">and</span> (not (pair? something))
  401. (not (null? something)))))
  402. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  403. (<span style="color: #F92672;">lambda</span> (pair)
  404. (build (second pair)
  405. (first pair))))
  406. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  407. (<span style="color: #F92672;">lambda</span> (num)
  408. (= (remainder num 2) 0)))
  409. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">looking</span>
  410. (<span style="color: #F92672;">lambda</span> (elem list-of-atoms)
  411. (keep-looking elem (pick 1 list-of-atoms) list-of-atoms)))
  412. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">keep-looking</span>
  413. (<span style="color: #F92672;">lambda</span> (searched-symbol current-list-elem list-of-atoms)
  414. (<span style="color: #F92672;">cond</span>
  415. [(number? current-list-elem)
  416. (keep-looking searched-symbol
  417. (pick current-list-elem list-of-atoms)
  418. list-of-atoms)]
  419. [<span style="color: #F92672;">else</span>
  420. (eq? searched-symbol current-list-elem)])))
  421. (looking 'apple '(1))
  422. (looking 'apple '(2 1))
  423. </pre>
  424. </div>
  425. <p>
  426. The fact that there are examples, for which <code>looking</code> does not return a value, means, that the function is not <i>total</i>, but is <i>partial</i> instead.
  427. </p>
  428. </div>
  429. </div>
  430. <div id="outline-container-org1ee8a71" class="outline-3">
  431. <h3 id="org1ee8a71"><span class="section-number-3">2.2</span> eternity</h3>
  432. <div class="outline-text-3" id="text-2-2">
  433. <p>
  434. <code>eternity</code> is an endlessly recursive procedure. Once it is called anywhere, the execution will be trapped in recurring over and over again.
  435. </p>
  436. <div class="org-src-container">
  437. <pre class="src src-scheme" id="org8e5814d">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">eternity</span>
  438. (<span style="color: #F92672;">lambda</span> (something)
  439. (eternity something)))
  440. </pre>
  441. </div>
  442. </div>
  443. </div>
  444. <div id="outline-container-org9b60f0d" class="outline-3">
  445. <h3 id="org9b60f0d"><span class="section-number-3">2.3</span> shift</h3>
  446. <div class="outline-text-3" id="text-2-3">
  447. <p>
  448. <code>shift</code> changes the structure of sublists in a list.
  449. </p>
  450. <div class="org-src-container">
  451. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  452. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  453. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  454. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  455. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  456. (<span style="color: #F92672;">cond</span>
  457. [(null? lst)
  458. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  459. [(= one-based-index 1)
  460. (car lst)]
  461. [<span style="color: #F92672;">else</span>
  462. (pick (- one-based-index 1) (cdr lst))])))
  463. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  464. (<span style="color: #F92672;">lambda</span> (something)
  465. (<span style="color: #F92672;">and</span> (not (pair? something))
  466. (not (null? something)))))
  467. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  468. (<span style="color: #F92672;">lambda</span> (pair)
  469. (build (second pair)
  470. (first pair))))
  471. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  472. (<span style="color: #F92672;">lambda</span> (num)
  473. (= (remainder num 2) 0)))
  474. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">shift</span>
  475. (<span style="color: #F92672;">lambda</span> (list-with-sublists)
  476. (build (first (first list-with-sublists))
  477. (build (second (first list-with-sublists))
  478. (second list-with-sublists)))))
  479. (display
  480. (simple-format #f <span style="color: #E6DB74;">"unshifted: ~a\n"</span>
  481. '((a b) (c d))))
  482. (display
  483. (simple-format #f <span style="color: #E6DB74;">"shifted: ~a\n"</span>
  484. (shift '((a b) (c d)))))
  485. </pre>
  486. </div>
  487. <pre class="example">
  488. unshifted: ((a b) (c d))
  489. shifted: (a (b (c d)))
  490. </pre>
  491. </div>
  492. </div>
  493. <div id="outline-container-orgfcefeb8" class="outline-3">
  494. <h3 id="orgfcefeb8"><span class="section-number-3">2.4</span> align</h3>
  495. <div class="outline-text-3" id="text-2-4">
  496. <div class="org-src-container">
  497. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">align</span>
  498. (<span style="color: #F92672;">lambda</span> (pair-or-atom)
  499. (<span style="color: #F92672;">cond</span>
  500. [(atom? pair-or-atom)
  501. pair-or-atom]
  502. [(pair? (car pair-or-atom))
  503. (align (shift pair-or-atom))]
  504. [<span style="color: #F92672;">else</span>
  505. (build (first pair-or-atom)
  506. (align (second pair-or-atom)))])))
  507. </pre>
  508. </div>
  509. </div>
  510. </div>
  511. <div id="outline-container-org7adaff1" class="outline-3">
  512. <h3 id="org7adaff1"><span class="section-number-3">2.5</span> Weighting function, length* and weight*</h3>
  513. <div class="outline-text-3" id="text-2-5">
  514. <p>
  515. This is about defining a procedure to estimate how long a procedure will need for evaluating with a given input.
  516. This is called <code>length*</code> in the book.
  517. </p>
  518. <div class="org-src-container">
  519. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  520. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  521. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  522. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  523. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  524. (<span style="color: #F92672;">cond</span>
  525. [(null? lst)
  526. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  527. [(= one-based-index 1)
  528. (car lst)]
  529. [<span style="color: #F92672;">else</span>
  530. (pick (- one-based-index 1) (cdr lst))])))
  531. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  532. (<span style="color: #F92672;">lambda</span> (something)
  533. (<span style="color: #F92672;">and</span> (not (pair? something))
  534. (not (null? something)))))
  535. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  536. (<span style="color: #F92672;">lambda</span> (pair)
  537. (build (second pair)
  538. (first pair))))
  539. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  540. (<span style="color: #F92672;">lambda</span> (num)
  541. (= (remainder num 2) 0)))
  542. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">length*</span>
  543. (<span style="color: #F92672;">lambda</span> (pair-or-atom)
  544. (<span style="color: #F92672;">cond</span>
  545. [(null? pair-or-atom) 0]
  546. [(atom? pair-or-atom) 1]
  547. [<span style="color: #F92672;">else</span>
  548. (+ (length* (car pair-or-atom))
  549. (length* (cdr pair-or-atom)))])))
  550. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (length* (cons 1 2))))
  551. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (length* (cons (cons 1 2) 3))))
  552. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (length* (cons 1 (cons 2 3)))))
  553. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (length* (cons (cons 1 2) (cons 3 4)))))
  554. </pre>
  555. </div>
  556. <pre class="example">
  557. 2
  558. 3
  559. 3
  560. 4
  561. </pre>
  562. <p>
  563. Pairs in the first part of the <code>pair-or-atom</code> are treated specially and the pair is <code>shift</code>-ed. This means, that if there are more (nested) pairs in the first part of the pair, the whole thing will require more time to compute. This means, that <code>length*</code> might not be a good measure for how much time the computation will take. The book argues, that a procedure weighing the first part more than the second part would be more appropriate and gives the following procedure:
  564. </p>
  565. <div class="org-src-container">
  566. <pre class="src src-scheme">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  567. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  568. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  569. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  570. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  571. (<span style="color: #F92672;">cond</span>
  572. [(null? lst)
  573. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  574. [(= one-based-index 1)
  575. (car lst)]
  576. [<span style="color: #F92672;">else</span>
  577. (pick (- one-based-index 1) (cdr lst))])))
  578. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  579. (<span style="color: #F92672;">lambda</span> (something)
  580. (<span style="color: #F92672;">and</span> (not (pair? something))
  581. (not (null? something)))))
  582. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  583. (<span style="color: #F92672;">lambda</span> (pair)
  584. (build (second pair)
  585. (first pair))))
  586. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  587. (<span style="color: #F92672;">lambda</span> (num)
  588. (= (remainder num 2) 0)))
  589. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">weight*</span>
  590. (<span style="color: #F92672;">lambda</span> (pair-or-atom)
  591. (<span style="color: #F92672;">cond</span>
  592. [(null? pair-or-atom) 0]
  593. [(atom? pair-or-atom) 1]
  594. [<span style="color: #F92672;">else</span>
  595. (+ (* (weight* (first pair-or-atom)) 2)
  596. (weight* (second pair-or-atom)))])))
  597. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (weight* '(1 2))))
  598. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (weight* '((1 2) 3))))
  599. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (weight* '(1 (2 3)))))
  600. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (weight* '((1 2) (3 4)))))
  601. </pre>
  602. </div>
  603. <pre class="example">
  604. 3
  605. 7
  606. 5
  607. 9
  608. </pre>
  609. </div>
  610. </div>
  611. <div id="outline-container-org3bd5465" class="outline-3">
  612. <h3 id="org3bd5465"><span class="section-number-3">2.6</span> shuffle</h3>
  613. <div class="outline-text-3" id="text-2-6">
  614. <div class="org-src-container">
  615. <pre class="src src-scheme" id="org319c6a0">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  616. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  617. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  618. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  619. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  620. (<span style="color: #F92672;">cond</span>
  621. [(null? lst)
  622. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  623. [(= one-based-index 1)
  624. (car lst)]
  625. [<span style="color: #F92672;">else</span>
  626. (pick (- one-based-index 1) (cdr lst))])))
  627. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  628. (<span style="color: #F92672;">lambda</span> (something)
  629. (<span style="color: #F92672;">and</span> (not (pair? something))
  630. (not (null? something)))))
  631. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  632. (<span style="color: #F92672;">lambda</span> (pair)
  633. (build (second pair)
  634. (first pair))))
  635. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  636. (<span style="color: #F92672;">lambda</span> (num)
  637. (= (remainder num 2) 0)))
  638. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">shuffle</span>
  639. (<span style="color: #F92672;">lambda</span> (pair-or-atom)
  640. (<span style="color: #F92672;">cond</span>
  641. [(atom? pair-or-atom) pair-or-atom]
  642. <span style="color: #75715E;">;; </span><span style="color: #75715E;">here is the problematic case</span>
  643. [(pair? (first pair-or-atom))
  644. <span style="color: #75715E;">;; </span><span style="color: #75715E;">still the same amount of atoms in the argument for recurring</span>
  645. (shuffle (revpair pair-or-atom))]
  646. [<span style="color: #F92672;">else</span>
  647. (build (first pair-or-atom)
  648. (shuffle (second pair-or-atom)))])))
  649. </pre>
  650. </div>
  651. <p>
  652. <code>shuffle</code> is shown to be a partial function, as it recurs indefinitely for some inputs and thus does not return a value for every input. It recurs indefinitely for pairs, where the first part and the seconf part are pairs.
  653. </p>
  654. </div>
  655. </div>
  656. <div id="outline-container-org3e34d1c" class="outline-3">
  657. <h3 id="org3e34d1c"><span class="section-number-3">2.7</span> Collatz function</h3>
  658. <div class="outline-text-3" id="text-2-7">
  659. <div class="org-src-container">
  660. <pre class="src src-scheme" id="org5e3d44d">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">first</span> car)
  661. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">second</span> cadr)
  662. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">build</span> list)
  663. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">pick</span>
  664. (<span style="color: #F92672;">lambda</span> (one-based-index lst)
  665. (<span style="color: #F92672;">cond</span>
  666. [(null? lst)
  667. (error <span style="color: #E6DB74;">"index error, index not in list"</span> one-based-index lst)]
  668. [(= one-based-index 1)
  669. (car lst)]
  670. [<span style="color: #F92672;">else</span>
  671. (pick (- one-based-index 1) (cdr lst))])))
  672. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">atom?</span>
  673. (<span style="color: #F92672;">lambda</span> (something)
  674. (<span style="color: #F92672;">and</span> (not (pair? something))
  675. (not (null? something)))))
  676. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">revpair</span>
  677. (<span style="color: #F92672;">lambda</span> (pair)
  678. (build (second pair)
  679. (first pair))))
  680. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">even?</span>
  681. (<span style="color: #F92672;">lambda</span> (num)
  682. (= (remainder num 2) 0)))
  683. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">C</span>
  684. (<span style="color: #F92672;">lambda</span> (num)
  685. (<span style="color: #F92672;">cond</span>
  686. [(= num 1) 1]
  687. [(even? num) (C (/ num 2))]
  688. [<span style="color: #F92672;">else</span> (C (+ (* num 3) 1))])))
  689. (display (simple-format #f <span style="color: #E6DB74;">"C(1) = ~a\n"</span> (C 1)))
  690. (display (simple-format #f <span style="color: #E6DB74;">"C(27) = ~a\n"</span> (C 27)))
  691. </pre>
  692. </div>
  693. <pre class="example">
  694. C(1) = 1
  695. C(27) = 1
  696. </pre>
  697. <p>
  698. We can refactor <code>C</code> into a <code>collatz-step</code>, which calculates the next value passed to C and <code>collatz</code>, which calls itself. Then we can output the number of iterations easily by counting:
  699. </p>
  700. <div class="org-src-container">
  701. <pre class="src src-scheme" id="orgb411e8a">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">collatz-step</span>
  702. (<span style="color: #F92672;">lambda</span> (num)
  703. (<span style="color: #F92672;">cond</span>
  704. [(even? num) (/ num 2)]
  705. [<span style="color: #F92672;">else</span> (+ (* num 3) 1)])))
  706. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">collatz-count</span>
  707. (<span style="color: #F92672;">lambda</span> (n)
  708. (<span style="color: #F92672;">let</span> <span style="color: #A6E22E;">loop</span> ((num n) (count 0))
  709. (<span style="color: #F92672;">cond</span>
  710. [(= num 1) count]
  711. [<span style="color: #F92672;">else</span>
  712. (loop (collatz-step num)
  713. (+ count 1))]))))
  714. </pre>
  715. </div>
  716. <p>
  717. Then we can easily write a procedure, which tries numbers, until a given number of iterations is exeeced and give us back the number, for which that was the case:
  718. </p>
  719. <div class="org-src-container">
  720. <pre class="src src-scheme" id="org56d34d1">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">collatz-step</span>
  721. (<span style="color: #F92672;">lambda</span> (num)
  722. (<span style="color: #F92672;">cond</span>
  723. [(even? num) (/ num 2)]
  724. [<span style="color: #F92672;">else</span> (+ (* num 3) 1)])))
  725. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">collatz-count</span>
  726. (<span style="color: #F92672;">lambda</span> (n)
  727. (<span style="color: #F92672;">let</span> <span style="color: #A6E22E;">loop</span> ((num n) (count 0))
  728. (<span style="color: #F92672;">cond</span>
  729. [(= num 1) count]
  730. [<span style="color: #F92672;">else</span>
  731. (loop (collatz-step num)
  732. (+ count 1))]))))
  733. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">collatz-find</span>
  734. (<span style="color: #F92672;">lambda</span> (max-iters)
  735. (<span style="color: #F92672;">let</span> <span style="color: #A6E22E;">loop</span> ([current-number 1])
  736. (<span style="color: #F92672;">cond</span>
  737. [(&gt;= (collatz-count current-number) max-iters) current-number]
  738. [<span style="color: #F92672;">else</span> (loop (+ current-number 1))]))))
  739. </pre>
  740. </div>
  741. <p>
  742. Enough fun with Collatz for now.
  743. </p>
  744. </div>
  745. </div>
  746. <div id="outline-container-orgacb1bf1" class="outline-3">
  747. <h3 id="orgacb1bf1"><span class="section-number-3">2.8</span> Ackermann function</h3>
  748. <div class="outline-text-3" id="text-2-8">
  749. <div class="org-src-container">
  750. <pre class="src src-scheme" id="orge8e0c93">(<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">zero?</span>
  751. (<span style="color: #F92672;">lambda</span> (num)
  752. (= num 0)))
  753. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">A</span>
  754. (<span style="color: #F92672;">lambda</span> (n m)
  755. (<span style="color: #F92672;">cond</span>
  756. [(zero? n) (+ m 1)]
  757. [(zero? m) (A (- n 1) 1)]
  758. [<span style="color: #F92672;">else</span>
  759. (A (- n 1)
  760. (A n (- m 1)))])))
  761. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (A 1 2)))
  762. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (A 2 2)))
  763. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (A 3 2)))
  764. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (A 3 4)))
  765. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (A 3 5)))
  766. <span style="color: #75715E;">;; </span><span style="color: #75715E;">do not try: (display (simple-format #f "~a\n" (A 4 3)))</span>
  767. (display (simple-format #f <span style="color: #E6DB74;">"~a\n"</span> (A 4 0)))
  768. </pre>
  769. </div>
  770. <pre class="example">
  771. 4
  772. 7
  773. 29
  774. 125
  775. 253
  776. 13
  777. </pre>
  778. <p>
  779. The numbers grow very quickly and calculating something like <code>(A 4 3)</code> is already infeasible.
  780. </p>
  781. </div>
  782. </div>
  783. <div id="outline-container-org3e1215e" class="outline-3">
  784. <h3 id="org3e1215e"><span class="section-number-3">2.9</span> Halting Problem</h3>
  785. <div class="outline-text-3" id="text-2-9">
  786. <p>
  787. This part treats the halting problem, without mentioning its name explicitly, but it shows, that it is impossible to define a procedure <code>will-stop?</code>, which tells us for arbitrary procedures and their arbitrary inputs, whether the execution of them will halt.
  788. </p>
  789. <p>
  790. To show this a procedure is defined:
  791. </p>
  792. <div class="org-src-container">
  793. <pre class="src src-scheme" id="org0eb7712"><span style="color: #75715E;">;; </span><span style="color: #75715E;">prerequisites start</span>
  794. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">eternity</span>
  795. (<span style="color: #F92672;">lambda</span> (something)
  796. (eternity something)))
  797. <span style="color: #75715E;">;; </span><span style="color: #75715E;">prerequisites end</span>
  798. (<span style="color: #F92672;">define</span> <span style="color: #A6E22E;">weird-proc</span>
  799. (<span style="color: #F92672;">lambda</span> (anything)
  800. (<span style="color: #F92672;">and</span> (will-stop? weird-proc)
  801. (eternity anything))))
  802. </pre>
  803. </div>
  804. <p>
  805. The procedure <code>weird-proc</code> calls <code>will-stop?</code> to check, if itself will stop.
  806. </p>
  807. <p>
  808. There are 2 cases for a predicate such as <code>will-stop?</code>. Either it returns true or it returns false, stating that either <code>weird-proc</code> will halt or that it will not.
  809. </p>
  810. <p>
  811. Let us take a look at the two cases.
  812. </p>
  813. </div>
  814. <div id="outline-container-org9ddc4de" class="outline-4">
  815. <h4 id="org9ddc4de"><span class="section-number-4">2.9.1</span> Case 1: <code>will-stop?</code> returns false</h4>
  816. <div class="outline-text-4" id="text-2-9-1">
  817. <p>
  818. Let us assume for a moment, that <code>will-stop?</code> will return the value false, stating, that <code>weird-proc</code> will not halt.
  819. </p>
  820. <p>
  821. In this case the the first argument to <code>and</code> will be false. This means, that logically the whole <code>and</code> expression will be false. If that is the case however, <code>weird-proc</code> will have finished, as all the expressions in its body have been evaluated and the return value of <code>weird-proc</code> is false. This means, that the call to <code>will-stop?</code> in the body of <code>weird-proc</code> gave the wrong result.
  822. </p>
  823. <p>
  824. If false is the wrong result of <code>will-stop?</code> then the correct result must surely be the value true.
  825. </p>
  826. </div>
  827. </div>
  828. <div id="outline-container-org0436c47" class="outline-4">
  829. <h4 id="org0436c47"><span class="section-number-4">2.9.2</span> Case 2: <code>will-stop?</code> returns true</h4>
  830. <div class="outline-text-4" id="text-2-9-2">
  831. <p>
  832. Let us assume for a moment, that <code>will-stop?</code> will return the value true, stating, that <code>weird-proc</code> will halt.
  833. </p>
  834. <p>
  835. In this case the first argument to <code>and</code> will be the value true. This means, that for <code>and</code> to decide its result, it needs to consider the second argument. This however, means that <code>eternity</code> needs to be evaluated at some point to get its return value. This is not possible though, because <code>eternity</code> recurs infinitely and will never return a value. This means, that the execution of <code>weird-proc</code>, will never end, because the call to <code>eternity</code> is in its body. The procedure <code>will-stop?</code> lied to us again.
  836. </p>
  837. </div>
  838. </div>
  839. <div id="outline-container-orgd2749df" class="outline-4">
  840. <h4 id="orgd2749df"><span class="section-number-4">2.9.3</span> Conclusion</h4>
  841. <div class="outline-text-4" id="text-2-9-3">
  842. <p>
  843. No matter what return value we assume the procedure <code>will-stop?</code> to return, we can construct an example, in which any boolean return value will be incorrect. This means, that we cannot write the procedure <code>will-stop?</code>. This is known as the "Halting Problem". It is not decidable, whether a procedure will halt for all possible inputs, without loss of generality.
  844. </p>
  845. </div>
  846. </div>
  847. </div>
  848. <div id="outline-container-org64aa20f" class="outline-3">
  849. <h3 id="org64aa20f"><span class="section-number-3">2.10</span> Y-combinator</h3>
  850. <div class="outline-text-3" id="text-2-10">
  851. <p>
  852. The following is a way to get to the so called Y-combinator. It is a thought experiment assuming, that we have no <code>define</code> form available and are trying to implement the function <code>length</code> for lists without making use of <code>define</code>. In the end a way will be found. This way is the Y-combinator, a function, which can turn a function <i>f</i> into a function <i>f-wrap</i>, which applies <i>f</i> again and again, as required by the nature of its input to recursively consume all of the input. Since no <code>define</code> form is used anywhere to write the Y-combinator, the body of the Y-combinator cannot refer to any definitions, except for names of values in respective scopes. This property enables the Y-combinator to implement recursion in a programming language, which does not support recursion natively, as long as it has lambda expressions, function application and (not 100% sure it is required) lexical scope. If a language forbids calling a procedure from within itself, then the Y-combinator is still valid code, as it does not do so. It in turn can produce an <i>f-wrap</i>, which will apply an <i>f</i> over and over again, creating the same effect, as if <i>f</i> was recursive itself. To deny the use of the <code>define</code> form actually is intentionally done in the book, because then only the Y-combinator or something with the properties of the Y-combinator can help us solve the problem.
  853. </p>
  854. <p>
  855. On the way there, a few common refactoring steps or equivalent transformations for code are applied. Those are the following:
  856. </p>
  857. <p>
  858. TODO: make a list of them here.
  859. </p>
  860. </div>
  861. </div>
  862. </div>
  863. </div>
  864. <div id="postamble" class="status">
  865. <p class="author">Author: Zelphir Kaltstahl</p>
  866. <p class="date">Created: 2019-09-13 Fri 03:26</p>
  867. <p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
  868. </div>
  869. </body>
  870. </html>