<?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>Artisan Coder</title>
	<atom:link href="http://www.artisancoder.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artisancoder.com</link>
	<description>Software development is art</description>
	<lastBuildDate>Mon, 23 Aug 2010 13:19:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Praising macros</title>
		<link>http://www.artisancoder.com/2010/06/praising-macros/</link>
		<comments>http://www.artisancoder.com/2010/06/praising-macros/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 21:37:20 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://www.artisancoder.com/?p=182</guid>
		<description><![CDATA[Macros are an indispensable programming feature. While procedures (functions, methods, messages etc.) let developers reuse common computation patterns, macros allow the abstraction of common syntactic constructions. These constructions can be as simple as a new control operator, or can be full domain-specific languages. For instance, there is no when operator in Scheme, but a lot [...]]]></description>
			<content:encoded><![CDATA[<p>Macros are an indispensable programming feature. While procedures (functions, methods, messages etc.) let developers reuse common computation patterns, macros allow the abstraction of common syntactic constructions. These constructions can be as simple as a new control operator, or can be full <a href="http://en.wikipedia.org/wiki/Domain-specific_language" title="Domain-specific language">domain-specific languages</a>. For instance, there is no <tt>when</tt> operator in Scheme, but a lot of people like it. It simply tests a condition and execute a block of code if the former evaluates to true:</p>

<div class="wp_codebox"><table><tr id="p1823"><td class="code" id="p182code3"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>when <span style="color: #66cc66;">&#40;</span>launch<span style="color: #66cc66;">-</span>authorised? president<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>fuel<span style="color: #66cc66;">-</span>missile icbm<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>open<span style="color: #66cc66;">-</span>hatch silo<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>launch<span style="color: #66cc66;">-</span>missile icbm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Another example of control operator is the very useful <a href="http://www.artisancoder.com/2008/12/equivalent-of-and-let/" title="Equivalent of and-let*?"><tt>and-let*</tt></a> macro, semi-standardised in <a href="http://srfi.schemers.org/srfi-2/" title="SRFI 2: AND-LET*: an AND with local bindings, a guarded LET* special form">SRFI-2</a>, tests several expressions in sequence, aborting the computation if any of them evaluates to false, optionally binding variables to the expressions&#8217;s resulting values in nested scopes otherwise. If none of the expressions evaluate to false, the body is executed in the resulting lexical environment. Those familiar with Haskell may find it somewhat similar to the <tt>Maybe</tt> monad.</p>
<p>Actually several of Scheme&#8217;s own standard control operators are macros, implemented almost exactly as user-defined macros. A named <tt>let</tt> can be easily rewritten as a <tt>letrec</tt> operator:</p>

<div class="wp_codebox"><table><tr id="p1824"><td class="code" id="p182code4"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; a named let expression</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> collect <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>seq <span style="color: #66cc66;">&#40;</span>produce<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>of<span style="color: #66cc66;">-</span>numbers<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>even '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>odd '<span style="color: #66cc66;">&#40;</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;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> seq<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> even odd<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>num <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> seq<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;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">even?</span> num<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>collect <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> seq<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> num even<span style="color: #66cc66;">&#41;</span> odd<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>collect <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> seq<span style="color: #66cc66;">&#41;</span> even <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> num odd<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;
<span style="color: #808080; font-style: italic;">;; becomes a letrec expression</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">letrec</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>collect <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>seq even odd<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;">null?</span> seq<span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> even odd<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>num <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> seq<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;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">even?</span> num<span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>collect <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> seq<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> num even<span style="color: #66cc66;">&#41;</span> odd<span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>collect <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> seq<span style="color: #66cc66;">&#41;</span> even <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> num odd<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><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>collect <span style="color: #66cc66;">&#40;</span>produce<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>of<span style="color: #66cc66;">-</span>numbers<span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</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>Macros are of course not limited to adding new control operators to a language. One can design his own little language for solving specific problems, like <a href="http://www.artisancoder.com/2008/04/dsls-are-cool/" title="DSLs are cool">stocks trading</a>. Or a data modelling language, a graphics drawing language etc. A language for object-oriented programming such as <a href="http://pagesperso-systeme.lip6.fr/Christian.Queinnec/WWW/Meroon.html" title="Meroon: an Object System in Scheme">Meroon</a> can be seamlessly embedded within Scheme. Paul Graham&#8217;s <a href="http://www.paulgraham.com/onlisp.html" title="On Lisp">On Lisp</a> book on advanced Common Lisp programming has several chapters devoted to macros. In short, the developer becomes almost as powerful as the language designer, and such distinction is blurred. Armed with such power, the developer becomes much more productive and, therefore, happy.</p>
<p>Unfortunately not many programming languages provide good macro facilities. The C preprocessor, for instance, only does textual replacement and is no aware of expressions. Granted, the syntax of the programming languages of the Lisp family is very simple and uniform, making better macros possible. Nevertheless other languages like <a href="http://www.haskell.org/haskellwiki/Template_Haskell" title="Template Haskell">Haskell</a> and <a href="http://brion.inria.fr/gallium/index.php/Camlp4" title="Camlp4">OCaml</a> with more complex syntax also provide such facilities.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2010/06/praising-macros/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Apple relaxes restriction on interpreted code</title>
		<link>http://www.artisancoder.com/2010/06/apple-relaxes-restriction-on-interpreted-code/</link>
		<comments>http://www.artisancoder.com/2010/06/apple-relaxes-restriction-on-interpreted-code/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 17:31:11 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.artisancoder.com/?p=174</guid>
		<description><![CDATA[Apple&#8217;s iOS SDK license agreement terms have always been source of disputes, discussions and many blog posts. Many accuse Apple of being too draconian and elitist, while being ineffective at barring bad applications from entering the store. The uproar just got fueled when Apple released the 4.0 version of the SDK with a new license [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s iOS SDK license agreement terms have always been source of disputes, discussions and many blog posts. Many accuse Apple of being too draconian and elitist, while being ineffective at barring bad applications from entering the store. The uproar just got fueled when Apple released the 4.0 version of the SDK with a new license agreement, changing</p>
<blockquote>
<p>3.3.1 — Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs.</p>
</blockquote>
<p>to</p>
<blockquote>
<p>3.3.1 — Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).</p>
</blockquote>
<p>The intended effect is obviously to ban third-party abstraction layers, like then soon-to-be-released <a href="http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/" title="Adobe Flash">Adobe&#8217;s Flash for iPhone</a>. But the wording is very broad and cover too many uses of other programming languages and technologies. Steve Jobs demoed himself the Tap Tap Revenge game for iPhone, which <a href="http://blog.anscamobile.com/2010/04/lua-the-lingua-franca-of-iphone-games/" title="Lua, the lingua franca of iPhone games">reportedly uses Lua</a>. The backslash was too big to ignore, making Apple revise the agreement again. This time section 3.3.2 was changed from</p>
<blockquote>
<p>3.3.2 &#8211; No interpreted code may be downloaded or used in an Application except for code that is interpreted and run by Apple’s Documented APIs and built-in interpreter(s).﻿</p>
</blockquote>
<p>to</p>
<blockquote>
<p>3.3.2 &#8211; Unless otherwise approved by Apple in writing, no interpreted code may be downloaded or used in an Application except for code that is interpreted and run by Apple’s Documented APIs and built-in interpreter(s). Notwithstanding the foregoing, with Apple’s prior written consent, an Application may use embedded interpreted code in a limited way if such use is solely for providing minor features or functionality that are consistent with the intended and advertised purpose of the Application.﻿</p>
</blockquote>
<p>Although still requiring Apple&#8217;s prior written approval, it leaves the door open for the use of other programming languages on iOS devices. Apple Outsider <a href="http://www.appleoutsider.com/2010/06/10/hello-lua/" title="Hello, Lua">believes</a> this is all about Lua, and he may be right. But of course Lua is not the only game in town.</p>
<p>I always thought <a href="http://www.artisancoder.com/2009/10/scheme-hits-the-app-store/" title="Scheme hits the App Store">my use of Scheme</a> in Reverso was ok with the previous version of the license agreement. No code is ever downloaded, and not interpreted either; Gambit-C compiles Scheme code to C which is compiled with Apple&#8217;s official tools to create a native library. But this changed with the new 3.3.1 section because my code was not originally written in one of the approved languages. <a href="http://jlongster.posterous.com/" title="jlongster’s posterous">James Long</a>, who first compiled Gambit-C for the iPhone, claimed that <a href="http://jlongster.com/blog/2010/04/09/scheme-dead-iphone/" title="Scheme is also dead on the iPhone">Scheme was dead on the iPhone</a>. But then Apple relented and changed section 3.3.2. What about now?</p>
<p>I believe that makes Scheme usable again, if used just like in Reverso: as a library. The application is written in Objective-C, but uses Scheme code (compiled to C) to only add features &#8220;that are consistent with the intended and advertised purpose of the Application&#8221;. Albeit it can be claimed that the code is not interpreted, this use follows the spirit of the law, even if it does not follow its letter.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2010/06/apple-relaxes-restriction-on-interpreted-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Moving</title>
		<link>http://www.artisancoder.com/2010/06/moving/</link>
		<comments>http://www.artisancoder.com/2010/06/moving/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 14:38:07 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Meta]]></category>
		<category><![CDATA[address change]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://www.artisancoder.com/?p=167</guid>
		<description><![CDATA[After nearly a decade of good service, I am moving the blog from ventonegro.org to artisancoder.com. Although I like the former domain name, it may be perceived as childish, and I am a serious person now. Please update your feed readers, and keep following!]]></description>
			<content:encoded><![CDATA[<p>After nearly a decade of good service, I am moving the blog from <tt>ventonegro.org</tt> to <tt>artisancoder.com</tt>. Although I like the former domain name, it may be perceived as childish, and I am a serious person now. <img src='http://www.artisancoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Please update your feed readers, and keep following!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2010/06/moving/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Syntactic closures in Sly</title>
		<link>http://www.artisancoder.com/2010/05/syntactic-closures-in-sly/</link>
		<comments>http://www.artisancoder.com/2010/05/syntactic-closures-in-sly/#comments</comments>
		<pubDate>Mon, 10 May 2010 19:52:57 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[sly]]></category>
		<category><![CDATA[syntactic closures]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=159</guid>
		<description><![CDATA[Continuing my quest in mastering Scheme, I have completely revamped Sly&#8216;s front-end and implemented syntactic closures in the source code expander. The initial motivation was to get alpha-renamed identifiers as the result of the expansion phase, to make it easier to apply subsequent transformations on the source code (like Dybvig&#8217;s Fixing Letrec). But it was [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my quest in mastering Scheme, I have completely revamped <a href="http://github.com/asandroq/sly" title="Sly Scheme">Sly</a>&#8216;s front-end and implemented <a href="http://community.schemewiki.org/?syntactic-closures" title="Syntactic Closures">syntactic closures</a> in the source code expander. The initial motivation was to get alpha-renamed identifiers as the result of the expansion phase, to make it easier to apply subsequent transformations on the source code (like Dybvig&#8217;s <a href="http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf" title="Fixing Letrec">Fixing Letrec</a>). But it was a worthy effort in itself. All previous derived syntax that was internally rewritten using <tt>gensym</tt> now is written using syntactic closures, making them clearer and much more robust because of the added hygiene.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2010/05/syntactic-closures-in-sly/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Babel Syndrome</title>
		<link>http://www.artisancoder.com/2010/01/babel-syndrome/</link>
		<comments>http://www.artisancoder.com/2010/01/babel-syndrome/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:10:06 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[r7rs]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[standard]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=156</guid>
		<description><![CDATA[At this moment the groups that will bring us the seventh revision of the report on the algorithmic language Scheme (R7RS), which is going to be split in two, are being formed. I just hope that they keep in mind that even the gods will fear us if they succeed at producing a report (or [...]]]></description>
			<content:encoded><![CDATA[<p>At this moment the groups that will bring us the seventh revision of the report on the algorithmic language Scheme (R7RS), which is going to be split in two, are being formed. I just hope that they keep in mind that even the gods will fear us if they succeed at producing a report (or reports) that the vast majority of us like:</p>
<p>
<blockquote>
&#8220;If now, while they are one people, all speaking the same language, they have started to do this, nothing will later stop them from doing whatever they propose to do.&#8221; &#8211; Genesis XI, v.6
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2010/01/babel-syndrome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scheme hits the App Store</title>
		<link>http://www.artisancoder.com/2009/10/scheme-hits-the-app-store/</link>
		<comments>http://www.artisancoder.com/2009/10/scheme-hits-the-app-store/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 19:32:47 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[othello]]></category>
		<category><![CDATA[reversi]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=123</guid>
		<description><![CDATA[I believe I have got the first Scheme application past Apple review into the iTunes App Store. It is yet another Reversi clone, called Reverso. It is a combination of 90% Scheme and 10% Objective-C, written with Gambit-C Scheme. James Long has already shown how to compile Gambit-C for the iPhone, and I started from [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dunasystems.com.br/reverso/images/revlogo.png" alt="Reverso logo" style="float:left;padding:20px;" /></p>
<p>I believe I have got the first Scheme application past Apple review into the iTunes App Store. It is yet another <a href="http://en.wikipedia.org/wiki/Reversi" title="Reversi">Reversi</a> clone, called <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=334979538&#038;mt=8" title="Reverso">Reverso</a>. It is a combination of 90% Scheme and 10% Objective-C, written with <a href="http://dynamo.iro.umontreal.ca/~gambit/wiki/index.php/Main_Page" title="Gambit-C">Gambit-C Scheme</a>. James Long has already <a href="http://jlongster.com/blog/2009/06/17/write-apps-iphone-scheme/" title="Writing apps for the iPhone in Scheme">shown how to compile Gambit-C for the iPhone</a>, and I started from there. My Scheme code is compiled to C by Gambit and later by GCC to produce native ARM code, bundled in a static library, which is ok with the iPhone SDK license agreement. The Objective-C then calls the library as a pure C library. The Scheme code deals with position evaluation, alpha-beta pruning, transposition tables, move legality, different strategies and so on. The Objective-C code deals with sound, animations, GUI, user preferences, basically everything that calls the iPhone OS API. Reversi was chosen because I like strategy games and it is much more algorithmic than artistic, and I am no artist.</p>
<p>The performance of the code is excellent. I used some Gambit-specific declarations, only fixnum arithmetic, pre-allocated a large heap, and called the garbage-collector every time the user needed to think. The search is not memory intensive, but the transposition tables are. I did not write a specific hash function but relied on Gambit-C&#8217;s table type. The boards used during search were retrieved from a pool (the newest Gambit-C has made <tt>subu8vector-move!</tt> a public API), so they did not put pressure on the garbage-collector. In the end it was a very successful experiment. Developing with Scheme is orders of magnitude more productive than with most other languages. Gambit-C is also one of the best Scheme compilers out there, and made my job a lot easier.</p>
<p><strong>Update</strong>: There was a bug in the application that caused it to play poorly. The weak AI was not Gambit&#8217;s or Scheme&#8217;s fault, but mine. I already sent an updated version to Apple that will play much better. Besides fixing the bug, I am now using Zobrist&#8217;s hashing instead of Gambit&#8217;s own hash function, that performs poorly for game positions. It is still written in Scheme, though. Now I am waiting for the approval of the update, and to hear further feedback from my users. <img src='http://www.artisancoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2009/10/scheme-hits-the-app-store/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Sly Scheme</title>
		<link>http://www.artisancoder.com/2009/09/sly-scheme/</link>
		<comments>http://www.artisancoder.com/2009/09/sly-scheme/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:29:12 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=119</guid>
		<description><![CDATA[&#8220;You think you know when you can learn, are more sure when you can write, even more when you can teach, but certain when you can program.&#8221; &#8211; Alan Perlis Just after have read Lisp in Small Pieces I felt the urge to write a Scheme compiler. This sentiment is common in the Lisp community, [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;You think you know when you can learn, are more sure when you can write, even more when you can teach, but certain when you can program.&#8221; &#8211; Alan Perlis</p>
</blockquote>
<p>Just after have read <a href="http://pagesperso-systeme.lip6.fr/Christian.Queinnec/WWW/LiSP.html" title="Lisp in Small Pieces">Lisp in Small Pieces</a> I felt the urge to write a Scheme compiler. This sentiment is common in the Lisp community, I guess. There are dozens of Scheme systems, some good, some bad, some maintained, some abandoned. There are large optimising compilers and small interpreters for embedded systems. But I, well, <strong>needed</strong> to know. I needed to know how to write a program that takes another as input and generates simple instructions that do the same computation. I needed to know how to a write a program that interprets those instructions, a runtime to provide primitives, a garbage collector to manage memory etc. So I started doing some coding in my spare time, and called the monster <em>Sly Scheme</em>.</p>
<p>I failed miserably. I began with the reader, believing that basic I/O was fundamental for a REPL (the REPL was the goal I set myself to achieve). The reader was growing too complex and my interest simply disappeared. Then I focused on the types, creating a large hierarchy of them, only to lose interest again. After some time I came across Abdulaziz Ghuloum&#8217;s <a href="http://lambda-the-ultimate.org/node/1752" title="An Incremental Approach to Compiler Construction">An Incremental Approach to Compiler Construction</a>, which starts with the compiler itself. Let another Scheme do the I/O! I then took this path, running my compiler with Gambit-C. But instead of generating machine code, it generated instructions for a virtual machine. I then proceeded in lockstep: Wrote in Scheme a new compiler feature, and then in C just enough to interpret the new instructions. Finally most of R4RS Scheme is compilable. Then I wrote a simple stop-and-copy garbage collector with two semi spaces. After that, I needed a runtime with enough procedures to run the compiler without Gambit-C. Almost an year later, I have my REPL (I am not that bad as a programmer, but this is just a hobby that I do in my spare time).</p>
<p>The code now sits in a <a href="http://github.com/asandroq/sly" title="Sly in Github">Github repository</a>. For those who cares to check it out, I must warn that everything was done as simplistic as possible, so I could have a working interpreter ASAP. Some things were even done outright stupidly. The compiler generates too many closures, it fails with simple cases, the VM uses a giant switch, it is slow, the garbage collector too etc. I probably will hack this code for the years to come, even if only as a hobby of mine. But the most important contribution of this project is how much I have been learning from it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2009/09/sly-scheme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Escape from Zurg</title>
		<link>http://www.artisancoder.com/2009/08/escape-from-zurg/</link>
		<comments>http://www.artisancoder.com/2009/08/escape-from-zurg/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 17:16:39 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=114</guid>
		<description><![CDATA[Lately I have been reading about searching in game trees. So a friend of mine sent me the &#8220;Escape from Zurg&#8221; paper, which talks about problem solving by tree searching with Haskell. In a large class of problems one is given a start state and some predicate for a desired final state. Moreover, the rules [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been reading about searching in <a href="http://en.wikipedia.org/wiki/Game_tree" title="Game tree">game trees</a>. So a friend of mine sent me the <a href="http://web.engr.oregonstate.edu/~erwig/papers/Zurg_JFP04.pdf" title="Escape from Zurg">&#8220;Escape from Zurg&#8221; paper</a>, which talks about problem solving by tree searching with Haskell.</p>
<p>In a large class of problems one is given a start state and some predicate for a desired final state. Moreover, the rules that dictate how a successor state is generated from a previous one are also given. Take a board game for instance, like tic-tac-toe. There is the initial state (which is the empty board), the predicate for a desired state (a state in which tree pieces of mine are aligned), and rules to create one state from another (the rules of the game). Another example of such problems is the &#8220;Escape from Zurg&#8221; one, that is stated as follows:</p>
<blockquote>
<p>Buzz, Woody, Rex, and Hamm have to escape from Zurg. They merely have to cross one last bridge before they are free. However, the bridge is fragile and can hold at most two of them at the same time. Moreover, to cross the bridge a flashlight is needed to avoid traps and broken parts. The problem is that our friends have only one flashlight with one battery that lasts for only 60 minutes (this is not a typo: <em>sixty</em>). The toys need different times to cross the bridge (in either direction):</p>
<table>
<tbody>
<tr>
<th>TOY</th>
<th>TIME</th>
</tr>
<tr>
<td>Buzz</td>
<td>5 minutes</td>
</tr>
<tr>
<td>Woody</td>
<td>10 minutes</td>
</tr>
<tr>
<td>Rex</td>
<td>20 minutes</td>
</tr>
<tr>
<td>Hamm</td>
<td>25 minutes</td>
</tr>
</tbody>
</table>
<p>Since there can be only two toys on the bridge at the same time, they cannot cross the bridge all at once. Since they need the flashlight to cross the bridge, whenever two have crossed the bridge, somebody has to go back and bring the flashlight to those toys on the other side that still have to cross the bridge. The problem now is: In which order can the four toys cross the bridge in time (that is, in 60 minutes) to be saved from Zurg?</p>
</blockquote>
<p>This class of problems is usually solved by searching. The initial state and all the successors ultimately build a DAG (directed acyclic graph) that is called the <em>search space</em>. Some search spaces are small, like tic-tac-toe playing or even the &#8220;Escape from Zurg&#8221; puzzle. Others are large, like playing Reversi; even larger, like playing Chess; others are infinite. Needless to say that that are better searching ways than others. This same puzzle, for instance, <a href="http://benjisimon.blogspot.com/2008/04/escape-from-zurg-scheme-solution.html" title="Escape From Zurg - A Scheme Solution">was solved in Scheme</a> by Ben Simon using the cool <tt>amb</tt> operator. The <tt>amb</tt> operator uses first-class continuations to backtrack and take another path in case the first one fails. It was an elegant solution for this puzzle, but it is not adequate for general searching because it backtracks blindly. There are others ways to search blindly, for instance <em>depth-first search</em> and <em>breadth-first search</em>.</p>
<p>In a depth-first search, we analyse the successors of a given state until there are no more successors to analyse, and then backtrack to a previous level of the tree. In a breadth-first search, we analyse the whole fringe of the tree (the <em>horizon nodes</em>) before expanding it one level further using the successor rules. This is good for infinite search spaces, because we will eventually find a solution if there is one. The implementation in Scheme is straightforward:</p>

<div class="wp_codebox"><table><tr id="p11410"><td class="code" id="p114code10"><pre class="scheme" style="font-family:monospace;">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>depth<span style="color: #66cc66;">-</span>first states successors goal?<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;">null?</span> states<span style="color: #66cc66;">&#41;</span>
      #f
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>state <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> states<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;">if</span> <span style="color: #66cc66;">&#40;</span>goal? state<span style="color: #66cc66;">&#41;</span>
            state
            <span style="color: #66cc66;">&#40;</span>depth<span style="color: #66cc66;">-</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> <span style="color: #66cc66;">&#40;</span>successors state<span style="color: #66cc66;">&#41;</span>
                                 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> states<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                         successors
                         goal?<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;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>breadth<span style="color: #66cc66;">-</span>first states successors goal?<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;">null?</span> states<span style="color: #66cc66;">&#41;</span>
      #f
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>state <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> states<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;">if</span> <span style="color: #66cc66;">&#40;</span>goal? state<span style="color: #66cc66;">&#41;</span>
            state            
            <span style="color: #66cc66;">&#40;</span>breadth<span style="color: #66cc66;">-</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> states<span style="color: #66cc66;">&#41;</span>
                                   <span style="color: #66cc66;">&#40;</span>successors state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                           successors
                           goal?<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>This is actually all we need for simple searches. Of course we need to supply the initial state, the procedure that generates successors and the goal predicate. These are specific for each kind of problem. Let&#8217;s see the Zurg problem procedures:</p>

<div class="wp_codebox"><table><tr id="p11411"><td class="code" id="p114code11"><pre class="scheme" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">;; the type of the states found in our puzzle</span>
<span style="color: #66cc66;">&#40;</span>define<span style="color: #66cc66;">-</span>type state
  <span style="color: #66cc66;">&#40;</span>previous unprintable:<span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">;; previous state</span>
  near                          <span style="color: #808080; font-style: italic;">;; toys in near shore</span>
  far                           <span style="color: #808080; font-style: italic;">;; toys in far shore</span>
  flashlight                    <span style="color: #808080; font-style: italic;">;; location of flashlight</span>
  time<span style="color: #66cc66;">&#41;</span>                         <span style="color: #808080; font-style: italic;">;; time consumed so far</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>start<span style="color: #66cc66;">-</span>state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>state #f
              '<span style="color: #66cc66;">&#40;</span>buzz woody rex hamm<span style="color: #66cc66;">&#41;</span>
              '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
              'near
              <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>final<span style="color: #66cc66;">-</span>state? state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>near state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;=</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>time state<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">60</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;">;; at most two toys can travel across the bridge</span>
<span style="color: #808080; font-style: italic;">;; the flashlight must be used in all crossings</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>successors state<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>orig <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>flashlight state<span style="color: #66cc66;">&#41;</span>
                        'near<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>near state<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>far state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>dest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>flashlight state<span style="color: #66cc66;">&#41;</span>
                        'near<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>far state<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>near state<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;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>toy<span style="color: #66cc66;">-</span>pair<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>toy1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> toy<span style="color: #66cc66;">-</span>pair<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>toy2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> toy<span style="color: #66cc66;">-</span>pair<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>next<span style="color: #66cc66;">-</span>state state
                              <span style="color: #66cc66;">&#40;</span>remove toy2
                                      <span style="color: #66cc66;">&#40;</span>remove toy1 orig<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">&#40;</span>ucons toy1
                                     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> toy2 dest<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span>time<span style="color: #66cc66;">-</span>cost toy1<span style="color: #66cc66;">&#41;</span>
                                   <span style="color: #66cc66;">&#40;</span>time<span style="color: #66cc66;">-</span>cost toy2<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>product orig orig<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>The previous procedures use some utilities:</p>

<div class="wp_codebox"><table><tr id="p11412"><td class="code" id="p114code12"><pre class="scheme" style="font-family:monospace;">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>next<span style="color: #66cc66;">-</span>state state orig dest time<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;">eqv?</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>flashlight state<span style="color: #66cc66;">&#41;</span> 'near<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>state state orig dest 'far <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>time state<span style="color: #66cc66;">&#41;</span> time<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>state state dest orig 'near <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>time state<span style="color: #66cc66;">&#41;</span> time<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;">;; the time each toy takes to cross the bridge</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> time<span style="color: #66cc66;">-</span>cost
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>costs '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>buzz . <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>woody . <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>rex . <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>hamm . <span style="color: #cc66cc;">25</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;">lambda</span> <span style="color: #66cc66;">&#40;</span>toy<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>pair <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assv</span> toy costs<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;">and</span> pair
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> pair<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;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>ucons a b<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;">memv</span> a b<span style="color: #66cc66;">&#41;</span>
      b
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> a b<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;">define</span> <span style="color: #66cc66;">&#40;</span>product lis1 lis2<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> lp1 <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>lis1 lis1<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>res '<span style="color: #66cc66;">&#40;</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;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> lis1<span style="color: #66cc66;">&#41;</span>
        res
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>i1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> lis1<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> lp2 <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>lis2 lis2<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span>res res<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;">null?</span> lis2<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>lp1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> lis1<span style="color: #66cc66;">&#41;</span> res<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>i2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> lis2<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;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">member</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> i2 i1<span style="color: #66cc66;">&#41;</span> res<span style="color: #66cc66;">&#41;</span>
                      <span style="color: #66cc66;">&#40;</span>lp2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> lis2<span style="color: #66cc66;">&#41;</span> res<span style="color: #66cc66;">&#41;</span>
                      <span style="color: #66cc66;">&#40;</span>lp2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> lis2<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> i1 i2<span style="color: #66cc66;">&#41;</span> res<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><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Our <tt>successors</tt> procedure does not take time in account. So it is possible for a branch of the tree to go on indefinitely. To guarantee that we will arrive at a solution, we use a breadth-first search:</p>

<div class="wp_codebox"><table><tr id="p11413"><td class="code" id="p114code13"><pre class="scheme" style="font-family:monospace;">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>print<span style="color: #66cc66;">-</span>path state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> loop <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>state state<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>path '<span style="color: #66cc66;">&#40;</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;">if</span> state
        <span style="color: #66cc66;">&#40;</span>loop <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>previous state<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> state path<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">for-each</span> println path<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: #66cc66;">&#40;</span>print<span style="color: #66cc66;">-</span>path <span style="color: #66cc66;">&#40;</span>breadth<span style="color: #66cc66;">-</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>start<span style="color: #66cc66;">-</span>state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> successors final<span style="color: #66cc66;">-</span>state?<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>With the result:</p>
<pre>
#&lt;state #14 near: (buzz woody rex hamm) far: () flashlight: near time: 0&gt;
#&lt;state #15 near: (rex hamm) far: (buzz woody) flashlight: far time: 10&gt;
#&lt;state #16 near: (woody rex hamm) far: (buzz) flashlight: near time: 20&gt;

#&lt;state #17 near: (woody) far: (rex hamm buzz) flashlight: far time: 45&gt;
#&lt;state #18 near: (buzz woody) far: (rex hamm) flashlight: near time: 50&gt;
#&lt;state #19 near: () far: (buzz woody rex hamm) flashlight: far time: 60&gt;
</pre>
<p>If we change our <tt>successors</tt> procedure a little, we can use depth-first search:</p>

<div class="wp_codebox"><table><tr id="p11414"><td class="code" id="p114code14"><pre class="scheme" style="font-family:monospace;">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>successors2 state<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>orig <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>flashlight state<span style="color: #66cc66;">&#41;</span>
                        'near<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>near state<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>far state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>dest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eqv?</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>flashlight state<span style="color: #66cc66;">&#41;</span>
                        'near<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>far state<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>near state<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>filter <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;=</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">-</span>time <span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">;; filtering out bad states</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>toy<span style="color: #66cc66;">-</span>pair<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>toy1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> toy<span style="color: #66cc66;">-</span>pair<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">&#40;</span>toy2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> toy<span style="color: #66cc66;">-</span>pair<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                     <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>next<span style="color: #66cc66;">-</span>state state
                                      <span style="color: #66cc66;">&#40;</span>remove toy2
                                              <span style="color: #66cc66;">&#40;</span>remove toy1 orig<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                      <span style="color: #66cc66;">&#40;</span>ucons toy1
                                             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> toy2 dest<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span>time<span style="color: #66cc66;">-</span>cost toy1<span style="color: #66cc66;">&#41;</span>
                                           <span style="color: #66cc66;">&#40;</span>time<span style="color: #66cc66;">-</span>cost toy2<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>product orig orig<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;
<span style="color: #66cc66;">&#40;</span>print<span style="color: #66cc66;">-</span>path <span style="color: #66cc66;">&#40;</span>depth<span style="color: #66cc66;">-</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>start<span style="color: #66cc66;">-</span>state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> successors2 final<span style="color: #66cc66;">-</span>state?<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The advantage of changing the successors procedure is that depth-first searches are usually faster than breadth-first searches, in this case 483ms vs. 24630ms. Also, it uses less memory. So it is always a good idea to try to cut the search space as soon as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2009/08/escape-from-zurg/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Is the App Store the way?</title>
		<link>http://www.artisancoder.com/2009/07/is-the-app-store-the-way/</link>
		<comments>http://www.artisancoder.com/2009/07/is-the-app-store-the-way/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 15:36:15 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[freelancing]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=109</guid>
		<description><![CDATA[Last year I chose some goals for my career: Work with very clever people or alone, from home Work with good development tools, i.e., Git, Mercurial, Emacs etc. Work with good programming languages, i.e., Scheme (first choice), Haskell, Common Lisp or Lua These goals are intended to guarantee my long term happiness and stress-free high [...]]]></description>
			<content:encoded><![CDATA[<p>Last year I chose some goals for my career:</p>
<ul>
<li>Work with very clever people or alone, from home</li>
<li>Work with good development tools, i.e.,  Git, Mercurial, Emacs etc.
<li>Work with good programming languages, i.e., Scheme (first choice), Haskell, Common Lisp or Lua</li>
</ul>
<p>These goals are intended to guarantee my long term happiness and stress-free high productivity. I mean a rewarding professional life. So far I could not attain any of them. Here in Brazil the chance of getting a regular job with these traits is nil. I began then to look for freelancing work, but I could only find worthless Java/PHP/ASP/VB/etc. projects. Unfortunately, here, those are the &#8220;high-tech&#8221; jobs.</p>
<p>As seen in my previous post, it is possible to use Scheme to develop for the iPhone and for the iPod Touch. Developing for the App Store has the potential of fulfilling my goals. Of course it is an already crowded market, but so far it seems to be the only way out. So I paid Apple and registered as an iPhone developer. Let&#8217;s see what I can come up with.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2009/07/is-the-app-store-the-way/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Writing apps for the iPhone in Scheme</title>
		<link>http://www.artisancoder.com/2009/06/writing-apps-for-the-iphone-in-scheme/</link>
		<comments>http://www.artisancoder.com/2009/06/writing-apps-for-the-iphone-in-scheme/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 19:55:43 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=107</guid>
		<description><![CDATA[James Long wrote a nice and comprehensive article about using Gambit-C Scheme for the development of iPhone applications.]]></description>
			<content:encoded><![CDATA[<p><a href="http://jlongster.com/" title="James Long">James Long</a> wrote a nice and comprehensive <a href="http://jlongster.com/blog/2009/06/17/write-apps-iphone-scheme/" title="Writing apps for the iPhone in Scheme">article</a> about using Gambit-C Scheme for the development of iPhone applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2009/06/writing-apps-for-the-iphone-in-scheme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
