<?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>It Seemed Like a Good Idea at the Time &#187; tutorial</title>
	<atom:link href="http://blog.willdonnelly.net/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.willdonnelly.net</link>
	<description>Coding, Mostly</description>
	<lastBuildDate>Fri, 20 Aug 2010 15:33:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fixing Broken Macros with Eval and Quasiquote</title>
		<link>http://blog.willdonnelly.net/2009/03/21/fixing-broken-macros-with-eval-and-quasiquote/</link>
		<comments>http://blog.willdonnelly.net/2009/03/21/fixing-broken-macros-with-eval-and-quasiquote/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 01:20:36 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[macro]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=210</guid>
		<description><![CDATA[Recently, I found myself needing to deal with a "conven [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I found myself needing to deal with a "convenience" macro, which quoted several of its arguments for me before passing them along to the real function.  Unfortunately, only the macro was exported from the library, and I was unable to access the base function.</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> convenient<span style="color: #66cc66;">-</span>function<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_ arg1 arg2<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>much<span style="color: #66cc66;">-</span>harder<span style="color: #66cc66;">-</span>function 'arg1 'arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>How useful.  To save me a handful of quotes, I lose the ability to programmatically generate my arguments.</p>
<p>As I was loath to reimplement the entire library just to regain that ability, I looked for another solution.  Thankfully, I found one.</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>much<span style="color: #66cc66;">-</span>harder<span style="color: #66cc66;">-</span>function arg1 arg2<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>eval `<span style="color: #66cc66;">&#40;</span>convenient<span style="color: #66cc66;">-</span>function ,arg1 ,arg2<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>environment '<span style="color: #66cc66;">&#40;</span>convenient<span style="color: #66cc66;">-</span>lib<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>As far as I can tell, this circumvents the macro's quoting features entirely to allow me to pass in a dynamically generated symbol.  While it won't work with functions which modify the environment, it worked well in my case, and allowed me to move on to more interesting code.</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F&amp;title=Fixing+Broken+Macros+with+Eval+and+Quasiquote" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F&amp;title=Fixing+Broken+Macros+with+Eval+and+Quasiquote" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F&amp;title=Fixing+Broken+Macros+with+Eval+and+Quasiquote" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F&amp;headline=Fixing+Broken+Macros+with+Eval+and+Quasiquote" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Fixing+Broken+Macros+with+Eval+and+Quasiquote&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Fixing+Broken+Macros+with+Eval+and+Quasiquote&amp;u=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Fixing+Broken+Macros+with+Eval+and+Quasiquote&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Fixing+Broken+Macros+with+Eval+and+Quasiquote&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Fixing+Broken+Macros+with+Eval+and+Quasiquote&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F&amp;title=Fixing+Broken+Macros+with+Eval+and+Quasiquote&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fblog.willdonnelly.net%2F2009%2F03%2F21%2Ffixing-broken-macros-with-eval-and-quasiquote%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2009/03/21/fixing-broken-macros-with-eval-and-quasiquote/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The SoLoad Foreign Function Server (Part III) &#8211; Intermediate Usage</title>
		<link>http://blog.willdonnelly.net/2008/09/15/the-soload-foreign-function-server-part-iii-intermediate-usage/</link>
		<comments>http://blog.willdonnelly.net/2008/09/15/the-soload-foreign-function-server-part-iii-intermediate-usage/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 19:27:08 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[soload]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=54</guid>
		<description><![CDATA[This is the third post in a series or four.  It comes a [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third post in a series or four.  It comes after <a title="The SoLoad Foreign Function Server (Part I) - An Introduction" href="http://willdonnelly.wordpress.com/2008/08/30/the-soload-foreign-function-server-part-i-an-introduction/">Introduction</a> and <a title="The SoLoad Foreign Function Server (Part II) - Basic Usage" href="http://willdonnelly.wordpress.com/2008/09/12/the-soload-foreign-function-server-part-ii-basic-usage/">Basic Usage</a>.</p>
<p>In the last two posts, I discussed the reason for SoLoad, and how to use it. The basic usage guide skipped a few of the more complex features in the interest of simplicity. The guide only showed how to use numeric types, and not pointers, strings, or user-defined types.  Also, it didn't mention the use of loadable definition files.  This post will discuss those features.</p>
<h3>Strings</h3>
<p>Strings in SoLoad are fairly simple to use. Any sequence of characters, separated by spaces, will be read as a string if its type says so.  To add spaces to a string, simply use quotes.  To add quotes to a string, use a backslash to escape them, and to add a backslash, use two backslashes.  Strings in SoLoad look pretty much like standard C strings.</p>
<p>Say that we have a hypothetical "message" function, which we want to load from a library and use.  It takes a string (char*) as an argument, and puts it in a message box.  It has no return value.</p>
<p>Loading this function is simple.  It looks like</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">load &amp;lt;library&amp;gt; message void 1 string</div></div>
<p>(</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">load &amp;lt;library&amp;gt; message void 1 char*</div></div>
<p>is also acceptable)</p>
<p>Calling it is just that simple, too.  For a single word output, you can just say</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call message Hello</div></div>
<p>and get "Hello" as a message. Quotes are needed if we want to use spaces in the string:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call message &quot;Hello, world!&quot;</div></div>
<p>And quotation marks can be escaped with a backslash:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call message &quot;\&quot;Hello, world!\&quot;, he said.&quot;</div></div>
<h3>Pointers</h3>
<p>Using pointers in SoLoad is very simple.  If all you need to do is to call functions that return pointers, and pass those values to other functions, you can just do that.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&amp;gt; call get_pointer 37<br />
0x2364DADB<br />
&amp;gt; call get_value 0x2364DADB<br />
37</div></div>
<p>If you want to be able to create new pointers or get their values, however, there are a few new commands to master.</p>
<h5>type</h5>
<p>The</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">type</div></div>
<p>command registers a new type with SoLoad.  It has pretty much the same syntax as the argument part of loading a function.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">type aType 2 int string</div></div>
<h5>new</h5>
<p>The</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">new</div></div>
<p>command creates an instance of a type in memory, and returns a pointer to it.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&amp;gt; new aType 37 &quot;Hello, World!&quot;</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0x253DB25A</div></div>
<h5>value</h5>
<p>The</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">value</div></div>
<p>command returns the values contained in a struct at the specified address.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&amp;gt; value aType</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0x253DB25A<br />
37 &quot;Hello, World!&quot;</div></div>
<h5>delete</h5>
<p>The</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">delete</div></div>
<p>command frees the struct at the given address.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">delete aType</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0x253DB25A</div></div>
<h5>deltype</h5>
<p>The del</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">type</div></div>
<p>command deletes a type that was already registered with SoLoad.  WARNING: Doing this will probably make SoLoad unable to delete remaining instances of this type.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">deltype aType</div></div>
<h3>Def Files</h3>
<p>All of the commands for opening a library and loading functions work well enough for one-shot uses, but they can get a little tedious when loading an entire library.  Also, I think that bindings developed for SoLoad should easily be usable by any program that uses SoLoad.  I intend to provide this functionality through loadable definitions files. These files are simply text files that list various bits of information necessary to load a library and its functions.  They are incapable of deleting data, they can only open libraries, load functions, and define types.  They look like this:</p>
<pre>name sdl
paths /usr/lib/libSDL.so /usr/lib/SDL.so /usr/local/lib/libSDL.so
function SDL_Init int 1 uint32
function SDL_Quit void 0
function SDL_SetVideoMode pointer 4 int int int uint32
function SDL_WM_ToggleFullScreen int 1 pointer
type struct_one 2 int int
type struct_two 2 int string</pre>
<p>The "name" statement declares a short name for the library.</p>
<p>The "paths" statement lists paths that the library might be found at.  They will be tried one at a time in order until one is opened successfully.</p>
<p>The "function" statement loads a function from the library.  "function ..." is equivalent to "load &lt;short-name&gt; ..."</p>
<p>The "type" statement does the exact same thing as the "type" statement does in interactive mode.</p>
<p>And that concludes the intermediate level usage tutorial for SoLoad.  It should now be possible for you to load and use any kind of function that SoLoad supports.  You should also be able to interact with structures, pointers, and strings.  In the next (and probably final) post in this series, I will detail the process of creating an FFI library for a dialect of Scheme (just as soon as I get around to writing the library myself).</p>
<script type="text/javascript">
var flattr_wp_ver = '0.9.11';
var flattr_uid = '19057';
var flattr_url = 'http://blog.willdonnelly.net';
var flattr_lng = 'en_GB';
var flattr_cat = 'text';
var flattr_tag = 'blog,wordpress,rss,feed';
var flattr_btn = 'large';
var flattr_tle = 'It Seemed Like a Good Idea at the Time';
var flattr_dsc = 'Coding, Mostly';
</script>
<script src="http://api.flattr.com/button/load.js?v=0.2" type="text/javascript"></script><div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F&amp;headline=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage&amp;u=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+III%29+-+Intermediate+Usage&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F15%2Fthe-soload-foreign-function-server-part-iii-intermediate-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2008/09/15/the-soload-foreign-function-server-part-iii-intermediate-usage/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The SoLoad Foreign Function Server (Part II) &#8211; Basic Usage</title>
		<link>http://blog.willdonnelly.net/2008/09/12/the-soload-foreign-function-server-part-ii-basic-usage/</link>
		<comments>http://blog.willdonnelly.net/2008/09/12/the-soload-foreign-function-server-part-ii-basic-usage/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 04:32:42 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[soload]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=18</guid>
		<description><![CDATA[I'm sorry this post has taken so long.  I started writ [...]]]></description>
			<content:encoded><![CDATA[<p>I'm sorry this post has taken so long.  I started writing it over a week ago, but by the time it was finished, it looked more like a reference document than a guide to basic usage.  I decided that a document like that would be much better if placed on the <a title="SoLoad Wiki" href="http://soload.wikidot.com/start">SoLoad Wiki</a>.</p>
<p>After much editing down, I ended up with this tutorial, which is much easier to read.</p>
<p>The <a href="http://soload.wikidot.com/start">SoLoad Foreign Function Server</a> is a program that allows interpreters and scripting languages to call C functions.  The only requirements for use are those necessary to start a process and communicate with it.</p>
<p><a title="The SoLoad Foreign Function Server (Part I) - An Introduction" href="http://willdonnelly.wordpress.com/2008/08/30/the-soload-foreign-function-server-part-i-an-introduction/">Previously</a>, I explained the need for and the purpose of SoLoad.  In this post, I will explain how it is used in a command-line session.</p>
<h3>Starting</h3>
<p>SoLoad has, at the time of this writing, two methods of communication with the outside world.  It can talk over the standard input/output streams, and via TCP.  To start SoLoad and control it over stdin, use the command</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">soload stdin</div></div>
<p>Similarly, to use it over TCP, just say</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">soload tcp &amp;lt;port&amp;gt;</div></div>
<p>Once you have connected to the port via telnet, there should be no further differences between the different communications methods.</p>
<h3>Opening a Library</h3>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">open &lt;em&gt;path&lt;/em&gt; [&lt;em&gt;short-form&lt;/em&gt;]</div></div>
<p>To open a library, use the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">open</div></div>
<p>command.  The</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">open</div></div>
<p>command takes one or two arguments.  The first is the path to the library, and the second is an optional short name for the library.  If provided, this short name can be used interchangeably with the library identifier that is returned by the command.</p>
<p>For the purposes of this example, we will try to open the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">libm</div></div>
<p>shared library.  I assume that the path to it is "/lib/libm.so.6" . If it is different on your system, the command will need to be modified accordingly.</p>
<p>So, to open the libm library, the command goes</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">open /lib/libm.so.6 libm</div></div>
<p>This opens the library and assigns it a short name of "libm".</p>
<h3>Loading a Function</h3>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">load &lt;em&gt;library-id&lt;/em&gt; &lt;em&gt;function-name&lt;/em&gt; &lt;em&gt;return-type&lt;/em&gt; &lt;em&gt;num-args&lt;/em&gt; &lt;em&gt;arg-types&lt;/em&gt;*</div></div>
<p>Loading a function from a library is done with the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">load</div></div>
<p>command.  The load command takes as its arguments: a library identifier, the name of the function to be loaded, the return type, the number of arguments to the function, and the types of all the arguments.  For our purposes, the command will look like</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">load libm sin double 1 double</div></div>
<p>If, however, we had opted not to provide a short name for the library when opening it, the call would probably look something more like</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">load 0x125FA427 sin double 1 double</div></div>
<h3>Calling a Function</h3>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call &lt;em&gt;function-id&lt;/em&gt; &lt;em&gt;arguments&lt;/em&gt;*</div></div>
<p>To call a function, use the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">command.  The</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call</div></div>
<p>command takes a function identifier, and a number of arguments.  For us, it will look like</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call sin 0.34612</div></div>
<p>and will return the result of the call.</p>
<h3>Exiting</h3>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exit</div></div>
<p>To exit, simply type</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exit</div></div>
<p>.  Everything should automatically be unloaded, and SoLoad will close.</p>
<h3>Cleanup (Optional)</h3>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">unload &lt;em&gt;function-id&lt;/em&gt;<br />
close &lt;em&gt;library-id&lt;/em&gt;</div></div>
<p>The following functions don't usually need to be used.  They are provided simply because doing so adds barely any complexity to the program, and allows use in situations where libraries must be continually loaded and unloaded without exiting, and without filling up all available memory with old library references.</p>
<p>Again, <strong>SoLoad will automatically do this cleanup when it exits</strong>, so you are free to just quit now if you wish.</p>
<p>To unload a function, use the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">unload</div></div>
<p>command.  It takes a function identifier as its only argument.  It would look like</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">unload sin</div></div>
<p>Similarly, to close a library, simply say</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">close libm</div></div>
<p>The library is closed, and functions loaded from it are no longer valid. (Note: Closing a library does not automatically unload all of its functions, but those functions <strong>will</strong> still be unloaded at exit)</p>
<h3>A More Complex Example</h3>
<p>The following is a transcript of a simple interaction with the SDL library.  SoLoad will be used to open SDL, create a window, and then quit.</p>
<pre>&gt; soload stdin
&gt; open /usr/lib/libSDL.so sdl
0x9fd7058
&gt; load sdl SDL_Init int 1 uint32
0x9fd7be8
&gt; load sdl SDL_SetVideoMode pointer 4 int int int uint32
0x9fd7bc8
&gt; load sdl SDL_Quit void 0
0x9fd7d88
&gt; call SDL_Init 0
0
&gt; call SDL_SetVideoMode 640 480 16 0
0xa004998
&gt; call SDL_Quit
@�
&gt; exit
exit</pre>
<p>Stay tuned for the next post in the series, in which I will discuss intermediate level usage (structs and strings).</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F&amp;headline=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage&amp;u=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F&amp;title=The+SoLoad+Foreign+Function+Server+%28Part+II%29+-+Basic+Usage&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F12%2Fthe-soload-foreign-function-server-part-ii-basic-usage%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2008/09/12/the-soload-foreign-function-server-part-ii-basic-usage/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Scheme Syntax-Rules Primer</title>
		<link>http://blog.willdonnelly.net/2008/09/04/a-scheme-syntax-rules-primer/</link>
		<comments>http://blog.willdonnelly.net/2008/09/04/a-scheme-syntax-rules-primer/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 02:34:48 +0000</pubDate>
		<dc:creator>Will Donnelly</dc:creator>
				<category><![CDATA[scheme]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[macros]]></category>

		<guid isPermaLink="false">http://willdonnelly.wordpress.com/?p=24</guid>
		<description><![CDATA[Scheme has a wonderfully powerful hygienic macro system [...]]]></description>
			<content:encoded><![CDATA[<p>Scheme has a wonderfully powerful hygienic macro system.  Unfortunately, explanations on how to use it are few and far between.  <a title="Revised^5 Report on the Algorithmic Languge Scheme" href="http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-7.html#%_sec_4.3">R5RS </a>is utterly incomprehensible to anyone who doesn't already have a firm grounding in hygienic macro systems, and <a title="Teach Yourself Scheme in Fixnum Days" href="http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-10.html#node_chap_8">TYSiFD</a>'s section on macros dates from a time before syntax-rules and hygiene.</p>
<p>So this here is my attempt to share what I've learned over the past few days with regards to syntax-rules macros.  Bear in mind that at the time of writing this, I have known how to use syntax-rules since <strong>yesterday</strong>, but my knowledge seems to be complete enough to write a working module system, so here's my attempt to pass on what I've learned.</p>
<p>First things first.  You use define-syntax to create a top level binding, and let-syntax bears the same relationship to define-syntax as you'd expect.</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;; define-syntax is used to create</span><br />
<span style="color: #808080; font-style: italic;">;; &nbsp;a top-level binding of a macro</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> macro<br />
&nbsp; <span style="color: #66cc66;">&lt;</span>syntax transformer<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>That much is pretty clear from reading R5RS, now we need to figure out how to write the syntax transformer.  There are a few ways to do this, but the best way for beginners is to use syntax-rules, which avoids inadvertent variable capture and other such nasties automatically, and uses a rather elegant pattern matching language.</p>
<p>We'll start by trying to write a simple macro, <code class="codecolorer text default"><span class="text">while</span></code>. This will just be a standard looping construct that keeps executing over and over until its condition becomes false.  We want its usage to look something like this:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;; A simple while loop</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> x <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span>while <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> x <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set!</span> x <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> x <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>print x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>So first, we need the <code class="codecolorer text default"><span class="text">define-syntax</span></code> form.</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> while<br />
&nbsp; <span style="color: #66cc66;">&lt;</span>syntax transformer<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Next, we need to write the syntax transformer itself.  This is where syntax-rules comes into play.  Syntax-rules uses pattern matching and text substitution to allow you to make some pretty advanced macros.  It looks like:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> while<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span>keywords<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span>pattern<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>template<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span>pattern<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>template<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>I will explain keywords later.  For now, just leave that bit blank.  What we're interested in are those <code class="codecolorer scheme default"><span class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span>pattern<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>template<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></span></code> pairs.  Each <code class="codecolorer scheme default"><span class="scheme"><span style="color: #66cc66;">&lt;</span>pattern<span style="color: #66cc66;">&gt;</span></span></code> is just that, a pattern of code that will be matched.  In our case, we want to match the pattern:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>while condition body ...<span style="color: #66cc66;">&#41;</span></div></div>
<p>Where the '...' signifies that <code class="codecolorer text default"><span class="text">body</span></code> may contain one or more forms.  Luckily for us, this is exactly the syntax that syntax-rules wants to see, so we can just plug it in, giving us:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> while<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>while condition body ...<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>template<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>So far so good.  Now we just have to fill in the other half, with a suitable <code class="codecolorer text default"><span class="text">&lt;template&gt;</span></code></p>
<p>Before we can write the <code class="codecolorer text default"><span class="text">&lt;template&gt;</span></code>, though, we have to decide what we want the code to end up looking like.  Since this isn't a guide to scheme code in general, I'll just go ahead and say that we want the output to look like:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">; Thanks to Alex Shinn for pointing out a mistake</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> loop <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> condition<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; body ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>loop<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; #f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Got that?  Okay, now we've just got to put this in our syntax-rules macro as a template.  By another startling coincidence, this is exactly what the template code is expected to look like.  We just plug in that code, and our final result is:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> while<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>while condition body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> loop <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> condition<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;body ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>loop<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#f<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></div></div>
<p>Just plug that into your scheme interpreter, and our <code class="codecolorer text default"><span class="text">while</span></code> loop from earlier should execute perfectly.</p>
<p>Now let's try to write something a little more complicated.  We want to write a <code class="codecolorer text default"><span class="text">for</span></code> loop similar to the one that Python has.  This should be a pretty easy task, since it's basically just syntactic sugar for scheme's <code class="codecolorer text default"><span class="text">map</span></code> function.</p>
<p>Our goal is to be able to write a piece of code taking the form:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>for <span style="color: #66cc66;">&lt;</span>element<span style="color: #66cc66;">&gt;</span> in <span style="color: #66cc66;">&lt;</span>list<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&lt;</span>body ...<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>And have it expand to:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">; Thanks to Alex Shinn for pointing out a mistake</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">for-each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span>element<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&lt;</span>body ...<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>list<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Our first try would probably look something like this:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> for<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>for element in <span style="color: #b1b100;">list</span> body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<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>element<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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;">&#41;</span></div></div>
<p>This works, but there's one issue with it.  All of the following are valid and work exactly the same:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>for i in '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>print i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span>for i fnord '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>print i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span>for i some<span style="color: #66cc66;">-</span>other<span style="color: #66cc66;">-</span>keyword '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>print i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>This is not so much of a problem in the case of a for loop, but what if you wanted to add another rule later, so that</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>for '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> as i<br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>print i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>will also work?  The solution to this problem is in that <code class="codecolorer text default"><span class="text">&lt;keywords&gt;</span></code> argument that we glossed over earlier.  Change the keywords list to include 'in' (and, for good measure, 'as'), and it will allow those symbols, and only those symbols, in places where they are mentioned.  This change leaves us with:</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> for<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span>in as<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>for element in <span style="color: #b1b100;">list</span> body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<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>element<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>for <span style="color: #b1b100;">list</span> as element body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<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>element<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Or, for simplicity (thanks to Dan Prager for pointing this out)</p>
<div class="codecolorer-container scheme default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="scheme codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define-syntax</span> for<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">syntax-rules</span> <span style="color: #66cc66;">&#40;</span>in as<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>for element in <span style="color: #b1b100;">list</span> body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<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>element<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>for <span style="color: #b1b100;">list</span> as element body ...<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>for element in <span style="color: #b1b100;">list</span> body ...<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>And if we load this code into our scheme interpreter of choice, we should have two fully functional little bits of new syntax.</p>
<p>Hopefully this guide will help shed some light on the arcane subject that is the Scheme macro system, and hopefully I will never have to learn enough about syntax-case to write a tutorial on it.</p>
<script type="text/javascript">
var flattr_wp_ver = '0.9.11';
var flattr_uid = '19057';
var flattr_url = 'http://blog.willdonnelly.net';
var flattr_lng = 'en_GB';
var flattr_cat = 'text';
var flattr_tag = 'blog,wordpress,rss,feed';
var flattr_btn = 'large';
var flattr_tle = 'It Seemed Like a Good Idea at the Time';
var flattr_dsc = 'Coding, Mostly';
</script>
<script src="http://api.flattr.com/button/load.js?v=0.2" type="text/javascript"></script><div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F&amp;title=A+Scheme+Syntax-Rules+Primer" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F&amp;title=A+Scheme+Syntax-Rules+Primer" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F&amp;title=A+Scheme+Syntax-Rules+Primer" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F&amp;headline=A+Scheme+Syntax-Rules+Primer" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=A+Scheme+Syntax-Rules+Primer&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=A+Scheme+Syntax-Rules+Primer&amp;u=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=A+Scheme+Syntax-Rules+Primer&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=A+Scheme+Syntax-Rules+Primer&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=A+Scheme+Syntax-Rules+Primer&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F&amp;title=A+Scheme+Syntax-Rules+Primer&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fblog.willdonnelly.net%2F2008%2F09%2F04%2Fa-scheme-syntax-rules-primer%2F" ><img class="lightsocial_img" src="http://blog.willdonnelly.net/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://blog.willdonnelly.net/2008/09/04/a-scheme-syntax-rules-primer/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
