common-lisp-to-scheme.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. <!-- 2020-05-03 So 14:06 -->
  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>Common Lisp to Scheme translation</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-2019 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">Common Lisp to Scheme translation
  227. <br />
  228. <span class="subtitle">Help for translating the book's code to Scheme</span>
  229. </h1>
  230. <div id="table-of-contents">
  231. <h2>Table of Contents</h2>
  232. <div id="text-table-of-contents">
  233. <ul>
  234. <li><a href="#org036c9f9">1. Common Lisp to Scheme translation</a>
  235. <ul>
  236. <li><a href="#org4eb5e32">1.1. Straight forward translations</a></li>
  237. <li><a href="#org724aca9">1.2. Others</a></li>
  238. </ul>
  239. </li>
  240. </ul>
  241. </div>
  242. </div>
  243. <div id="outline-container-org036c9f9" class="outline-2">
  244. <h2 id="org036c9f9"><span class="section-number-2">1</span> Common Lisp to Scheme translation</h2>
  245. <div class="outline-text-2" id="text-1">
  246. </div>
  247. <div id="outline-container-org4eb5e32" class="outline-3">
  248. <h3 id="org4eb5e32"><span class="section-number-3">1.1</span> Straight forward translations</h3>
  249. <div class="outline-text-3" id="text-1-1">
  250. <ul class="org-ul">
  251. <li><code>defun</code> -&gt; <code>define</code></li>
  252. <li><code>defvar</code> -&gt; <code>define</code> or <code>let</code></li>
  253. <li><code>setf</code> -&gt; <code>define</code> or <code>let</code></li>
  254. <li><code>function</code> or <code>#'</code> -&gt; nothing, no difference is made in referring to values or procedures, Scheme is a Lisp 1.</li>
  255. <li><code>mapcar</code> -&gt; <code>map</code></li>
  256. <li><code>funcall</code> -&gt; simply call the procedure, s-expr with procedure in the first place</li>
  257. </ul>
  258. </div>
  259. </div>
  260. <div id="outline-container-org724aca9" class="outline-3">
  261. <h3 id="org724aca9"><span class="section-number-3">1.2</span> Others</h3>
  262. <div class="outline-text-3" id="text-1-2">
  263. </div>
  264. <div id="outline-container-orgbd8e713" class="outline-4">
  265. <h4 id="orgbd8e713"><span class="section-number-4">1.2.1</span> NIL</h4>
  266. <div class="outline-text-4" id="text-1-2-1">
  267. <p>
  268. In Common Lisp <code>NIL</code> is a defined symbol:
  269. </p>
  270. <div class="org-src-container">
  271. <pre class="src src-scheme" id="org1e99618">(<span style="color: #9FCA56;">define</span> <span style="color: #55B5DB;">NIL</span> '())
  272. </pre>
  273. </div>
  274. </div>
  275. </div>
  276. <div id="outline-container-org80d76c3" class="outline-4">
  277. <h4 id="org80d76c3"><span class="section-number-4">1.2.2</span> car</h4>
  278. <div class="outline-text-4" id="text-1-2-2">
  279. <p>
  280. <code>car</code> of an empty list in Common Lisp returns NIL, the empty list again. <code>car</code> in Scheme does not return the empty list, but raises an error. This means, that, for compatibility reasons, we have to write a little procedure, which acts like <code>car</code> from Common Lisp:
  281. </p>
  282. <div class="org-src-container">
  283. <pre class="src src-scheme">(<span style="color: #9FCA56;">define</span> <span style="color: #55B5DB;">NIL</span> '())
  284. (<span style="color: #9FCA56;">define</span> <span style="color: #55B5DB;">car/nil</span>
  285. (<span style="color: #9FCA56;">lambda</span> (possibly-empty-list)
  286. <span style="color: #55B5DB;">"In Common Lisp car of an empty list returns the empty list called NIL. In</span>
  287. <span style="color: #55B5DB;">Scheme it would be an error to call car on an empty list. We write a wrapper, to</span>
  288. <span style="color: #55B5DB;">avoid an error."</span>
  289. (<span style="color: #9FCA56;">cond</span>
  290. [(null? possibly-empty-list) NIL]
  291. [<span style="color: #9FCA56;">else</span> (car possibly-empty-list)])))
  292. </pre>
  293. </div>
  294. </div>
  295. </div>
  296. <div id="outline-container-org610dbc1" class="outline-4">
  297. <h4 id="org610dbc1"><span class="section-number-4">1.2.3</span> rest</h4>
  298. <div class="outline-text-4" id="text-1-2-3">
  299. <p>
  300. <code>rest</code> in Common Lisp also returns <code>NIL</code>, when called with the empty list as argument, so a wrapper is needed:
  301. </p>
  302. <div class="org-src-container">
  303. <pre class="src src-scheme">(<span style="color: #9FCA56;">define</span> <span style="color: #55B5DB;">NIL</span> '())
  304. (<span style="color: #9FCA56;">define</span> <span style="color: #55B5DB;">rest</span>
  305. (<span style="color: #9FCA56;">lambda</span> (lst)
  306. (<span style="color: #9FCA56;">cond</span>
  307. [(null? lst) NIL]
  308. [<span style="color: #9FCA56;">else</span> (cdr lst)])))
  309. </pre>
  310. </div>
  311. </div>
  312. </div>
  313. <div id="outline-container-orgf608477" class="outline-4">
  314. <h4 id="orgf608477"><span class="section-number-4">1.2.4</span> defparameter</h4>
  315. <div class="outline-text-4" id="text-1-2-4">
  316. <p>
  317. A parameter is explained in the book as a binding, that usually does not chance, a constant. A change to it is explained as a change <i>to</i> the program, not a change <i>by</i> the program, in contrast to using <code>defvar</code>.
  318. </p>
  319. <p>
  320. Since in Scheme we usually try to avoid using assignment or <code>set!</code> anyway, we can get away with following a convention of naming constants with enclosing asterisks: <code>*constant*</code> and using <code>define</code>.
  321. </p>
  322. </div>
  323. </div>
  324. <div id="outline-container-orgfed180c" class="outline-4">
  325. <h4 id="orgfed180c"><span class="section-number-4">1.2.5</span> case</h4>
  326. <div class="outline-text-4" id="text-1-2-5">
  327. <p>
  328. ???
  329. </p>
  330. </div>
  331. </div>
  332. </div>
  333. </div>
  334. </div>
  335. <div id="postamble" class="status">
  336. <p class="author">Author: Zelphir Kaltstahl</p>
  337. <p class="date">Created: 2020-05-03 So 14:06</p>
  338. <p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
  339. </div>
  340. </body>
  341. </html>