<?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:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Intuitionistically Uncertain &#187; Programming</title>
	<atom:link href="http://hircus.wordpress.com/category/technology/programming-languages/feed" rel="self" type="application/rss+xml" />
	<link>http://hircus.wordpress.com</link>
	<description>Michel explores computing and assorted gadgetries</description>
	<pubDate>Tue, 15 Jul 2008 20:45:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>C types 101</title>
		<link>http://hircus.wordpress.com/2008/06/08/c-types-101/</link>
		<comments>http://hircus.wordpress.com/2008/06/08/c-types-101/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 01:12:17 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[FLOSS]]></category>

		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2008/06/08/c-types-101/</guid>
		<description><![CDATA[ I was cleaning up the code of an application that I&#8217;m packaging for Fedora, and was Googling for information on size_t (in the code, a size_t variable was being printed as a normal integer (%d), which triggered a compiler warning, and I forgot what the relevant option is. Ended up finding it in printf&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div> I was cleaning up the code of an application that I&#8217;m packaging for Fedora, and was Googling for information on <strong>size_t</strong> (in the code, a size_t variable was being printed as a normal integer (<tt>%d</tt>), which triggered a compiler warning, and I forgot what the relevant option is. Ended up finding it in printf&#8217;s manpage) when I discovered this rather well-written gem. </div>
<table cellpadding="0" cellspacing="0" width="100%" style="font-family:arial;color:#333333;background:#ffffff;border:solid 4px #e5e5e5;width:100%;clear:left;margin:12px 0;">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="100%" class="CM_CTB_Content_Wrap" style="background-color:#ffffff;margin:0;padding:0;">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="100%" style="border-bottom:solid 1px #dcdcdc;white-space:nowrap;margin-bottom:8px;background-color:#eeeeee;background-image:url('http://clipmarks.com/images/source-bg.gif');background-repeat:repeat-x;height:24px;line-height:24px;vertical-align:middle;padding-bottom:4px;color:#666666;font-size:10px;">
<tr>
<td valign="top"><a href="http://clipmarks.com/clip-to-blog/" title="clipmarks' clip-to-blog"><img src="http://content.clipmarks.com/blog_icon/fb0e31ea-247b-4396-b879-55723cb0754c/7AF356FF-F474-45F5-AE3E-808FDE1D80F8/" alt="" width="19" height="19" border="0" style="vertical-align:middle;display:inline;border:none;float:none;margin:0 4px;" /></a>clipped from <a title="http://www.embedded.com/columns/programmingpointers/200900195" href="http://www.embedded.com/columns/programmingpointers/200900195">www.embedded.com</a></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="100%" style="text-align:left;background:transparent;border:none;margin:4px 0 8px;padding:0 8px;">
<tr>
<td valign="top"><!-- CLIPPED FROM: http://www.embedded.com/columns/programmingpointers/200900195 --><P><FONT size="3" face="Verdana" color="#003366"><B>Using size_t appropriately can improve the portability, efficiency, or readability of your code. Maybe even all three.</B></FONT></p>
<p> <P><br />
</P><P><br />
Numerous functions in the Standard C library accept arguments or return values that represent object sizes in bytes. For example, the lone argument in <FONT size="2" face="Courier"><B>malloc(n)</B></FONT> specifies the size of the object to be allocated, and the last argument in <FONT size="2" face="Courier"><B>memcpy(s1, s2, n)</B></FONT> specifies the size of the object to be copied. The return value of <FONT size="2" face="Courier"><B>strlen(s)</B></FONT> yields the length of (the number of characters in) null-terminated character array s excluding the null character, which isn&#8217;t exactly the size of <FONT size="2" face="Courier"><B>s</B></FONT>, but it&#8217;s in the ballpark.</p>
<p> <P><br />
</P><P><br />
You might reasonably expect these parameters and return types that represent sizes to be declared with type <FONT size="2" face="Courier"><B>int</B></FONT> (possibly <FONT size="2" face="Courier"><B>long</B></FONT> and/or <FONT size="2" face="Courier"><B>unsigned</B></FONT>), but they aren&#8217;t. Rather, the C standard declares them as type <FONT size="2" face="Courier"><B>size_t</B></FONT>. According to the standard, the declaration for <FONT size="2" face="Courier"><B>malloc</B></FONT> should appear in <FONT size="2" face="Courier"><B>&lt;stdlib.h&gt;</B></FONT> as something equivalent to:</p>
<p> <P><br />
</P><P><br />
</P><PRE><CODE><FONT size="2" face="Courier"><B>void *malloc(<SPAN>size_t</SPAN> n);</B></FONT></CODE></PRE></td>
</tr>
</table>
</td>
</tr>
</table>
<div style="margin:0 6px 6px 4px;">
<table style="font-size:11px;border-spacing:0;padding:0;" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="background:transparent;border-width:0;padding:0;">&nbsp;</td>
<td align="right" style="background:transparent;width:107px;border-width:0;padding:0;" width="107"><a href="http://clipmarks.com/share/7AF356FF-F474-45F5-AE3E-808FDE1D80F8/blog/" title="blog or email this clip"><img src="http://content9.clipmarks.com/images/c2b-foot.png" border="0" alt="blog it" width="107" height="17" style="border-width:0;margin:0;padding:0;" /></a></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/223/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/223/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/223/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=223&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2008/06/08/c-types-101/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://content.clipmarks.com/blog_icon/fb0e31ea-247b-4396-b879-55723cb0754c/7AF356FF-F474-45F5-AE3E-808FDE1D80F8/" medium="image" />

		<media:content url="http://content9.clipmarks.com/images/c2b-foot.png" medium="image">
			<media:title type="html">blog it</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming languages, in a nutshell</title>
		<link>http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/</link>
		<comments>http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 08:21:22 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/</guid>
		<description><![CDATA[Task: List the programming languages you consider important or interesting. Describe each of them in one sentence.


C
High-level assembler


Lua
Table-based, functional and embeddable


Python
Great RAD language crippled by dogma


Scala
Java done (almost) right, with Erlang- and Haskell-inspired features


Posting this from the new N810 using its built-in keyboard. Typing those tags was painful, and as a result, more languages will [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Task: <span style='font-style:italic;'>List the programming languages you consider important or interesting. Describe each of them in one sentence.</span></p>
<table>
<tr>
<td>C</td>
<td>High-level assembler</td>
</tr>
<tr>
<td>Lua</td>
<td>Table-based, functional and embeddable</td>
</tr>
<tr>
<td>Python</td>
<td>Great RAD language crippled by dogma</td>
</tr>
<tr>
<td>Scala</td>
<td>Java done (almost) right, with Erlang- and Haskell-inspired features</td>
</tr>
</table>
<p>Posting this from the new N810 using its built-in keyboard. Typing those tags was painful, and as a result, more languages will be added to the table tomorrow &#8212; from the workstation.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/216/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/216/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/216/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=216&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Wide Finder: OCaml and JoCaml</title>
		<link>http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/</link>
		<comments>http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 17:40:48 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[ML]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/</guid>
		<description><![CDATA[Spent last night getting a crash course in using OCaml to do non-functional things (hash tables, file I/O, regular expressions) and the result is now up.
The JoCaml version does the file-partitioning trick used in the C++ implementation, with each finder workers being run inside a JoCaml channel; the channels share a single lock so they [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Spent last night getting a crash course in using OCaml to do non-functional things (hash tables, file I/O, regular expressions) and the result <a href="http://hircus.org/widefinder/widefinder-ml.tar.bz2">is now up</a>.</p>
<p>The JoCaml version does the file-partitioning trick used in the C++ implementation, with each finder workers being run inside a JoCaml channel; the channels share a single lock so they can update the hash table serially.</p>
<p>Interestingly, current implementation does not get a speed-up from the input file being cached (Ilmari&#8217;s <a href="http://fhtr.blogspot.com/2007/10/wide-finder-ocaml.html">wf.ml</a> does). Will have to peruse his to see what&#8217;s slowing things down.</p>
<p>Lesson: not all techniques for processing a file line-wise are equally good!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/214/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/214/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=214&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Wide Finder: C++ update</title>
		<link>http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/</link>
		<comments>http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/#comments</comments>
		<pubDate>Tue, 16 Oct 2007 21:15:21 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/</guid>
		<description><![CDATA[Talked with a colleague about the slow single-threaded performance of my Wide Finder implementation, and we narrowed it down to two possibilities:

Boost regular expression is not compiled?
C++ strings have higher overhead than null-terminated c_str

First point can be ruled out: Boost compiles regular expressions when you assign them. Second point &#8212; well, reading in the file [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Talked with a colleague about the slow single-threaded performance of my Wide Finder implementation, and we narrowed it down to two possibilities:</p>
<ul>
<li>Boost regular expression is not compiled?</li>
<li>C++ strings have higher overhead than null-terminated <tt>c_str</tt></li>
</ul>
<p>First point can be ruled out: Boost compiles regular expressions when you assign them. Second point &#8212; well, reading in the file using <tt>std::getline</tt> turns out to consume the bulk of time.</p>
<p>I&#8217;ve reorganized the code a bit, using a multimap rather than a vector to rank the URLs by count, with no effect on speed. With two and four threads on a dual-core Intel notebook, the performance is at least on par with Ruby.</p>
<p><a href="http://girtby.net/archives/2007/10/9/wide-finder-in-c">Alastair Rankine</a> has a C++ implementation that is slightly faster, but uses Boost memory-mapped IO that I avoided for the same reason he put as caveat: that it will not scale to files that are too large. Which Tim&#8217;s log file might well be. Again, that is not significantly faster than the Ruby code.</p>
<p>Moral of the question: Perl and Ruby can be faster than C++! The C implementations out there are blindingly fast, but the way they do regular expression handling are really painful.</p>
<p>Will turn my (limited) spare time to doing a clean JoCaml implementation &#8212; it might not be faster but it definitely will look cleaner!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/213/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/213/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/213/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=213&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>JoCaml</title>
		<link>http://hircus.wordpress.com/2007/10/15/jocaml/</link>
		<comments>http://hircus.wordpress.com/2007/10/15/jocaml/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 23:36:20 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[ML]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/15/jocaml/</guid>
		<description><![CDATA[After turning in the C/C++ monster (cleanest C code I reckon it is possible to write, thus the total lack of memory-mapped I/O and other optimizations), I turned my attention to picking a better implementation language.
Requirements:

Functional
Good support for threading
If possible, support for distributed computing

As it turns out, JoCaml fits the bill perfectly. It&#8217;s an extension [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After turning in the C/C++ monster (cleanest C code I reckon it is possible to write, thus the total lack of memory-mapped I/O and other optimizations), I turned my attention to picking a better implementation language.</p>
<p>Requirements:</p>
<ul>
<li>Functional</li>
<li>Good support for threading</li>
<li>If possible, support for distributed computing</li>
</ul>
<p>As it turns out, JoCaml fits the bill perfectly. It&#8217;s an extension of Ocaml, so it combines a rich library with a familiar syntax (not to me, but having used both Scheme and Haskell, how different can it be) &#8212; and a very nice process calculus!</p>
<p>Example: this is a concurrent stack that blocks if there is no input available</p>
<blockquote><p>
let new_stack () =<br />
  def state (s) &amp; push (v) = state (v::s) &amp; reply to push<br />
    or state (x::s) &amp; pop () = state s &amp; reply x to pop in<br />
  spawn state([]);<br />
  pop, push<br />
;;
</p></blockquote>
<p>This defines a private <tt>state</tt> channel, and then export the <tt>pop</tt> and <tt>push</tt> synchronous channels (that to the user behave just like ordinary functions)</p>
<p>and this is how you use it:</p>
<blockquote><p>
let pop, push = new_stack ();;<br />
spawn echo(pop());;<br />
push(1);;
</p></blockquote>
<p>Note that the <tt>echo</tt> channel will block, since pop can&#8217;t return a value until the stack contains something! This value is then pushed into the stack and &#8216;1&#8242; printed.</p>
<p>More of this at the <a href="http://jocaml.infria.fr/">JoCaml site</a>. And, as it turns out, there already is <a href="http://fhtr.blogspot.com/2007/10/wide-finder-ocaml.html">a JoCaml implementation of the Wide Finder</a>, by Ilmari Heikkinen. Will have to grok the finer details from him.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/212/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/212/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/212/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=212&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/15/jocaml/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Tim Bray&#8217;s Wide Finder: a minimalist implementation</title>
		<link>http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/</link>
		<comments>http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/#comments</comments>
		<pubDate>Fri, 12 Oct 2007 23:45:21 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[boost]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[pthreads]]></category>

		<category><![CDATA[tim-bray]]></category>

		<category><![CDATA[wide-finder]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/</guid>
		<description><![CDATA[Several weeks ago, Tim Bray posted his Wide Finder project: take the Ruby script that parses an Apache log file and report the top 10 hits, and parallelize it in your language of choice.
It occurred to me a while back that this is a perfect job for a C multi-process program, taking advantage of Linux&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Several weeks ago, Tim Bray posted his Wide Finder project: take the Ruby script that parses an Apache log file and report the top 10 hits, and parallelize it in your language of choice.</p>
<p>It occurred to me a while back that this is a perfect job for a C multi-process program, taking advantage of Linux&#8217;s cheap copy-on-write <tt>fork</tt>, if not for the need to merge the result. So it would probably be easier to write it using the <tt>pthreads</tt> library instead. One would want to reduce inter-thread communication as much as possible, though.</p>
<p>I did not have time to touch the code until today, but now it&#8217;s done. Two key insights:</p>
<ul>
<li>The input file can be partitioned cleanly into multiple chunks of roughly identical sizes without communication, as long as each thread follows the same protocol. i.e. a common chunk size is used, and all but the first thread needs to check if the character before their starting offset is the end-of-line. If not, they need to skip until the end of line, yielding that line to the previous thread.</li>
<li>When merging the hash maps, the first thread to acquire the lock does not need to insert its items one by one, but can just set the main hash map to point to its own</li>
</ul>
<p>On a data set size of 200MB, with a single thread, performs slightly slower as Ruby by wall clock:</p>
<p>Ruby timing<br />
<code><br />
2.99user 1.88system 0:05.26elapsed 92%CPU (0avgtext+0avgdata 0maxresident)k<br />
152inputs+0outputs (3major+853minor)pagefaults 0swaps<br />
3.04user 1.84system 0:05.33elapsed 91%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+856minor)pagefaults 0swaps<br />
2.84user 2.01system 0:05.19elapsed 93%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+856minor)pagefaults 0swaps<br />
</code></p>
<p>C++ timing, one thread<br />
<code><br />
2.70user 5.21system 0:07.95elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+655minor)pagefaults 0swaps<br />
2.78user 5.14system 0:07.96elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+655minor)pagefaults 0swaps<br />
2.72user 5.12system 0:07.90elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+654minor)pagefaults 0swaps<br />
</code></p>
<p>With two threads, though, it runs faster, almost at Ruby speed.</p>
<p>C++ timing, two threads<br />
<code><br />
3.33user 5.40system 0:05.51elapsed 158%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+675minor)pagefaults 0swaps<br />
3.40user 5.33system 0:05.86elapsed 148%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+672minor)pagefaults 0swaps<br />
3.41user 5.36system 0:05.60elapsed 156%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+674minor)pagefaults 0swaps<br />
</code></p>
<p>So, as Tim observed, the problem is not entirely IO-bound. More testing is needed, but will probably need to be done on a machine with faster IO (and more CPUs). Like Tim&#8217;s new Niagara T2 testbed.</p>
<p>Will edit this post later &#8212; need to run now, and wanted to get this out as soon as possible. Code is available  <a href="http://hircus.org/widefinder/">here</a>&#8230; now I just need to find Tim&#8217;s email, since his blog is down. Argh!</p>
<p><b>Update:</b> switched to the new <tt>std::tr1::unsorted_map</tt>. Performance seems identical with a single thread, but slightly higher (within margin of error) on two threads &#8212; which makes sense: a multithreaded run exercises the map more, because each thread&#8217;s map has to be merged into the final map.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/211/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/211/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=211&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Stream module for Lua</title>
		<link>http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/</link>
		<comments>http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/#comments</comments>
		<pubDate>Sat, 18 Nov 2006 18:06:41 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Lua]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/</guid>
		<description><![CDATA[&#8220;Be not afraid of greatness: some are born great, some achieve greatness and some have greatness thrust upon them&#8221;.
- (Shakespeare, W., Twelfth Night, Quote Act II, Scene V).
&#8220;Some men are born mediocre, some men achieve mediocrity, and some men have mediocrity thrust upon them. With Major Major it had been all three&#8221;.
- (Heller, J., Catch-22, [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><em>&#8220;Be not afraid of greatness: some are born great, some achieve greatness and some have greatness thrust upon them&#8221;.</em><br />
- (<strong>Shakespeare, W.</strong>, <em>Twelfth Night</em>, Quote Act II, Scene V).</p></blockquote>
<blockquote><p><em>&#8220;Some men are born mediocre, some men achieve mediocrity, and some men have mediocrity thrust upon them. With Major Major it had been all three&#8221;.</em><br />
- (<strong>Heller, J.</strong>, <em>Catch-22</em>, Quote Page 94 (paperback)).</p></blockquote>
<blockquote><p><em>&#8220;Some languages are born lazy, some languages achieve laziness, and some languages have laziness thrust upon them&#8221;.</em><br />
- (<strong>Salim, M.</strong>, unpublished)</p>
<p>So you can probably guess what this is about. A stream library for Lua, that allows you to do things like running the Sieve of Eratosthenes without specifying an upper bound, lazily filtering the set of <em>all</em> positive integers as you go along.</p>
<p><span id="more-192"></span><br />
Witness the use of the library:</p>
<blockquote>
<pre>
require("stream")

function ints(n)
   return {
      head = function () return n end,
      tail = function () return ints(n+1) end }
end

function primes (sints)
   local curprime = false
   local tailthunk = false
   local fhead =
      function ()
	 if curprime then return curprime
	 else
	    curprime = sints.head()
	    tailthunk = sints.tail
	    return curprime
	 end
      end
   local ftail =
      function ()
	 if (not tailthunk) then
	    fhead()
	 end
	 return primes(
		 stream.filter(function(n) return not (n%curprime == 0) end,
			       tailthunk() ))
      end
   return {head=fhead, tail=ftail}
end

-- print the first 100 primes
stream.print(100, primes(ints(2)))</pre>
</blockquote>
<p>A stream is a table containing a head and a tail; both are thunks, so you call them with no arguments to obtain the value. Note the caching that avoids recomputation, similar caching is done in the functions provided by the stream library (like stream.filter)</p>
<p>Code available <a href="http://hircus.org/lua/stream.lua">on hircus.org</a>.</p>
<p>And some benchmarks:</p>
<table border="1">
<tr align="center">
<th></th>
<th width="35%">Lua</th>
<th width="35%">Haskell</th>
</tr>
</table>
<p>The Haskell code was run using GHC&#8217;s &#8216;runhaskell&#8217;, which does not precompile the code, to be fair to Lua which is interpreted. Not bad when put up against a language that is lazy by design (and thus optimized for it)! Timing taken using the &#8216;time&#8217; utility on an Intel Core 2 Duo 1.66 GHz laptop which was mostly idle.</p></blockquote>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/192/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/192/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/192/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=192&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Effectively functional: swapping tree leaves with streams</title>
		<link>http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/</link>
		<comments>http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/#comments</comments>
		<pubDate>Tue, 18 Apr 2006 20:40:00 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/</guid>
		<description><![CDATA[(Thanks to Roshan for posing the task)
Given two binary trees, and a depth-first-search traversal ordering of the leaf nodes, produce two new trees with the same structures as the originals, but with the leaf nodes swapped. If there are more leaf nodes in one tree than the other, the remaining leaf nodes are copied unchanged.
So [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>(Thanks to <a href="http://www.thinkingms.com/pensieve">Roshan</a> for posing the task)</p>
<p>Given two binary trees, and a depth-first-search traversal ordering of the leaf nodes, produce two new trees with the same structures as the originals, but with the leaf nodes swapped. If there are more leaf nodes in one tree than the other, the remaining leaf nodes are copied unchanged.</p>
<p>So (swap-leaves &#8216;(1 . (2 . 3)) &#8216;(a . ((b . c) . d)))<br />
=&gt; &#8216;((a . (b . c)) &#8216;(1 . ((2 . 3) . d))</p>
<p><span id="more-189"></span><br />
Waiving the requirement that the program be completely functional, my original thought was <em>I could probably just modify my tree walker to do this</em>. This <tt>treewalk</tt> procedure takes a tree and produces a stream of leaf node values. Changing it so that it returns a pair of the value, and a setter procedure that can modify the value in place, is trivial: the recursive auxiliary procedure for treewalk is given the parent node, so it can use <tt>set-car!</tt> or <tt>set-cdr!</tt> (depending on which side of the parent node this leaf node is) to update the parent:</p>
<blockquote>
<pre>
(define treewalk
  (lambda (t)
    (if (not (pair? t))
        (stream-cons
          `(t . (lambda (v)
                  (error "Cannot modify singleton")))
          empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))

(define treewalk^
  (lambda (t left? parent)
    (if (not (pair? t))
        (stream-cons `(,t . ,(if left?
                               (lambda (v)
                                 (set-car! parent v))
                               (lambda (v)
                                 (set-cdr! parent v))))
                     empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))</pre>
</blockquote>
<p>The swapping is then just a matter of calling the setter for node1 with the value of node2, and vice-versa; this generalizes to swapping between multiple trees as well.</p>
<blockquote>
<pre>(define treewalk
  (lambda (t)
    (if (not (pair? t))
        (stream-cons `(t . (lambda (v)
                             (error "Cannot modify singleton")))
                     empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))

(define treewalk^
  (lambda (t left? parent)
    (if (not (pair? t))
        (stream-cons `(,t . ,(if left?
                               (lambda (v)
                                 (set-car! parent v))
                               (lambda (v)
                                 (set-cdr! parent v))))
                     empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))</pre>
</blockquote>
<p>But this modifies existing trees in place, a big no-no in functional programming land (though imperative programmers live with this every day). Is there a way to do this in, if not a fully functional way, then in a way that the side-effects are confined? (I mentioned to Roshan that this is a dual of Haskell&#8217;s monads: it <em>behaves functionally</em> even though it is imperative; monads gave the illusion of imperative behaviour even though they are fully functional. He pointed out that internally the state monad is optimized into imperative calls as well, so the comparison is even more interesting)</p>
<p>Instead of modifying existing trees in place, we want to copy the trees. But copying, naively done, would involve traversing the trees again, and that would be inefficient. The copying should be done <em>Lazily</em> as needed, and with streams, that means doing it while you&#8217;re walking the tree and collecting the nodes!</p>
<p>In short, at every internal node,<br />
- create a new node, which has as its children the children of the original node;<br />
- update the <em>clone</em> of the parent, instead of the parent, so that its car or cdr points to this new node<br />
- the setter procedures for each leaf nodes modify these cloned nodes instead of the originals</p>
<blockquote>
<pre>...
(let ([copy (cons (car t) (cdr t))])
  (if left?
    (set-car! copy-parent copy)
    (set-cdr! copy-parent copy))
  (stream-append (treewalk-copy^ (car t) #t copy)
                 (treewalk-copy^ (cdr t) #f copy))
...</pre>
</blockquote>
<p>The main <tt>treewalk</tt> procedure now has to return the cloned tree as well, since at each subsequent stage of the walk only the direct parent is known, not the entire tree.</p>
<blockquote>
<pre>...
(let ([copy (cons (car t) (cdr t))])
       (if left?
           (set-car! copy-parent copy)
           (set-cdr! copy-parent copy))
  (stream-append (treewalk-copy^ (car t) #t copy)
                 (treewalk-copy^ (cdr t) #f copy))
...</pre>
</blockquote>
<p>Code, including stream implementation, available <a href="http://www.cs.indiana.edu/~msalim/scheme/swap-leaves.scm">here</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/189/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/189/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/189/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=189&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>LiteratePrograms</title>
		<link>http://hircus.wordpress.com/2006/03/04/literateprograms/</link>
		<comments>http://hircus.wordpress.com/2006/03/04/literateprograms/#comments</comments>
		<pubDate>Sat, 04 Mar 2006 11:55:00 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Pedagogy]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/03/04/literateprograms/</guid>
		<description><![CDATA[This being mid-term week for course I am a TA for, the lab sections I taught yesterday were made optional, and so apart from handling several questions on exam questions and assignments, I had some free time.
That time was spent reading Lambda the Ultimate, which was how I noticed the LiteratePrograms wiki. Based on Wikipedia&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This being mid-term week for course I am a TA for, the lab sections I taught yesterday were made optional, and so apart from handling several questions on exam questions and assignments, I had some free time.</p>
<p>That time was spent reading <a href="http://lambda-the-ultimate.org/">Lambda the Ultimate</a>, which was how I <a href="http://lambda-the-ultimate.org/node/1336">noticed</a> the <a href="http://literateprograms.org/LiteratePrograms:Welcome">LiteratePrograms</a> wiki. Based on Wikipedia&#8217;s MediaWiki, with added feature from the noweb literate programming system, it is to collaborative programming what other wikis are to collaborative writing. You can declare code blocks, and if the language is supported get syntax highlighting for free; the code is automatically packaged into a zip archive everytime someone hits the download link.</p>
<p>Check it out. But if you&#8217;re teaching a programming course, don&#8217;t mention it to your students <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/188/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/188/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/188/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=188&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/03/04/literateprograms/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Python tail-call decorator</title>
		<link>http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/</link>
		<comments>http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/#comments</comments>
		<pubDate>Thu, 02 Mar 2006 22:36:00 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/</guid>
		<description><![CDATA[Two days ago, this tail call decorator
 by Crutcher Dunnavant was mentioned on LtU. It&#8217;s a clever little trick: the decorator returns a new function that, when called, checks if its grandparent
is itself. This would mean that a function declared to be tail-call-optimized is called from another tail-called-optimized function, and so the caller&#8217;s frame in [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Two days ago, this <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474088">tail call decorator<br />
</a> by Crutcher Dunnavant was <a href="http://lambda-the-ultimate.org/node/1331">mentioned</a> on LtU. It&#8217;s a clever little trick: the decorator returns a new function that, when called, checks if its <em>grandparent</em><br />
is itself. This would mean that a function declared to be tail-call-optimized is called from another tail-called-optimized function, and so the caller&#8217;s frame in the stack can be wiped. How does it do this? The function <em>throws</em> an exception. This is caught by the grandparent, which then calls the saved function with the arguments passed in the exception.</p>
<p>This is the <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474088">original </a>implementation:</p>
<blockquote>
<pre>

#!/usr/bin/env python2.4
# This program shows off a python decorator(
# which implements tail call optimization. It
# does this by throwing an exception if it is
# it's own grandparent, and catching such
# exceptions to recall the stack.

import sys

class TailRecurseException:
  def __init__(self, args, kwargs):
    self.args = args
    self.kwargs = kwargs

def tail_call_optimized(g):
  """
  This function decorates a function with tail call
  optimization. It does this by throwing an exception
  if it is it's own grandparent, and catching such
  exceptions to fake the tail call optimization.

  This function fails if the decorated
  function recurses in a non-tail context.
  """
  def func(*args, **kwargs):
    f = sys._getframe()
    if f.f_back and f.f_back.f_back         and f.f_back.f_back.f_code == f.f_code:
      raise TailRecurseException(args, kwargs)
    else:
      while 1:
        try:
          return g(*args, **kwargs)
        except TailRecurseException, e:
          args = e.args
          kwargs = e.kwargs
  func.__doc__ = g.__doc__
  return func

@tail_call_optimized
def factorial(n, acc=1):
  "calculate a factorial"
  if n == 0:
    return acc
  return factorial(n-1, n*acc)

print factorial(10000)
# prints a big, big number,
# but doesn't hit the recursion limit.

@tail_call_optimized
def fib(i, current = 0, next = 1):
  if i == 0:
    return current
  else:
    return fib(i - 1, next, current + next)

print fib(10000)
# also prints a big number,
# but doesn't hit the recursion limit.</pre>
</blockquote>
<p>There is a subtle bug: after handling the exception, <em>func</em> then calls its own saved version of  <em>g</em>, not the <em>g</em> of the <em>func</em> that throws the exception. Modifying <em>func</em> and the <strong>TailRecurseException</strong> class so that the function to be called can be passed in the exception solves this nicely:</p>
<blockquote>
<pre>
class TailRecurseException:
  def __init__(self, g, args, kwargs):
    self.g = g
    self.args = args
    self.kwargs = kwargs

  ...
  def func(*args, **kwargs):
    f = sys._getframe()
    if f.f_back and f.f_back.f_back         and f.f_back.f_back.f_code == f.f_code:
      raise TailRecurseException(g, args, kwargs)
    else:
      newg = g
      while 1:
        try:
          return newg(*args, **kwargs)
        except TailRecurseException, e:
          newg = e.g
          args = e.args
          kwargs = e.kwargs
  ...</pre>
</blockquote>
<p>Throwing an exception just to handle the required stack manipulation because the interpreter won&#8217;t handle it is grossly inefficient, granted, and requiring the programmer to declare functions as optimizable is not be ideal, but still a really neat trick.</p>
<p>As a side note, if a function can be called in tail and non-tail position, a non-decorated version should be made available, otherwise bad things can happen:</p>
<blockquote>
<pre>
@tail_call_optimized
def add1(n):
  return (n+1)

@tail_call_optimized
def add2(n):
  return add1(add1(n))</pre>
</blockquote>
<p>This returns 41.</p>
<p>Modified version, and test cases, available <a href="http://www.cs.indiana.edu/%7Emsalim/python/tailcall2.py">here</a> [IUCS] (backup copy <a href="http://hircus.org/python/tailcall2_py.txt">here</a>).</p>
<p><span style="font-weight:bold;">Update</span>: added <a href="http://lambda-the-ultimate.org/node/1331#comment-15183">isTailCall</a> by jorend. Now it is safe to optimize every function (though for efficiency reason you&#8217;d only optimize the ones that make tail calls).</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/187/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/187/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/187/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=187&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Whorfianism in PL design</title>
		<link>http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/</link>
		<comments>http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/#comments</comments>
		<pubDate>Sun, 12 Feb 2006 19:34:00 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Linguistics]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/</guid>
		<description><![CDATA[While the aptness of the Whorfian hypothesis is still a highly-contentious issue when it comes to natural languages, the hypothesis more or less confirms itself again and again in the realms of programming languages. Certain programming styles are simply more suited to certain languages: you don&#8217;t try to use higher-order functions in C, after all; [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>While the aptness of the Whorfian hypothesis is still a highly-contentious issue when it comes to natural languages, the hypothesis more or less confirms itself again and again in the realms of programming languages. Certain programming styles are simply more suited to certain languages: you don&#8217;t try to use higher-order functions in C, after all; the lack of nested functions make this idea unworkable.</p>
<p>Which makes languages like <a href="http://scala.epfl.ch/">Scala</a> exciting. It&#8217;s fully functional, and also fully object-oriented.Integers are objects. Functions? Also objects. The List ADT familiar to functional programmer is now an Object, with methods such as fold and filter defined on it (which makes the syntax a bit confusing, but not if you come from Ruby. Or your first language is japanese. But I digress)</p>
<p>Best of both worlds? Well, not quite. The new Scala compiler looks promising, but it still trips up on this simple mutual-recursion example:</p>
<blockquote>
<pre>
object RecEvenOdd {
  def rec_oddp(n: Int): Boolean =
    { if (n == 0) false else rec_evenp(n-1) }
  def rec_evenp(n: Int): Boolean =
    { if (n == 0 ) true else rec_oddp(n-1) }
  def rec_oddevenp(flag: Boolean, n: Int): Boolean = {
    if (n == 0) flag else rec_oddevenp(!flag, n-1)
  }

  def main(args: Array[String]): unit = {
    val n: Int = Integer.parseInt(args(0));
    System.out.println("recursive: " + n + ": " +       rec_oddevenp(true, n));
    System.out.println("mutual rec: " + n + ": " +       rec_evenp(n));
  }
}</pre>
</blockquote>
<p>The former gets detected as a tail call and optimized. The second? No such luck.. yet.</p>
<p>Of further note is the implementation of streams: Scala implements the &#8220;odd&#8221; style of lazy programming (Wadler et al., <a href="http://cm.bell-labs.com/who/wadler/papers/lazyinstrict/lazyinstrict.ps.gz"><em>How to add laziness to a strict language without even being odd</em></a>); the provided implementation is very similar to the reference implementation of streams for Scheme in <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html">SICP</a>. The following program will thus unnecessarily compute the square root of -1:</p>
<blockquote>
<pre>
object WadlerTest {
  def countdown(n: Int): Stream[Int] =
    Stream.cons(n, countdown(n-1));
  def sqrt(n: Double): Double = {
    System.out.println("sqrt: called with " + n);
    Math.sqrt(n);
  }
  def main(args: Array[String]): unit = {
    countdown(4) map { n =&gt; sqrt(n) } take(5) print
  }
}</pre>
</blockquote>
<p>The syntax can also be bizarre at times, a motley of Java-isms and proper FP style. For example,</p>
<pre>{ n =&gt; sqrt(n) }</pre>
<p>is η-reducible to sqrt, but Scala would not compile it with this definition of sqrt. Rewriting  sqrt thus:</p>
<blockquote>
<pre>
  def sqrt = { n: Double =&gt;
  ...</pre>
</blockquote>
<p>now sqrt can be passed to map - but the automatic cast from Int to Double is no longer performed.  Eek.</p>
<p>Moral of the story? I&#8217;m learning Haskell.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/186/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/186/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=186&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Static (lexical) vs dynamic scoping</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/</link>
		<comments>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comments</comments>
		<pubDate>Thu, 02 Jun 2005 22:20:00 +0000</pubDate>
		<dc:creator>hircus</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/</guid>
		<description><![CDATA[Eric and I were discussing scoping in Scheme and Python earlier today, our third over the past few weeks - and we finally nailed it shut. The first time he brought up dynamic scoping in Common Lisp and how Prof. Friedman dislikes it; the second was on how Python appears to have dynamic scoping (which [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Eric and I were discussing scoping in Scheme and Python earlier today, our third over the past few weeks - and we finally nailed it shut. The first time he brought up dynamic scoping in Common Lisp and how Prof. Friedman dislikes it; the second was on how Python appears to have dynamic scoping (which turns out to be true, pre-Python 2.2), and now, thanks to Wikipedia, I think we have it right.</p>
<p>Provided Eric gets the H211 Introduction to Programming (Honors) class, which is in Python, and I get the C211 Introduction to Programming (Scheme), our discussion should stand us in good steed, though funnily today I played the Python guy and he played the Scheme one.</p>
<p>I&#8217;m going to show the examples, both in Scheme and Python; the first one in each section would appear to show that the language in question features dynamic scoping, which is incorrect as both actually do lexical scoping.</p>
<p>Scheme:<br />
Bad:</p>
<pre>
(let ((pi 3.1415))
 (define area
  (lambda (r)
   (* pi r r)))
 (display (area 10))
 (newline)
 (set! pi 3)
 (display (area 10)))</pre>
<p>Good:</p>
<pre>
(let ((pi 3.1415))
 (define area
  (lambda (r)
   (* pi r r)))
 (display (area 10))
 (newline)
 (let ((pi 3))
  (display (area 10))))</pre>
<p>Python:<br />
Bad:</p>
<pre>
pi = 3.1415
def area(r):
 return pi*r*r
area(10)
pi = 3
area(10)</pre>
<p>Good:</p>
<pre>
pi = 3.1415
pi_holder = 10
def create_area():
 pi_holder = pi  # local pi_holder, different from pi_holder outside
 def area(r):
  return pi_holder*r*r
 return area
area = create_area()
area(10)    # 314.15
pi_holder    # Still 10
pi_holder = 3
area(10)    # Still 314.15</pre>
<p>The above works, but is a bit problematic. I introduced <tt>pi_holder=10</tt> to show that, (1), pi_holder inside of <tt>create_area()</tt> is a local variable; (2), that this local pi_holder is the one that is in <tt>area</tt>&#8217;s scope, and thus changing the value of pi_holder does not affect it.</p>
<p>Isn&#8217;t it easier to just do pi = pi ? Well, that does not work. My initial hunch was that Python reads the LHS of the expression, decided pi has been redeclared as a local variable, and thus since it&#8217;s not been initialized it got confused trying to assign it the value of itself. But it&#8217;s actually worse; this code does not work either:</p>
<pre>
x = 42
def local_var_test():
 temp = x
 print temp  # 42
 x = temp
 print x   # 42?</pre>
<p>Surprise! Python won&#8217;t let you do that either. Take out the last two lines and the code works, though. Basically, if in the block a variable is declared <em>anywhere</em>, it is a local variable everywhere in that block, and trying to refer to a variable declared in the surrounding scope, even before the local declaration, will fail.</p>
<p>But this is where default parameters come in handy. A better way to rewrite the clunky code above is as follows:</p>
<pre>
pi = 3.1415
def create_area(pi = pi):
 def area(r):
  return pi*r*r
 return area
area = create_area()
area(10)    #314.15
pi = 3
area(10)    #314.15</pre>
<p>So Python has static scoping after all. The thing to bear in mind is that Scheme functions are named closures, while Python functions inherit the surrounding scope, so to freeze the variables you depend on you have to wrap your function definition inside another function that copies in the values you need into its local variables.</p>
<p>References:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Python_programming_language#Closures">Python programming language (Wikipedia): Closures</a></li>
<li><a href="http://en.wikipedia.org/wiki/Scope_%28programming%29">Scope (Wikipedia)</a></li>
</ul>
<p>And the funny thing is, I started the day trying to find good dynamic languages that run on the Java platform (platform envy, I guess, since .NET more prominently touts its language neutrality). Sun&#8217;s finally catching up, though - Tim Bray <a href="http://www.tbray.org/ongoing/When/200x/2005/03/15/Coyote">wrote</a> a few months back about the <a href="https://coyote.dev.java.net/">Coyote</a> project to support dynamic languages in Sun&#8217;s open source IDE, NetBeans, and pointed to an interesting Sun-developed scripting language, <a href="https://pnuts.dev.java.net/">Pnuts</a>. Which reminded me of Groovy and Boo.</p>
<p>Googling for <em>groovy boo .net</em> - Groovy being a Ruby-like scripting language for Java that received a lot of attention a few months ago, and then taken some flak over its development model, and Boo being the Python-like language for .NET - yields this <a href="http://developers.slashdot.org/article.pl?sid=05/03/30/1932209&amp;tid=156&amp;tid=8">very interesting Slashdot discussion</a> that led me to such intriguing functional OO languages as <a href="http://scala.epfl.ch/">Scala</a> and <a href="http://nice.sourceforge.net">Nice</a>. .NET fans do not get to have all the fun!</p>
<p>Groovy, on the other hand, seems <a href="http://www.pyrasun.com/mike/mt/archives/2005/01/09/20.57.06/">rather</a> <a href="http://www.manageability.org/blog/stuff/what-is-wrong-with-groovy/view">disappointing</a>. Oh well. Scala looks more like Haskell, but with dynamic type inference (like Boo).. yay!</p>
<p><strong>Update</strong> - <em>2005/06/06</em></p>
<p>Realized a few days ago, but haven&#8217;t gotten round to posting about it, that I was unfairly comparing Scheme and Python, and that Python methods <em>are</em> closures in themselves. Note:</p>
<pre>
pi = 3.1415
def area(r):
 return pi*r*r
print area(10)  # 314.15
def test():
 pi = 3
 print area(10) # 314.15
test()</pre>
<p>In the earlier example, overriding the value of pi with <tt>pi = 3</tt> is the equivalent of doing <tt>(set! pi 3)</tt> in Scheme, i.e. it will change the value of the variable that both the top-level pi, which is the one that area knows. In a dynamic scope, which uses a stack to figure out which value assignment should apply, <tt>pi = 3</tt> would affect the call to <tt>area</tt> just after it.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/180/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/180/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=180&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/hircus-128.jpg" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
	</channel>
</rss>