25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

540 satır
30 KiB

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <!-- 2018-10-14 So 16:55 -->
  6. <meta charset="utf-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <meta name="generator" content="Org-mode">
  9. <meta name="author" content="Felix Brendel">
  10. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
  11. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  12. <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
  13. <style type="text/css">
  14. /* org mode styles on top of twbs */
  15. html {
  16. position: relative;
  17. min-height: 100%;
  18. }
  19. body {
  20. font-size: 18px;
  21. margin-bottom: 105px;
  22. }
  23. footer {
  24. position: absolute;
  25. bottom: 0;
  26. width: 100%;
  27. height: 101px;
  28. background-color: #f5f5f5;
  29. }
  30. footer > div {
  31. padding: 10px;
  32. }
  33. footer p {
  34. margin: 0 0 5px;
  35. text-align: center;
  36. font-size: 16px;
  37. }
  38. #table-of-contents {
  39. margin-top: 20px;
  40. margin-bottom: 20px;
  41. }
  42. blockquote p {
  43. font-size: 18px;
  44. }
  45. pre {
  46. font-size: 16px;
  47. }
  48. .footpara {
  49. display: inline-block;
  50. }
  51. figcaption {
  52. font-size: 16px;
  53. color: #666;
  54. font-style: italic;
  55. padding-bottom: 15px;
  56. }
  57. /* from twbs docs */
  58. .bs-docs-sidebar.affix {
  59. position: static;
  60. }
  61. @media (min-width: 768px) {
  62. .bs-docs-sidebar {
  63. padding-left: 20px;
  64. }
  65. }
  66. /* All levels of nav */
  67. .bs-docs-sidebar .nav > li > a {
  68. display: block;
  69. padding: 4px 20px;
  70. font-size: 14px;
  71. font-weight: 500;
  72. color: #999;
  73. }
  74. .bs-docs-sidebar .nav > li > a:hover,
  75. .bs-docs-sidebar .nav > li > a:focus {
  76. padding-left: 19px;
  77. color: #A1283B;
  78. text-decoration: none;
  79. background-color: transparent;
  80. border-left: 1px solid #A1283B;
  81. }
  82. .bs-docs-sidebar .nav > .active > a,
  83. .bs-docs-sidebar .nav > .active:hover > a,
  84. .bs-docs-sidebar .nav > .active:focus > a {
  85. padding-left: 18px;
  86. font-weight: bold;
  87. color: #A1283B;
  88. background-color: transparent;
  89. border-left: 2px solid #A1283B;
  90. }
  91. /* Nav: second level (shown on .active) */
  92. .bs-docs-sidebar .nav .nav {
  93. display: none; /* Hide by default, but at >768px, show it */
  94. padding-bottom: 10px;
  95. }
  96. .bs-docs-sidebar .nav .nav > li > a {
  97. padding-top: 1px;
  98. padding-bottom: 1px;
  99. padding-left: 30px;
  100. font-size: 12px;
  101. font-weight: normal;
  102. }
  103. .bs-docs-sidebar .nav .nav > li > a:hover,
  104. .bs-docs-sidebar .nav .nav > li > a:focus {
  105. padding-left: 29px;
  106. }
  107. .bs-docs-sidebar .nav .nav > .active > a,
  108. .bs-docs-sidebar .nav .nav > .active:hover > a,
  109. .bs-docs-sidebar .nav .nav > .active:focus > a {
  110. padding-left: 28px;
  111. font-weight: 500;
  112. }
  113. /* Nav: third level (shown on .active) */
  114. .bs-docs-sidebar .nav .nav .nav {
  115. padding-bottom: 10px;
  116. }
  117. .bs-docs-sidebar .nav .nav .nav > li > a {
  118. padding-top: 1px;
  119. padding-bottom: 1px;
  120. padding-left: 40px;
  121. font-size: 12px;
  122. font-weight: normal;
  123. }
  124. .bs-docs-sidebar .nav .nav .nav > li > a:hover,
  125. .bs-docs-sidebar .nav .nav .nav > li > a:focus {
  126. padding-left: 39px;
  127. }
  128. .bs-docs-sidebar .nav .nav .nav > .active > a,
  129. .bs-docs-sidebar .nav .nav .nav > .active:hover > a,
  130. .bs-docs-sidebar .nav .nav .nav > .active:focus > a {
  131. padding-left: 38px;
  132. font-weight: 500;
  133. }
  134. /* Show and affix the side nav when space allows it */
  135. @media (min-width: 992px) {
  136. .bs-docs-sidebar .nav > .active > ul {
  137. display: block;
  138. }
  139. /* Widen the fixed sidebar */
  140. .bs-docs-sidebar.affix,
  141. .bs-docs-sidebar.affix-bottom {
  142. width: 213px;
  143. }
  144. .bs-docs-sidebar.affix {
  145. position: fixed; /* Undo the static from mobile first approach */
  146. top: 20px;
  147. }
  148. .bs-docs-sidebar.affix-bottom {
  149. position: absolute; /* Undo the static from mobile first approach */
  150. }
  151. .bs-docs-sidebar.affix .bs-docs-sidenav,.bs-docs-sidebar.affix-bottom .bs-docs-sidenav {
  152. margin-top: 0;
  153. margin-bottom: 0
  154. }
  155. }
  156. @media (min-width: 1200px) {
  157. /* Widen the fixed sidebar again */
  158. .bs-docs-sidebar.affix-bottom,
  159. .bs-docs-sidebar.affix {
  160. width: 263px;
  161. }
  162. }
  163. </style>
  164. <script type="text/javascript">
  165. $(function() {
  166. 'use strict';
  167. $('.bs-docs-sidebar li').first().addClass('active');
  168. $(document.body).scrollspy({target: '.bs-docs-sidebar'});
  169. $('.bs-docs-sidebar').affix();
  170. });
  171. </script>
  172. </head>
  173. <body>
  174. <div id="content" class="container">
  175. <div class="row"><div class="col-md-9"><h1 class="title"></h1>
  176. <div id="outline-container-sec-1" class="outline-2">
  177. <h2 id="sec-1"><span class="section-number-2">1</span> Arguments</h2>
  178. <div class="outline-text-2" id="text-1">
  179. <p>
  180. In Emacs lisp, keyword arguments must be passed in the same order as they were
  181. defined in, so <code>((lambda (a &amp;key b c) (+ a b c)) 1 3 :b 2)</code> will preduce an
  182. error, because <code>b</code> was passed as last argument but was defined as second argument.
  183. </p>
  184. <div class="org-src-container">
  185. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">3</span>
  186. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 <span style="color: #6c71c4;">:b</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">3</span>
  187. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:b</span> 2 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error</span>
  188. </pre>
  189. </div>
  190. <p>
  191. But if mulitiple keyword arguments are defined, their ordering does not matter.
  192. </p>
  193. <div class="org-src-container">
  194. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b <span style="color: #268bd2;">&amp;key</span> c <span style="color: #268bd2;">&amp;key</span> d<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 <span style="color: #6c71c4;">:d</span> 4 <span style="color: #6c71c4;">:c</span> 3 <span style="color: #6c71c4;">:b</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">10</span>
  195. </pre>
  196. </div>
  197. <p>
  198. It is even possible to have a non keyword argument bewtween keyword arguments
  199. and when calling, switch the order.
  200. </p>
  201. <div class="org-src-container">
  202. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b c <span style="color: #268bd2;">&amp;key</span> d<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 <span style="color: #6c71c4;">:d</span> 4 3 <span style="color: #6c71c4;">:b</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">10</span>
  203. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b c <span style="color: #268bd2;">&amp;key</span> d<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 <span style="color: #6c71c4;">:d</span> 4 <span style="color: #6c71c4;">:b</span> 2 3<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error</span>
  204. </pre>
  205. </div>
  206. <p>
  207. However somehow it is not possible to flip the ordering at the first argument
  208. </p>
  209. <div class="org-src-container">
  210. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 2 <span style="color: #6c71c4;">:b</span> 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">3</span>
  211. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a <span style="color: #268bd2;">&amp;key</span> b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:b</span> 2 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error</span>
  212. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span><span style="color: #268bd2;">&amp;key</span> a b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:a</span> 2 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">3</span>
  213. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span><span style="color: #268bd2;">&amp;key</span> a b<span style="color: #b58900;">)</span> <span style="color: #b58900;">(</span>+ a b<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 <span style="color: #6c71c4;">:a</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error</span>
  214. </pre>
  215. </div>
  216. <p>
  217. So it seems, positional arguments have to be at the exact position they were
  218. defined in, but keyword arguments can appear in any order, but at the places
  219. where keyword arguments have been defined.
  220. </p>
  221. </div>
  222. <div id="outline-container-sec-1-1" class="outline-3">
  223. <h3 id="sec-1-1"><span class="section-number-3">1.1</span> Idea</h3>
  224. <div class="outline-text-3" id="text-1-1">
  225. <p>
  226. Every argument can be set using keys, but keyword arguments always come strictly
  227. after positional arguments
  228. </p>
  229. <div class="org-src-container">
  230. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2 3 4<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">ok</span>
  231. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2 <span style="color: #6c71c4;">:c</span> 2 <span style="color: #6c71c4;">:d</span> 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">ok</span>
  232. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2 <span style="color: #6c71c4;">:d</span> 1 <span style="color: #6c71c4;">:c</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">ok</span>
  233. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:d</span> 1 1 2 <span style="color: #6c71c4;">:c</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error, keyword arguments must come last</span>
  234. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2 <span style="color: #6c71c4;">:c</span> 2 <span style="color: #6c71c4;">:d</span> 1 <span style="color: #6c71c4;">:a</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error, 'a' was passed two times</span>
  235. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2 <span style="color: #6c71c4;">:d</span> 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error, c was not passed, but does not have a default value</span>
  236. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b c <span style="color: #6c71c4;">:default</span> 10 d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> 1 2 <span style="color: #6c71c4;">:d</span> 1<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">okay again</span>
  237. </pre>
  238. </div>
  239. <p>
  240. The drawback then is that you can never pass a keyword as a positional argument,
  241. because it will always try to assign the next argument to the variable defined
  242. by the keyword. You could still pass it as a keyword argument though. This is
  243. especially annoying because we want to use keywords as type identifiers.
  244. </p>
  245. <div class="org-src-container">
  246. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span>= <span style="color: #2aa198;">(</span>type <span style="color: #2aa198;">"hello"</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:string</span><span style="color: #839496;">)</span>
  247. </pre>
  248. </div>
  249. <div class="org-src-container">
  250. <pre class="src src-emacs-lisp"><span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>i-wanna-get-a-kw<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:hey</span><span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error, unmatched value for keyword argument</span>
  251. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>i-wanna-get-a-kw<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:i-wanna-get-a-kw</span> <span style="color: #6c71c4;">:hey</span><span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">this would work again</span>
  252. </pre>
  253. </div>
  254. <p>
  255. Other Idea is to mark the place in the parameter list where keyword argumetns
  256. start and arguments before that are positional and will not be stuffed into
  257. key-value pairs and arguments after that will be keyword arguments
  258. </p>
  259. <div class="org-src-container">
  260. <pre class="src src-ebnf">&lt;lambda list&gt; -&gt; &lt;positional argument&gt;*
  261. [:keys ( &lt;keyword arguments&gt; [:defaults-to &lt;value&gt;])*]
  262. [(:rest &lt;rest argument&gt;)|(:body &lt;body argument&gt;)]
  263. </pre>
  264. </div>
  265. <div class="org-src-container">
  266. <pre class="src src-emacs-lisp"> <span style="color: #839496;">(</span>= <span style="color: #2aa198;">(</span>type <span style="color: #2aa198;">"hello"</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:string</span><span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">this will work again because (=) does</span>
  267. <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">not use keyword arguments</span>
  268. <span style="color: #839496;">(</span><span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>i-wanna-get-a-kw<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span> <span style="color: #6c71c4;">:hey</span><span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">would work because of the same reason</span>
  269. <span style="color: #839496;">(</span>define fun
  270. <span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span><span style="color: #6c71c4;">:keys</span> a b c d<span style="color: #b58900;">)</span>
  271. <span style="color: #b58900;">(</span>+ a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span><span style="color: #839496;">)</span>
  272. <span style="color: #839496;">(</span>fun 1 2 3 4<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">okay</span>
  273. <span style="color: #839496;">(</span>fun 1 2 <span style="color: #6c71c4;">:d</span> 3 <span style="color: #6c71c4;">:c</span> 4<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">okay</span>
  274. <span style="color: #839496;">(</span>fun 1 2 <span style="color: #6c71c4;">:d</span> 3 <span style="color: #6c71c4;">:c</span> 4 <span style="color: #6c71c4;">:a</span> 2<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error, 'a' was passed two times</span>
  275. <span style="color: #839496;">(</span>fun <span style="color: #6c71c4;">:c</span> 1 <span style="color: #6c71c4;">:b</span> 2 <span style="color: #6c71c4;">:d</span> 3 <span style="color: #6c71c4;">:a</span> 4<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">okay</span>
  276. <span style="color: #839496;">(</span>fun <span style="color: #6c71c4;">:c</span> 1 <span style="color: #6c71c4;">:b</span> 2 <span style="color: #6c71c4;">:d</span> 3 4<span style="color: #839496;">)</span> <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">error, positional argument after keyword</span>
  277. <span style="color: #49974A; font-style: italic;">;; </span><span style="color: #49974A; font-style: italic;">and fun does not accept rest parameter</span>
  278. <span style="color: #839496;">(</span>define fun2
  279. <span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span><span style="color: #6c71c4;">:quoted</span> op a b <span style="color: #6c71c4;">:keys</span> c d <span style="color: #6c71c4;">:rest</span> r<span style="color: #b58900;">)</span>
  280. <span style="color: #b58900;">(</span>print r<span style="color: #b58900;">)</span>
  281. <span style="color: #b58900;">(</span>op a b c d<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span><span style="color: #839496;">)</span>
  282. &gt;&gt; <span style="color: #839496;">(</span>fun2 * 1 2 <span style="color: #6c71c4;">:c</span> 3 <span style="color: #6c71c4;">:d</span> 4 <span style="color: #2aa198;">"this"</span> <span style="color: #2aa198;">"can"</span> <span style="color: #6c71c4;">:be</span> <span style="color: #2aa198;">"whatever"</span><span style="color: #839496;">)</span>
  283. <span style="color: #839496;">(</span><span style="color: #2aa198;">"this"</span> <span style="color: #2aa198;">"can"</span> <span style="color: #6c71c4;">:be</span> <span style="color: #2aa198;">"whatever"</span><span style="color: #839496;">)</span>
  284. 24
  285. &gt;&gt; <span style="color: #839496;">(</span>define print-before-eval <span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span><span style="color: #6c71c4;">:body</span> expr<span style="color: #b58900;">)</span>
  286. <span style="color: #b58900;">(</span>print expr<span style="color: #b58900;">)</span>
  287. <span style="color: #b58900;">(</span>eval expr<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span><span style="color: #839496;">)</span>
  288. &gt;&gt; <span style="color: #839496;">(</span>define r <span style="color: #2aa198;">(</span>print-before-eval <span style="color: #b58900;">(</span>+ 1 2 3<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span><span style="color: #839496;">)</span>
  289. <span style="color: #839496;">(</span>+ 1 2 3<span style="color: #839496;">)</span>
  290. &gt;&gt; r
  291. 6
  292. </pre>
  293. </div>
  294. <p>
  295. Wait this does not work either because you won't be able to pass keywords into
  296. rest.
  297. </p>
  298. <p>
  299. <b>Unrealated Idea: Distinguish between functions and macros</b>
  300. </p>
  301. <p>
  302. The difference is really simple:
  303. </p>
  304. <dl class="org-dl">
  305. <dt> Function </dt><dd>like a lambda, but no argument is automatically quoted
  306. </dd>
  307. <dt> Macro </dt><dd>like a lambda, but every argument is automatically quoted
  308. </dd>
  309. </dl>
  310. <p>
  311. This is important, because a macro should not introduce a new environment, but
  312. use the one it lives in.
  313. </p>
  314. <div class="org-src-container">
  315. <pre class="src src-emacs-lisp">&gt;&gt; <span style="color: #839496;">(</span>define print-before-eval <span style="color: #2aa198;">(</span>macro <span style="color: #b58900;">(</span><span style="color: #6c71c4;">:rest</span> expr<span style="color: #b58900;">)</span>
  316. <span style="color: #b58900;">(</span>print expr<span style="color: #b58900;">)</span>
  317. <span style="color: #b58900;">(</span>eval expr<span style="color: #b58900;">)</span><span style="color: #2aa198;">)</span>
  318. </pre>
  319. </div>
  320. </div>
  321. </div>
  322. <div id="outline-container-sec-1-2" class="outline-3">
  323. <h3 id="sec-1-2"><span class="section-number-3">1.2</span> the actual problem</h3>
  324. <div class="outline-text-3" id="text-1-2">
  325. <p>
  326. We have some requirements to the arguments:
  327. </p>
  328. <ul class="org-ul">
  329. <li>positional arguments (always required, no default value)
  330. </li>
  331. <li>keyword arguments (always come after positionals), can be not passed if there
  332. is a default value
  333. </li>
  334. <li>We want to be able to have a rest paramter, where everything is dumped in to
  335. that exceeds the other parameters
  336. </li>
  337. </ul>
  338. <p>
  339. <b>The problem is, we have no way of knowing if the current keyword we are
  340. reading is still part of the keyword block or part of the rest paramter.</b>
  341. </p>
  342. <p>
  343. Consider:
  344. </p>
  345. <div class="org-src-container">
  346. <pre class="src src-emacs-lisp">&gt;&gt; <span style="color: #839496;">(</span>define fun <span style="color: #2aa198;">(</span><span style="color: #859900;">lambda</span> <span style="color: #b58900;">(</span>a b <span style="color: #6c71c4;">:key</span> c d <span style="color: #6c71c4;">:defaults-to</span> 4 <span style="color: #6c71c4;">:rest</span><span style="color: #b58900;">)</span> nil<span style="color: #2aa198;">)</span><span style="color: #839496;">)</span>
  347. &gt;&gt; <span style="color: #839496;">(</span>fun 1 2 <span style="color: #6c71c4;">:c</span> 3 <span style="color: #6c71c4;">:d</span> 5<span style="color: #839496;">)</span>
  348. </pre>
  349. </div>
  350. <p>
  351. What should this be evaluated to?
  352. </p>
  353. <table class="table table-striped table-bordered table-hover table-condensed">
  354. <colgroup>
  355. <col class="left">
  356. <col class="right">
  357. <col class="right">
  358. <col class="right">
  359. <col class="right">
  360. <col class="left">
  361. </colgroup>
  362. <thead>
  363. <tr>
  364. <th scope="col" class="text-left">&#xa0;</th>
  365. <th scope="col" class="text-right">a</th>
  366. <th scope="col" class="text-right">b</th>
  367. <th scope="col" class="text-right">c</th>
  368. <th scope="col" class="text-right">d</th>
  369. <th scope="col" class="text-left">rest</th>
  370. </tr>
  371. </thead>
  372. <tbody>
  373. <tr>
  374. <td class="text-left">v1</td>
  375. <td class="text-right">1</td>
  376. <td class="text-right">2</td>
  377. <td class="text-right">3</td>
  378. <td class="text-right">5</td>
  379. <td class="text-left">nil</td>
  380. </tr>
  381. <tr>
  382. <td class="text-left">v2</td>
  383. <td class="text-right">1</td>
  384. <td class="text-right">2</td>
  385. <td class="text-right">3</td>
  386. <td class="text-right">4</td>
  387. <td class="text-left">(:d 5)</td>
  388. </tr>
  389. </tbody>
  390. </table>
  391. <p>
  392. It is ambiguous. It is however important to note that this can only happen when
  393. there are optinal keyword arguments (keyword arguments with default values).
  394. Otherwise we could always know which variables are set and wich have not been
  395. set to see if we are in an errornious state.
  396. </p>
  397. </div>
  398. </div>
  399. <div id="outline-container-sec-1-3" class="outline-3">
  400. <h3 id="sec-1-3"><span class="section-number-3">1.3</span> Solution</h3>
  401. <div class="outline-text-3" id="text-1-3">
  402. <p>
  403. We can resolve that problem by always making the the decision of chosing <code>v1</code>
  404. between <code>v1</code> and <code>v1</code>. This means, when we are expecting to read kwargs, and we
  405. encounter an kwarg, try to apply it (this can fail, for two reasons:
  406. </p>
  407. <ol class="org-ol">
  408. <li>The keyword has no following value
  409. </li>
  410. <li>The keyword is not a keyword argument in the funciton
  411. </li>
  412. </ol>
  413. <p>
  414. ), if it fails treat it as part of the rest arguments, and read all the
  415. following arguments into the rest arguments, or if it works, use it as the
  416. keyword argument.
  417. </p>
  418. </div>
  419. </div>
  420. <div id="outline-container-sec-1-4" class="outline-3">
  421. <h3 id="sec-1-4"><span class="section-number-3">1.4</span> Implementation</h3>
  422. <div class="outline-text-3" id="text-1-4">
  423. <div class="org-src-container">
  424. <pre class="src src-c"><span style="color: #859900;">typedef</span> <span style="color: #859900;">struct</span> <span style="color: #839496;">{</span>
  425. <span style="color: #268bd2;">char</span>* <span style="color: #6c71c4;">docstring</span>;
  426. <span style="color: #268bd2;">char</span>** <span style="color: #6c71c4;">positional_arguments</span>;
  427. <span style="color: #859900;">struct</span> <span style="color: #2aa198;">{</span>
  428. <span style="color: #268bd2;">char</span>* <span style="color: #6c71c4;">identifier</span>;
  429. <span style="color: #268bd2;">Ast_node</span>* <span style="color: #6c71c4;">default_value</span>; <span style="color: #49974A; font-style: italic;">// </span><span style="color: #49974A; font-style: italic;">will be nullptr if no defalut value was declared</span>
  430. <span style="color: #2aa198;">}</span>* <span style="color: #6c71c4;">keyword_arguments</span>;
  431. <span style="color: #268bd2;">char</span>* <span style="color: #6c71c4;">rest_argument</span>; <span style="color: #49974A; font-style: italic;">// </span><span style="color: #49974A; font-style: italic;">will be nullptr if no rest argument is declared</span>
  432. <span style="color: #268bd2;">Ast_Node_array_list</span> <span style="color: #6c71c4;">body</span>;
  433. <span style="color: #839496;">}</span> <span style="color: #268bd2;">lambda</span>;
  434. </pre>
  435. </div>
  436. </div>
  437. </div>
  438. </div>
  439. </div><div class="col-md-3"><nav id="table-of-contents">
  440. <div id="text-table-of-contents" class="bs-docs-sidebar">
  441. <ul class="nav">
  442. <li><a href="#sec-1">1. Arguments</a>
  443. <ul class="nav">
  444. <li><a href="#sec-1-1">1.1. Idea</a></li>
  445. <li><a href="#sec-1-2">1.2. the actual problem</a></li>
  446. <li><a href="#sec-1-3">1.3. Solution</a></li>
  447. <li><a href="#sec-1-4">1.4. Implementation</a></li>
  448. </ul>
  449. </li>
  450. </ul>
  451. </div>
  452. </nav>
  453. </div></div></div>
  454. <footer id="postamble" class="">
  455. <div><p class="author">Author: Felix Brendel</p>
  456. <p class="date">Created: 2018-10-14 So 16:55</p>
  457. <p class="creator"><a href="http://www.gnu.org/software/emacs/">Emacs</a> 26.0.91 (<a href="http://orgmode.org">Org-mode</a> 9.1.13)</p>
  458. </div>
  459. </footer>
  460. </body>
  461. </html>