<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Adztec Independent - Blog &#187; tagbody</title>
	<atom:link href="http://www.adztec-independent.de/tag/tagbody/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adztec-independent.de</link>
	<description>Programming Ruby / JRuby / Rails / Common Lisp by Christopher Bertels</description>
	<lastBuildDate>Mon, 13 Sep 2010 22:58:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Lisp macros revisited</title>
		<link>http://www.adztec-independent.de/2008/12/lisp-macros-revisited/</link>
		<comments>http://www.adztec-independent.de/2008/12/lisp-macros-revisited/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 10:56:14 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[common lisp]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[special operator]]></category>
		<category><![CDATA[tagbody]]></category>

		<guid isPermaLink="false">http://www.adztec-independent.de/?p=90</guid>
		<description><![CDATA[Since my first posts on my own little control macros (while, foreach etc.) I&#8217;ve learned quite a bit and so here are my revised versions of some of them, since not all of them would work outside of an interpreter, since they were recursive macros.
A better, and also non recursive way to write macros is [...]]]></description>
			<content:encoded><![CDATA[<p>Since my first posts on my own little control macros (while, foreach etc.) I&#8217;ve learned quite a bit and so here are my revised versions of some of them, since not all of them would work outside of an interpreter, since they were recursive macros.<br />
A better, and also non recursive way to write macros is to either use exisiting macros (as done with my for macro) or for example by using the tagbody &#038; go constructs of lisp (similar to a goto in C):</p>
<p><strong>new while macro</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p90code4'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p904"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p90code4"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; while macro with tagbody &amp; go.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> while-new <span style="color: #66cc66;">&#40;</span>condition <span style="color: #66cc66;">&amp;</span>body body<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>tag-<span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    `<span style="color: #66cc66;">&#40;</span>tagbody
      <span style="color: #66cc66;">,</span>tag-<span style="color: #b1b100;">name</span>
      <span style="color: #66cc66;">,</span>@body
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">,</span>condition
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">go</span> <span style="color: #66cc66;">,</span>tag-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><em>Thanks again to the anonymous commenter &#8216;foo&#8217;, who made it clear, that the previous version (without gensymed-symbols) could make trouble in the case of a caller having symbols defined with the same name (in this case tag-name).</p>
<p>I also changed the foreach-macro to use gensymed symbols:</em></p>
<p><strong>new foreach macro</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p90code5'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p905"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p90code5"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; foreach macro with tagbody &amp; go.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> foreach-new <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>var <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>body body<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>tmp-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>start-tag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>tmp-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">,</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>var <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">,</span>tmp-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>tagbody
	  <span style="color: #66cc66;">,</span>start-tag
	  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> <span style="color: #66cc66;">,</span>tmp-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #b1b100;">nil</span>
	      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
		<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">,</span>var <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">,</span>tmp-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">,</span>tmp-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">,</span>var
		  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span> <span style="color: #66cc66;">,</span>@body
			 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">,</span>tmp-<span style="color: #b1b100;">list</span> rest<span style="color: #66cc66;">&#41;</span>
			 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">go</span> <span style="color: #66cc66;">,</span>start-tag<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		  <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><strong>new until macro</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p90code6'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p906"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p90code6"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; until macro reusing new while macro.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> until-new <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&amp;</span>body body<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span>while-new <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">,</span><span style="color: #b1b100;">cond</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">,</span>@body<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Also, thanks to &#8216;foo&#8217;, who pointed this out in some comments.<br />
I&#8217;ve known about this fact for little while already, but haven&#8217;t thought about posting it here. Since some people might be reading this and wondering, why they don&#8217;t seem to work, they now should have some working samples. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.adztec-independent.de/2008/12/lisp-macros-revisited/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

