{"id":127,"date":"2007-10-24T02:38:07","date_gmt":"2007-10-24T01:38:07","guid":{"rendered":"http:\/\/people.iola.dk\/arj\/2007\/10\/24\/the-power-of-lisp-part-2\/"},"modified":"2008-03-05T02:14:37","modified_gmt":"2008-03-05T01:14:37","slug":"the-power-of-lisp-part-2","status":"publish","type":"post","link":"https:\/\/people.iola.dk\/arj\/2007\/10\/24\/the-power-of-lisp-part-2\/","title":{"rendered":"The power of lisp part 2"},"content":{"rendered":"<p>As mentioned earlier, lisp is incredible at abstracting away small everyday stuff you write over and over. A good example is this way one has to define a local variable in order to make variables sucked into a delegates from the surroundings static in C#. As mentioned briefly in <a href=\"http:\/\/people.iola.dk\/arj\/2007\/10\/24\/the-power-of-lisp-part-1\/\">this blog post<\/a>, one needs to change the loop to read like the following code for it to print 0 to 9 instead of 10 times 9:<\/p>\n<pre class=\"prettyprint\">foreach (int i in numbers) {\r\n\r\n        int j = i;\r\n\r\n        l.Add(delegate() { System.Console.WriteLine(j); };\r\n}<\/pre>\n<p>In lisp one would see that this is a pattern and simply abstract it away like <a href=\"http:\/\/people.iola.dk\/anders\/example.lisp\">this<\/a>. As one can see, the macro static-loop is more or less exactly the same as the pattern which luckily made it quite easy to write.<\/p>\n<pre class=\"prettyprint\">\r\n(defmacro static-loop ((i container) &amp;body body)\r\n (with-gensyms (j)\r\n  `(loop for ,j in ,container do\r\n    (let ((,i ,j))\r\n     ,@body))))<\/pre>\n<p>The only small trick is the with-gensyms macro which prevents <a href=\"http:\/\/www.joelonsoftware.com\/articles\/LeakyAbstractions.html\">leaking<\/a> the temporary variable j into the scope of where it will be substituted into. So in that regard the macro is even nicer than rolling your own temp variable hack \ud83d\ude42<\/p>\n<p>For reference the <a href=\"http:\/\/people.iola.dk\/anders\/example.py.txt\">python example<\/a> works the same way, but allows one to use the lambda function to redefine variables, although one must note that the static-loop solution is more general in that it will make the loop variable local for the whole scope and not just for one lambda function. Funny thing is that I had originally written it like <a href=\"http:\/\/people.iola.dk\/anders\/example-faulty.py.txt\">this<\/a>, which actually worked, although that means that the closure binding is not only by reference but by name.<\/p>\n<p>I tried writing the macro in boo but I failed. Apparently their macro support is still quite new and there&#8217;s close to no examples to draw inspiration from. If anyone could help me out with the solution please mail it to me or even better add it as a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned earlier, lisp is incredible at abstracting away small everyday stuff you write over and over. A good example is this way one has to define a local variable in order to make variables sucked into a delegates from the surroundings static in C#. As mentioned briefly in this blog post, one needs to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,26,30,19,34],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-boo","category-c","category-lisp","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":0,"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"wp:attachment":[{"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/people.iola.dk\/arj\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}