<?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; lisp</title>
	<atom:link href="http://www.adztec-independent.de/tag/lisp/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>Clojure</title>
		<link>http://www.adztec-independent.de/2009/01/clojure/</link>
		<comments>http://www.adztec-independent.de/2009/01/clojure/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 00:56:55 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[free software]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://www.adztec-independent.de/?p=123</guid>
		<description><![CDATA[




I&#8217;ve been trying out Clojure lately. Clojure is a new lisp dialect, that is hosted on the JVM and interoperates nicely with it. You can easily use Java methods, classes and libraries. I&#8217;ve been using it a little bit, watching some videos about it on blip.tv by it&#8217;s author, Rich Hickey, which really is a [...]]]></description>
			<content:encoded><![CDATA[<div class="mceTemp">
<dl class="wp-caption alignleft" style="width: 110px;">
<dt class="wp-caption-dt"><a href="http://www.clojure.org"><img title="Clojure logo" src="http://clojure.org/file/view/clojure-icon.gif" alt="Clojure logo" width="100" height="100" /></a></dt>
</dl>
</div>
<p>I&#8217;ve been trying out <a title="Clojure Website" href="http://www.clojure.org" target="_blank">Clojure</a> lately. Clojure is a new lisp dialect, that is hosted on the JVM and interoperates nicely with it. You can easily use Java methods, classes and libraries. I&#8217;ve been using it a little bit, watching some <a title="Clojure Channel on blip.tv" href="http://clojure.blip.tv" target="_blank">videos about it on blip.tv</a> by it&#8217;s author, Rich Hickey, which really is a brilliant guy. You should check them out as well, they give you a pretty good introduction to the language.</p>
<p>Compared to Common Lisp, which I really like allot as well, Clojure does feel a little bit more modern. For example, being not limited to reader support for lists, but also having maps and vectors (like arrays) as first class citizens, is really nice. And then theres the huge emphasis on multithreading support in a really nice way without locks and condition variables but with a stm (software transactional memory). Clojure is a functional language and focuses on immutability of data structures compared to Common Lisp, which also allows a more imperative style, if you want to. In Clojure, you&#8217;re forced to a functional style, which might seem like a downside at first, but which makes it possible to have some pretty neat features concerning multithreaded programming support. Since all the datastructures are immutable, there&#8217;s no need do use locks when having multiple threads sharing them. I suggest checking out the videos and to give Clojure a try. It seems very promising and it&#8217;s having quite a publicity boost right now. Just google for it, take a look on some mailinglists or checkout reddit etc. You&#8217;ll find plenty comments, links and blogposts about it (like this one <img src='http://www.adztec-independent.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ).</p>
<p>So since I thought it&#8217;s always nice to try to port an existing little application from a language I already know to a new one I want to learn, I tried to port a very small programm <a title="CallToPower's website" href="http://www.calltopower.de" target="_blank">CallToPower</a>, a collegue from <a title="University of Osnabrück" href="http://www.uos.de" target="_blank">university</a>, wrote and <a title="Sysinfo.jar" href="http://calltopower.ca.funpic.de/stuff/blog/software/java/SysInfo.jar" target="_blank">posted on his website</a>. I pretty much copied it straightly to Clojure, changing some parts.</p>
<p>Now I know this might not be the best way to write it in Clojure. But it was my real first (although very small) programm in Clojure and I wanted to make it as close to the original, as possible. Basically, what it does is display some values of some system properties you can choose by pressing the appropriate. Nothing fancy, but it shows some Clojure related stuff pretty well.</p>
<p>So here&#8217;s the code, use it as you like:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p123code2'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1232"><td class="code" id="p123code2"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>ns sysinfo
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">gen-class</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">import</span> <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>awt BorderLayout
		     GridLayout<span style="color: #66cc66;">&#41;</span>
	   <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>awt<span style="color: #66cc66;">.</span>event ActionEvent
			   ActionListener<span style="color: #66cc66;">&#41;</span>
	   <span style="color: #66cc66;">&#40;</span>javax<span style="color: #66cc66;">.</span>swing JFrame
			JPanel
			JLabel
			JButton<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>def *x-coord* 30<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *y-coord* 30<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *width* 800<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *height* 230<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *prog-<span style="color: #b1b100;">name</span>* <span style="color: #ff0000;">&quot;SysInfo 1.0 [Clojure]&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *visible* true<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *resizable* true<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def *frame* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>def *label-property-mappings*
     <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">&quot;OS Name&quot;</span> <span style="color: #ff0000;">&quot;os.name&quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot;OS Architecture&quot;</span> <span style="color: #ff0000;">&quot;os.arch&quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot;Java Classpath&quot;</span> <span style="color: #ff0000;">&quot;java.class.path&quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot;Java Version&quot;</span> <span style="color: #ff0000;">&quot;java.version&quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot;Current working directory&quot;</span> <span style="color: #ff0000;">&quot;user.dir&quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot;Resize to default&quot;</span> <span style="color: #ff0000;">&quot;Resize to default&quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot;Clear&quot;</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn resize-frame
  <span style="color: #66cc66;">&#91;</span>frame a b awidth bheight<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>doto frame
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setBounds a b awidth bheight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn set-label-text <span style="color: #66cc66;">&#91;</span>label text<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>doto label
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setText text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn button-listener
  <span style="color: #ff0000;">&quot;Returns a ActionListener, which sets the text of the content label (label-content)
  to the appropriate system property (System.getProperty).&quot;</span>
  <span style="color: #66cc66;">&#91;</span>property label frame<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>proxy <span style="color: #66cc66;">&#91;</span>ActionListener<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>actionPerformed <span style="color: #66cc66;">&#91;</span>evt<span style="color: #66cc66;">&#93;</span>
		     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>system-property-<span style="color: #b1b100;">value</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> System getProperty property<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>equalsIgnoreCase property <span style="color: #ff0000;">&quot;Resize to default&quot;</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;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>equalsIgnoreCase property <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			   <span style="color: #66cc66;">&#40;</span>set-label-text label <span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getActionCommand evt<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;: <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> system-property-<span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			   <span style="color: #66cc66;">&#40;</span>set-label-text label property<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			 <span style="color: #66cc66;">&#40;</span>resize-frame frame *x-coord* *y-coord* *width* *height*<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>
&nbsp;
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> doto-each
  <span style="color: #ff0000;">&quot;Macro, that works like doto-macro, only that it does it for each object in objects.
  varname is bound to each object in turn inside of the body (forms) and can be used to access the current
  object, if needed.&quot;</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>varname <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&amp;</span> objects<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&amp;</span> forms<span style="color: #66cc66;">&#93;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>
     ~@<span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>f<span style="color: #66cc66;">&#93;</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>~varname ~f<span style="color: #66cc66;">&#93;</span>
		       <span style="color: #66cc66;">&#40;</span>doto ~f
			 ~@forms<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	    objects<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> add-each
  <span style="color: #ff0000;">&quot;Macro, that calls .add on to-object with each object in objects.&quot;</span>
  <span style="color: #66cc66;">&#91;</span>to-object <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&amp;</span> objects<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>
     ~@<span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>o<span style="color: #66cc66;">&#93;</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>add ~to-object ~o<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	    objects<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn create-new-sysinfo-window
  <span style="color: #ff0000;">&quot;Creates the main form and returns it. Mainly GUI-related stuff (javax.swing).&quot;</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>frame <span style="color: #66cc66;">&#40;</span>JFrame<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Adztec-Independent.de: SysInfo Clojure-style!&quot;</span><span style="color: #66cc66;">&#41;</span>
	panel-header <span style="color: #66cc66;">&#40;</span>JPanel<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#41;</span>
	panel-main <span style="color: #66cc66;">&#40;</span>JPanel<span style="color: #66cc66;">.</span> <span style="color: #66cc66;">&#40;</span>BorderLayout<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	panel-left <span style="color: #66cc66;">&#40;</span>JPanel<span style="color: #66cc66;">.</span> <span style="color: #66cc66;">&#40;</span>GridLayout<span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	panel-south <span style="color: #66cc66;">&#40;</span>JPanel<span style="color: #66cc66;">.</span> <span style="color: #66cc66;">&#40;</span>GridLayout<span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #808080; font-style: italic;">;; labels</span>
	label-header <span style="color: #66cc66;">&#40;</span>JLabel<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;System Information&quot;</span><span style="color: #66cc66;">&#41;</span>
	label-blank <span style="color: #66cc66;">&#40;</span>JLabel<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
	label-content <span style="color: #66cc66;">&#40;</span>JLabel<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
	label-adztec-indep <span style="color: #66cc66;">&#40;</span>JLabel<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;by www.adztec-independent.de&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setHorizontalAlignment label-blank <span style="color: #66cc66;">&#40;</span>JLabel/CENTER<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setHorizontalAlignment label-adztec-indep <span style="color: #66cc66;">&#40;</span>JLabel/CENTER<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">;; buttons</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>btn-java-cp <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Java Classpath&quot;</span><span style="color: #66cc66;">&#41;</span>
	  btn-java-version <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Java Version&quot;</span><span style="color: #66cc66;">&#41;</span>
	  btn-os-<span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;OS Name&quot;</span><span style="color: #66cc66;">&#41;</span>
	  btn-os-arch <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;OS Architecture&quot;</span><span style="color: #66cc66;">&#41;</span>
	  btn-current-working-dir <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Current working directory&quot;</span><span style="color: #66cc66;">&#41;</span>
	  btn-resize-to-default <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Resize to default&quot;</span><span style="color: #66cc66;">&#41;</span>
	  btn-clear-all <span style="color: #66cc66;">&#40;</span>JButton<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Clear&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>
	<span style="color: #808080; font-style: italic;">;; add button-listener to all buttons</span>
	<span style="color: #66cc66;">&#40;</span>doto-each  <span style="color: #66cc66;">&#91;</span>btn <span style="color: #66cc66;">&#91;</span>btn-java-cp
			  btn-java-version
			  btn-os-<span style="color: #b1b100;">name</span>
			  btn-os-arch
			  btn-current-working-dir
			  btn-resize-to-default
			  btn-clear-all<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
		      <span style="color: #808080; font-style: italic;">;; add listener to button</span>
		    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>addActionListener
		     <span style="color: #66cc66;">&#40;</span>button-listener <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">get</span> *label-property-mappings* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getText btn<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> label-content frame<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">;; add buttons to panel</span>
	<span style="color: #66cc66;">&#40;</span>add-each panel-left
		  <span style="color: #66cc66;">&#91;</span>btn-java-cp
		   btn-java-version
		   btn-os-<span style="color: #b1b100;">name</span>
		   btn-os-arch
		   btn-current-working-dir<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>add-each panel-south
		  <span style="color: #66cc66;">&#91;</span>btn-resize-to-default
		   btn-clear-all<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">;; add labels to panel</span>
	<span style="color: #66cc66;">&#40;</span>add-each panel-header
		  <span style="color: #66cc66;">&#91;</span>label-header
		   label-blank<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>add panel-south label-adztec-indep<span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">;; pack on frame</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>content-pane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getContentPane frame<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	  <span style="color: #66cc66;">&#40;</span>add-each content-pane
		    <span style="color: #66cc66;">&#91;</span>panel-main
		     label-content<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>doto content-pane
	    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>add BorderLayout/NORTH panel-header<span style="color: #66cc66;">&#41;</span>
	    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>add BorderLayout/WEST panel-left<span style="color: #66cc66;">&#41;</span>
	    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>add BorderLayout/SOUTH panel-south<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>doto frame
	  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>pack<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setTitle *prog-<span style="color: #b1b100;">name</span>*<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>resize-frame *x-coord* *y-coord* *width* *height*<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setVisible *visible*<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setResizable *resizable*<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setDefaultCloseOperation JFrame/HIDE_ON_CLOSE<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    frame<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; return frame</span>
&nbsp;
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn -main
  <span style="color: #ff0000;">&quot;Main function. Gets called by the system, if used as an executable .jar file.&quot;</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>*frame* <span style="color: #66cc66;">&#40;</span>create-new-sysinfo-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> Thread <span style="color: #66cc66;">&#40;</span>sleep <span style="color: #cc66cc;">100</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>

]]></content:encoded>
			<wfw:commentRss>http://www.adztec-independent.de/2009/01/clojure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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('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
5
6
7
8
</pre></td><td class="code" id="p90code6"><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('p90code7'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p907"><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="p90code7"><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('p90code8'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p908"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p90code8"><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>
		<item>
		<title>Lisp labels and anonymous functions</title>
		<link>http://www.adztec-independent.de/2008/12/lisp-labels-and-anonymous-functions/</link>
		<comments>http://www.adztec-independent.de/2008/12/lisp-labels-and-anonymous-functions/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 02:08:20 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[common lisp]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[special operator]]></category>

		<guid isPermaLink="false">http://www.adztec-independent.de/?p=79</guid>
		<description><![CDATA[I was playing around with Lisp some more in the last few weeks, and today i came across a little problem with anonymous functions (or lambdas, if you want to).
The problem with anonymous lambda functions is, that they can&#8217;t call themselves recursively, since &#8211; well they&#8217;re anonymous and hence don&#8217;t have a name. To get [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing around with Lisp some more in the last few weeks, and today i came across a little problem with anonymous functions (or lambdas, if you want to).</p>
<p>The problem with anonymous lambda functions is, that they can&#8217;t call themselves recursively, since &#8211; well they&#8217;re anonymous and hence don&#8217;t have a name. To get passed this problem, you can let it have an additional function parameter which it will call in place where the recursion would normally take place.</p>
<p>An example for a reverse function, which reverses the elements of its argument (a list) with the help of an accumulator list (which holds the reversed list) could be written like this, without using an additional function:</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('p79code12'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7912"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p79code12"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> myreverse <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&amp;</span>optional <span style="color: #66cc66;">&#40;</span>acc <span style="color: #b1b100;">nil</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>head <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</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: #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><span style="color: #b1b100;">cond</span>
      <span style="color: #66cc66;">&#40;</span>head
       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
	 <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">atom</span> head<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>myreverse rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> head acc<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	 <span style="color: #66cc66;">&#40;</span>t
	  <span style="color: #66cc66;">&#40;</span>myreverse rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>myreverse head<span style="color: #66cc66;">&#41;</span> acc<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;">&#40;</span>t
       acc<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>But, as you can see, the accumulator list is part of the function&#8217;s argument list, although optional. To make this function a little bit &#8217;safer&#8217;, it shouldn&#8217;t be part of the &#8216;public interface&#8217; of the function. So you&#8217;d probably come up with something like this, where you&#8217;d use an anonymous helper function, which takes the additional accumulator list and returns it when it&#8217;s done reversing the original list:</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('p79code13'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7913"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p79code13"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> myreverse <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</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>reverse-acc #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>func <span style="color: #b1b100;">list</span> acc<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>head <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</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: #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><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span>head
				 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">atom</span> head<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> func func rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> head acc<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
				       <span style="color: #66cc66;">&#40;</span>t
					<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> func func rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> func func head <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> acc<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;">&#40;</span>t
				 acc<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;">&#40;</span><span style="color: #b1b100;">funcall</span> reverse-acc reverse-acc <span style="color: #b1b100;">list</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></pre></td></tr></table></div>

<p>But, as you can see, since anonymous functions can&#8217;t call themselves recursively, you need to give it another extra parameter, a function object, which actually gets filled with the anonymous function itself, so it can call itself inside its body. But, fortunately, theres an even better version how to do exactly this in lisp: With the labels special operator, which defines local functions with a given name (like normal functions) which can then get called inside the function body itself and in the body-form of the macro:</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('p79code14'); return false;">View Code</a> LISP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7914"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p79code14"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> myreverse <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>labels <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>myreverse-acc <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> acc<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>head <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</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: #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><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span>head
		      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">atom</span> head<span style="color: #66cc66;">&#41;</span>
			     <span style="color: #66cc66;">&#40;</span>myreverse-acc rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> head acc<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			    <span style="color: #66cc66;">&#40;</span>t
			     <span style="color: #66cc66;">&#40;</span>myreverse-acc rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>myreverse-acc head <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> acc<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;">&#40;</span>t
		      acc<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;">&#40;</span>myreverse-acc <span style="color: #b1b100;">list</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></pre></td></tr></table></div>

<p>As you can see, this version is nicer than the first or the second, since it hides the fact, that the reverse function uses an accumulator list and that it is also easier to understand, since you can use an actual name for the function instead of passing the function as an argument to itself, in ordner to make a recursive call. So basically, you can think of a function defined with the labels special operator as a local defun, which I think is really nice. <img src='http://www.adztec-independent.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.adztec-independent.de/2008/12/lisp-labels-and-anonymous-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Language creation</title>
		<link>http://www.adztec-independent.de/2008/12/language-creation/</link>
		<comments>http://www.adztec-independent.de/2008/12/language-creation/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 21:41:38 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[hobbies]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.adztec-independent.de/?p=76</guid>
		<description><![CDATA[ 

I recently started thinking about creating a programming language from scratch, but obviously heavily inspired by existing programming languages out there.
I don&#8217;t have anything concrete yet but these are the main principles I came up with for the language:

Somewhat functional (but not purely functional). So side-effects should be more or less easily possible within non side-effecting [...]]]></description>
			<content:encoded><![CDATA[<p> </p>
<div>
<p>I recently started thinking about creating a programming language from scratch, but obviously heavily inspired by existing programming languages out there.</p>
<p>I don&#8217;t have anything concrete yet but these are the main principles I came up with for the language:</p>
<ul>
<li>Somewhat functional (but not purely functional). So side-effects should be more or less easily possible within non side-effecting code. Mainly for reasons regarding the process of actually building the compiler / interpreter and that it should be a dynamic language. Especially the last point makes it pretty obvious that since there is no static typechecking, making  it purely functional would be kind of hard to realise (although possible i guess). </li>
<li>Garbage-collected / automatic memory management. Kind of makes sense with a functional language and who really wants another assembly-like, low-level language?</li>
<li>An easy way to extend the language in itself (as in Lisp, for example, with macros or some similar meta-programming capability).</li>
<li>Dynamically typed (like Lisp or Ruby for example)</li>
<li>Small, in the sense that the language itself should not be bloated and most of the value of it shouldn&#8217;t be part of the language itself, but implemented in it. By that I don&#8217;t only mean the standard library, where this usually is the case already, but also those things, often considered part of the language (or &#8220;built-in&#8221;) in other languages. Haskell is a good example of operators and data types not being tied completely into the language, but rather implemented in it. A good example is the Boolean data type, which is merely defined in Haskell itself (and if it hadn&#8217;t been, it would be trivial to come up with your own). This works because Haskell uses some very nice pattern matching with its algebraic datatypes and I could imagine something similar for my language.</li>
</ul>
<p>That are my main concerns this far. Obviously theres still much to decide, mainly syntax and semantics. Compelling are Haskell&#8217;s typesystem with its really powerful, yet simple datatypes and pattern matching. On the other hand there&#8217;s Lisp with its macros and Ruby with its also powerful meta-programming mechanisms.</p>
<p>Let&#8217;s see, where this will go <img src='http://www.adztec-independent.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
]]></content:encoded>
			<wfw:commentRss>http://www.adztec-independent.de/2008/12/language-creation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

