<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<link rel="self" href="/Atom/" />
	<id>http://billmill.org/</id>
	<title>My Name Rhymes</title>
	<subtitle>Bill Mill blogs irregularly</subtitle>
	<updated>2008-03-19T00:14:00Z</updated>
	<author>
		<name>Bill Mill</name>
		<email>bill.mill@gmail.com</email>
		<uri>http://billmill.org/</uri>
	</author>
	<link href="http://billmill.org/" />
	<entry>
		<title>Pong in 30 Lines</title>
		<link href="http://billmill.org/pong.html" />	
		<id>http://billmill.org/pong.html</id>
		<updated>2008-03-19T00:14:00Z</updated>
		<summary type="html">&lt;a href="http://nodebox.net"&gt;NodeBox&lt;/a&gt; rocks. Copy this code into the 
nodebox window and hit apple-R to play pong.
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;size(&lt;span style="color: #009999"&gt;400&lt;/span&gt;,&lt;span style="color: #009999"&gt;400&lt;/span&gt;)
speed(&lt;span style="color: #009999"&gt;40&lt;/span&gt;)

ball_diameter &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;20&lt;/span&gt;
paddle_size &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;75&lt;/span&gt;
v_x, v_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (&lt;span style="color: #009999"&gt;3&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;)
p_x, p_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (&lt;span style="color: #009999"&gt;10&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;)
bounce &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;1.2&lt;/span&gt;
points &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; []
computer &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; WIDTH &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;
compuspeed &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;10&lt;/span&gt;

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;draw&lt;/span&gt;():
    &lt;span style="font-weight: bold"&gt;global&lt;/span&gt; v_x, v_y, p_x, p_y, points, bounce, computer, compuspeed
    
    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="font-weight: bold"&gt;not&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;ball_diameter &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; p_y &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; HEIGHT &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; ball_diameter:
        text(&lt;span style="color: #bb8844"&gt;&amp;quot;Game Over&amp;quot;&lt;/span&gt;, WIDTH&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;, HEIGHT&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;)
        &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; p_y &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt;: text(&lt;span style="color: #bb8844"&gt;&amp;quot;You win!&amp;quot;&lt;/span&gt;, WIDTH&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;, HEIGHT&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;&lt;span style="font-weight: bold"&gt;+&lt;/span&gt;&lt;span style="color: #009999"&gt;20&lt;/span&gt;)
        &lt;span style="font-weight: bold"&gt;else&lt;/span&gt;:       text(&lt;span style="color: #bb8844"&gt;&amp;quot;Computer wins&amp;quot;&lt;/span&gt;, WIDTH&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;, HEIGHT&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;&lt;span style="color: #009999"&gt;20&lt;/span&gt;)
        &lt;span style="font-weight: bold"&gt;return&lt;/span&gt;

    paddle_left &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #999999"&gt;min&lt;/span&gt;(&lt;span style="color: #999999"&gt;max&lt;/span&gt;(MOUSEX, &lt;span style="color: #009999"&gt;0&lt;/span&gt;), WIDTH&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;paddle_size)
    ny &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; p_y &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; v_y
    nx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; p_x &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; v_x
    
    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; (ball_diameter&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; paddle_size: computer &lt;span style="font-weight: bold"&gt;+=&lt;/span&gt; compuspeed
    &lt;span style="font-weight: bold"&gt;elif&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; computer:                                 computer &lt;span style="font-weight: bold"&gt;-=&lt;/span&gt; compuspeed
    rect(computer, &lt;span style="color: #009999"&gt;0&lt;/span&gt;, paddle_size, &lt;span style="color: #009999"&gt;4&lt;/span&gt;, roundness&lt;span style="font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;)

    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; ny &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; ball_diameter &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; HEIGHT &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; v_y &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; \
    &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; paddle_left &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; (ball_diameter &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; paddle_left &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; paddle_size:
        v_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;v_y &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; bounce
        v_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (nx &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; paddle_left &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; (paddle_size &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;)) &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; &lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #009999"&gt;25&lt;/span&gt;
        ny &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; HEIGHT &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; ball_diameter
    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; ny &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; v_y &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; \
    &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; (ball_diameter &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; paddle_size:
        v_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;v_y &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; bounce
        v_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (nx &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; (paddle_size &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;)) &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; &lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #009999"&gt;25&lt;/span&gt;
        ny &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt;
    &lt;span style="font-weight: bold"&gt;elif&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; ball_diameter &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; WIDTH &lt;span style="font-weight: bold"&gt;or&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt;:
        v_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;v_x
        
    mx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #999999"&gt;max&lt;/span&gt;(&lt;span style="color: #009999"&gt;0&lt;/span&gt;, &lt;span style="color: #999999"&gt;min&lt;/span&gt;(MOUSEX, WIDTH &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; paddle_size))
    rect(mx, HEIGHT&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;&lt;span style="color: #009999"&gt;4&lt;/span&gt;, paddle_size, &lt;span style="color: #009999"&gt;4&lt;/span&gt;, roundness&lt;span style="font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;)

    p_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; nx
    p_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; ny
    oval(p_x, p_y, ball_diameter, ball_diameter)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;p&gt;I'd never written a game before, and they say you aren't allowed if you 
don't start with pong, so here it is. I was actually just playing around with 
motion in NodeBox for a seperate project, and did this for fun.
</summary>
		<content type="html">&lt;a href="http://nodebox.net"&gt;NodeBox&lt;/a&gt; rocks. Copy this code into the 
nodebox window and hit apple-R to play pong.
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;size(&lt;span style="color: #009999"&gt;400&lt;/span&gt;,&lt;span style="color: #009999"&gt;400&lt;/span&gt;)
speed(&lt;span style="color: #009999"&gt;40&lt;/span&gt;)

ball_diameter &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;20&lt;/span&gt;
paddle_size &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;75&lt;/span&gt;
v_x, v_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (&lt;span style="color: #009999"&gt;3&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;)
p_x, p_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (&lt;span style="color: #009999"&gt;10&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;)
bounce &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;1.2&lt;/span&gt;
points &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; []
computer &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; WIDTH &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;
compuspeed &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;10&lt;/span&gt;

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;draw&lt;/span&gt;():
    &lt;span style="font-weight: bold"&gt;global&lt;/span&gt; v_x, v_y, p_x, p_y, points, bounce, computer, compuspeed
    
    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="font-weight: bold"&gt;not&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;ball_diameter &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; p_y &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; HEIGHT &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; ball_diameter:
        text(&lt;span style="color: #bb8844"&gt;&amp;quot;Game Over&amp;quot;&lt;/span&gt;, WIDTH&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;, HEIGHT&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;)
        &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; p_y &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt;: text(&lt;span style="color: #bb8844"&gt;&amp;quot;You win!&amp;quot;&lt;/span&gt;, WIDTH&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;, HEIGHT&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;&lt;span style="font-weight: bold"&gt;+&lt;/span&gt;&lt;span style="color: #009999"&gt;20&lt;/span&gt;)
        &lt;span style="font-weight: bold"&gt;else&lt;/span&gt;:       text(&lt;span style="color: #bb8844"&gt;&amp;quot;Computer wins&amp;quot;&lt;/span&gt;, WIDTH&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;, HEIGHT&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;&lt;span style="color: #009999"&gt;20&lt;/span&gt;)
        &lt;span style="font-weight: bold"&gt;return&lt;/span&gt;

    paddle_left &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #999999"&gt;min&lt;/span&gt;(&lt;span style="color: #999999"&gt;max&lt;/span&gt;(MOUSEX, &lt;span style="color: #009999"&gt;0&lt;/span&gt;), WIDTH&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;paddle_size)
    ny &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; p_y &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; v_y
    nx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; p_x &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; v_x
    
    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; (ball_diameter&lt;span style="font-weight: bold"&gt;/&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; paddle_size: computer &lt;span style="font-weight: bold"&gt;+=&lt;/span&gt; compuspeed
    &lt;span style="font-weight: bold"&gt;elif&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; computer:                                 computer &lt;span style="font-weight: bold"&gt;-=&lt;/span&gt; compuspeed
    rect(computer, &lt;span style="color: #009999"&gt;0&lt;/span&gt;, paddle_size, &lt;span style="color: #009999"&gt;4&lt;/span&gt;, roundness&lt;span style="font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;)

    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; ny &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; ball_diameter &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; HEIGHT &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; v_y &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; \
    &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; paddle_left &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; (ball_diameter &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; paddle_left &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; paddle_size:
        v_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;v_y &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; bounce
        v_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (nx &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; paddle_left &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; (paddle_size &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;)) &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; &lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #009999"&gt;25&lt;/span&gt;
        ny &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; HEIGHT &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; ball_diameter
    &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; ny &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; v_y &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; \
    &lt;span style="font-weight: bold"&gt;and&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; (ball_diameter &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; paddle_size:
        v_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;v_y &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; bounce
        v_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; (nx &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; computer &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; (paddle_size &lt;span style="font-weight: bold"&gt;/&lt;/span&gt; &lt;span style="color: #009999"&gt;2&lt;/span&gt;)) &lt;span style="font-weight: bold"&gt;*&lt;/span&gt; &lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #009999"&gt;25&lt;/span&gt;
        ny &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt;
    &lt;span style="font-weight: bold"&gt;elif&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;+&lt;/span&gt; ball_diameter &lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; WIDTH &lt;span style="font-weight: bold"&gt;or&lt;/span&gt; nx &lt;span style="font-weight: bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt;:
        v_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;-&lt;/span&gt;v_x
        
    mx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #999999"&gt;max&lt;/span&gt;(&lt;span style="color: #009999"&gt;0&lt;/span&gt;, &lt;span style="color: #999999"&gt;min&lt;/span&gt;(MOUSEX, WIDTH &lt;span style="font-weight: bold"&gt;-&lt;/span&gt; paddle_size))
    rect(mx, HEIGHT&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;&lt;span style="color: #009999"&gt;4&lt;/span&gt;, paddle_size, &lt;span style="color: #009999"&gt;4&lt;/span&gt;, roundness&lt;span style="font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;2&lt;/span&gt;)

    p_x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; nx
    p_y &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; ny
    oval(p_x, p_y, ball_diameter, ball_diameter)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;p&gt;I'd never written a game before, and they say you aren't allowed if you 
don't start with pong, so here it is. I was actually just playing around with 
motion in NodeBox for a seperate project, and did this for fun.
</content>
	</entry>
	<entry>
		<title>DNA on Programming</title>
		<link href="http://billmill.org/dna.html" />	
		<id>http://billmill.org/dna.html</id>
		<updated>2008-02-16T17:45:47Z</updated>
		<summary type="html">&lt;blockquote&gt;"Well, what we called a computer in 1977 was really a kind of electric abacus, but..."&lt;p&gt;
"Oh, now, don't underestimate the abacus," said Reg. "In skilled hands it's a very sophisticated calculating device. Furthermore it requires no power, can be made with any materials you have to hand, and never goes bing in the middle of an important piece of work."&lt;p&gt;
"So an electric one would be particularly pointless," said Richard.&lt;p&gt;
"True enough," conceded Reg.&lt;p&gt;
"There really wasn't a lot this machine could do that you couldn't do yourself in half the time with a lot less trouble," said Richard, "but it was, on the other hand, very good at being a slow and dim-witted pupil."&lt;p&gt;
Reg looked at him quizzically.&lt;p&gt;
"I had no idea they were in such short supply," he said. "I could hit a dozen of them with a bread roll from where I'm sitting."&lt;p&gt;
"I'm sure. But look at it this way. What really is the point of trying to teach anything to anybody?"&lt;p&gt;
This question seemed to provoke a murmur of sympathetic approval from up and down the table. &lt;p&gt;
    Richard continued, "What I mean is that if you really want to understand something, the best way is to try and explain it to someone else. That forces you to sort it out in your own mind. And the more slow and dim-witted your pupil, the more you have to break things down into more and more simple ideas. And that's really the essence of programming. By the time you've sorted out a complicated idea into little steps that even a stupid machine can deal with, you've certainly learned something about it yourself. The teacher usually learns more than the pupil. Isn't that true?"&lt;p&gt;
    "It would be hard to learn much less than my pupils," came a low growl from somewhere on the table, "without undergoing a pre-frontal lobotomy."&lt;p&gt;
    "So I used to spend days struggling to write essays on this 16K machine that would have taken a couple of hours on a typewriter, but what was fascinating to me was the process of trying to explain to the machine what it was I wanted it to do. I virtually wrote my own word processor in BASIC. A simple search and replace routine would take about three hours."&lt;p&gt;
    "I forget, did you ever get any essays done at all?"&lt;p&gt;
    "Well, not as such. No actual essays, but the reasons why not were absolutely fascinating. For instance, I discovered that..."&lt;p&gt;
    He broke off, laughing at himself.&lt;/blockquote&gt;
-&lt;a href="http://www.amazon.com/Dirk-Gentlys-Holistic-Detective-Agency/dp/0671746723"&gt;DNA&lt;/a&gt;
</summary>
		<content type="html">&lt;blockquote&gt;"Well, what we called a computer in 1977 was really a kind of electric abacus, but..."&lt;p&gt;
"Oh, now, don't underestimate the abacus," said Reg. "In skilled hands it's a very sophisticated calculating device. Furthermore it requires no power, can be made with any materials you have to hand, and never goes bing in the middle of an important piece of work."&lt;p&gt;
"So an electric one would be particularly pointless," said Richard.&lt;p&gt;
"True enough," conceded Reg.&lt;p&gt;
"There really wasn't a lot this machine could do that you couldn't do yourself in half the time with a lot less trouble," said Richard, "but it was, on the other hand, very good at being a slow and dim-witted pupil."&lt;p&gt;
Reg looked at him quizzically.&lt;p&gt;
"I had no idea they were in such short supply," he said. "I could hit a dozen of them with a bread roll from where I'm sitting."&lt;p&gt;
"I'm sure. But look at it this way. What really is the point of trying to teach anything to anybody?"&lt;p&gt;
This question seemed to provoke a murmur of sympathetic approval from up and down the table. &lt;p&gt;
    Richard continued, "What I mean is that if you really want to understand something, the best way is to try and explain it to someone else. That forces you to sort it out in your own mind. And the more slow and dim-witted your pupil, the more you have to break things down into more and more simple ideas. And that's really the essence of programming. By the time you've sorted out a complicated idea into little steps that even a stupid machine can deal with, you've certainly learned something about it yourself. The teacher usually learns more than the pupil. Isn't that true?"&lt;p&gt;
    "It would be hard to learn much less than my pupils," came a low growl from somewhere on the table, "without undergoing a pre-frontal lobotomy."&lt;p&gt;
    "So I used to spend days struggling to write essays on this 16K machine that would have taken a couple of hours on a typewriter, but what was fascinating to me was the process of trying to explain to the machine what it was I wanted it to do. I virtually wrote my own word processor in BASIC. A simple search and replace routine would take about three hours."&lt;p&gt;
    "I forget, did you ever get any essays done at all?"&lt;p&gt;
    "Well, not as such. No actual essays, but the reasons why not were absolutely fascinating. For instance, I discovered that..."&lt;p&gt;
    He broke off, laughing at himself.&lt;/blockquote&gt;
-&lt;a href="http://www.amazon.com/Dirk-Gentlys-Holistic-Detective-Agency/dp/0671746723"&gt;DNA&lt;/a&gt;
</content>
	</entry>
	<entry>
		<title>Functional Roman Numerals in Python</title>
		<link href="http://billmill.org/python_roman.html" />	
		<id>http://billmill.org/python_roman.html</id>
		<updated>2008-01-12T17:52:00Z</updated>
		<summary type="html">Just as a quck note, I thought I'd post the cleanest python I could come up 
with to solve the &lt;a href="http://billmill.org/roman.html"&gt;roman numerals&lt;/a&gt; 
problem I discussed earlier. It tries to use a functional style while actually 
avoiding recursion. To do so, I wrote an iterative python unfold:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;unfold&lt;/span&gt;(f, x):
    res &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; []                       
    &lt;span style="font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #009999"&gt;1&lt;/span&gt;:
        &lt;span style="font-weight: bold"&gt;try&lt;/span&gt;:
            w, x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; f(x)
            res&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;append(w)
        &lt;span style="font-weight: bold"&gt;except&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;TypeError&lt;/span&gt;:
            &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; res
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And then the answer becomes:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
    (&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;),
    (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;),
    (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt;(x):
    &lt;span style="font-weight: bold"&gt;for&lt;/span&gt; n &lt;span style="font-weight: bold"&gt;in&lt;/span&gt; numerals:
        &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; n[&lt;span style="color: #009999"&gt;1&lt;/span&gt;] &lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt; x: &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; (n[&lt;span style="color: #009999"&gt;0&lt;/span&gt;], x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;n[&lt;span style="color: #009999"&gt;1&lt;/span&gt;])

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt;(n):
    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #bb8844"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;join(unfold(next, n))
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Compare to the haskell I posted before:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next
    &lt;span style="font-weight: bold"&gt;where&lt;/span&gt; next &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;
          next x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
          numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The "where" idiom allows you to group helper functions and constants 
underneath the main function, and the unimportance of function order means you 
can highlight the high-level logic. The haskell code is much shorter and 
tighter than the python.
&lt;p&gt;Having powerful iterative functions like unfoldr in the standard library is 
another clear win for Haskell; it took me more lines to define unfold than it 
did to define both next() and romanize(). (By the by, some googling failed to 
turn up a previous python implementation of unfold(); I think this code gets a 
lot uglier without it. I also don't see any obvious way to do it with 
itertools.)
&lt;p&gt;On the other hand, I find the simplicity of the python next() appealing, 
with its constructive instead of declarative approach. It's a less generic 
solution that's intuitively simpler (for me, still an imperative thinker).  
&lt;p&gt;You could match the haskell more exactly:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #555555"&gt;itertools&lt;/span&gt; &lt;span style="font-weight: bold"&gt;import&lt;/span&gt; ifilter

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;second&lt;/span&gt;(f, (a, b)): &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; (a, f(b))

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt;(x):
    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; second(&lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; a: x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;a, ifilter(&lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; (y, z): z &lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt; x, numerals)&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;next()
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;but the result is pretty ugly. The inability to compose functions compactly
results in a lot of boilerplate, and in having to pick a lot of meaningless 
variable names that obscure what's going on. We could move the lambdas out of
the call and give them names:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt;(x):
    subx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; a: x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;a
    ltx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; (y, z): z &lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt; x

    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; second(subx, ifilter(ltx, numerals)&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;next())
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And it's a little better, but I think we're now pretty far from idiomatic 
python.
&lt;p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The python translation of the haskell isn't bad, but I do think it loses
something. The unfold() trick works really well, and translates easily into
an imperative, pythonic implementation; I'll look for ways to use it in the 
future. And, finally, I'm pretty jealous of Haskell's function combination
abilities.
&lt;p&gt;&lt;h2&gt;Updates&lt;/h2&gt;
&lt;p&gt;In the &lt;a href="http://programming.reddit.com/info/65bpf/comments/"&gt;comments&lt;/a&gt;
at reddit, nostrademons posts a nicer unfold function:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;unfold&lt;/span&gt;(f, x):
    &lt;span style="font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #999999"&gt;True&lt;/span&gt;:
        w, x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; f(x)
        &lt;span style="font-weight: bold"&gt;yield&lt;/span&gt; w
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And Kay Schluehr prefers the iterative solution:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt; numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; ((&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
(&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;), 
(&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;))

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt;(n):
    roman &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; []
    &lt;span style="font-weight: bold"&gt;for&lt;/span&gt; ltr, num &lt;span style="font-weight: bold"&gt;in&lt;/span&gt; numerals:
        (k,n) &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #999999"&gt;divmod&lt;/span&gt;(n, num)
        roman&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;append(ltr&lt;span style="font-weight: bold"&gt;*&lt;/span&gt;k)
    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #bb8844"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;join(roman)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;David Pollak contributes a Scala unfold and romanize &lt;a
href="http://scala-blogs.org/2008/01/roman-numerals-in-scala.html"&gt;at
his blog&lt;/a&gt;.
</summary>
		<content type="html">Just as a quck note, I thought I'd post the cleanest python I could come up 
with to solve the &lt;a href="http://billmill.org/roman.html"&gt;roman numerals&lt;/a&gt; 
problem I discussed earlier. It tries to use a functional style while actually 
avoiding recursion. To do so, I wrote an iterative python unfold:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;unfold&lt;/span&gt;(f, x):
    res &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; []                       
    &lt;span style="font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #009999"&gt;1&lt;/span&gt;:
        &lt;span style="font-weight: bold"&gt;try&lt;/span&gt;:
            w, x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; f(x)
            res&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;append(w)
        &lt;span style="font-weight: bold"&gt;except&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;TypeError&lt;/span&gt;:
            &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; res
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And then the answer becomes:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
    (&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;),
    (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;),
    (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt;(x):
    &lt;span style="font-weight: bold"&gt;for&lt;/span&gt; n &lt;span style="font-weight: bold"&gt;in&lt;/span&gt; numerals:
        &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; n[&lt;span style="color: #009999"&gt;1&lt;/span&gt;] &lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt; x: &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; (n[&lt;span style="color: #009999"&gt;0&lt;/span&gt;], x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;n[&lt;span style="color: #009999"&gt;1&lt;/span&gt;])

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt;(n):
    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #bb8844"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;join(unfold(next, n))
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Compare to the haskell I posted before:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next
    &lt;span style="font-weight: bold"&gt;where&lt;/span&gt; next &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;
          next x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
          numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The "where" idiom allows you to group helper functions and constants 
underneath the main function, and the unimportance of function order means you 
can highlight the high-level logic. The haskell code is much shorter and 
tighter than the python.
&lt;p&gt;Having powerful iterative functions like unfoldr in the standard library is 
another clear win for Haskell; it took me more lines to define unfold than it 
did to define both next() and romanize(). (By the by, some googling failed to 
turn up a previous python implementation of unfold(); I think this code gets a 
lot uglier without it. I also don't see any obvious way to do it with 
itertools.)
&lt;p&gt;On the other hand, I find the simplicity of the python next() appealing, 
with its constructive instead of declarative approach. It's a less generic 
solution that's intuitively simpler (for me, still an imperative thinker).  
&lt;p&gt;You could match the haskell more exactly:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #555555"&gt;itertools&lt;/span&gt; &lt;span style="font-weight: bold"&gt;import&lt;/span&gt; ifilter

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;second&lt;/span&gt;(f, (a, b)): &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; (a, f(b))

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt;(x):
    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; second(&lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; a: x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;a, ifilter(&lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; (y, z): z &lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt; x, numerals)&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;next()
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;but the result is pretty ugly. The inability to compose functions compactly
results in a lot of boilerplate, and in having to pick a lot of meaningless 
variable names that obscure what's going on. We could move the lambdas out of
the call and give them names:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt;(x):
    subx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; a: x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;a
    ltx &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;lambda&lt;/span&gt; (y, z): z &lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt; x

    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; second(subx, ifilter(ltx, numerals)&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;next())
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And it's a little better, but I think we're now pretty far from idiomatic 
python.
&lt;p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The python translation of the haskell isn't bad, but I do think it loses
something. The unfold() trick works really well, and translates easily into
an imperative, pythonic implementation; I'll look for ways to use it in the 
future. And, finally, I'm pretty jealous of Haskell's function combination
abilities.
&lt;p&gt;&lt;h2&gt;Updates&lt;/h2&gt;
&lt;p&gt;In the &lt;a href="http://programming.reddit.com/info/65bpf/comments/"&gt;comments&lt;/a&gt;
at reddit, nostrademons posts a nicer unfold function:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;unfold&lt;/span&gt;(f, x):
    &lt;span style="font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #999999"&gt;True&lt;/span&gt;:
        w, x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; f(x)
        &lt;span style="font-weight: bold"&gt;yield&lt;/span&gt; w
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And Kay Schluehr prefers the iterative solution:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt; numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; ((&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
(&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;), 
(&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;))

&lt;span style="font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt;(n):
    roman &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; []
    &lt;span style="font-weight: bold"&gt;for&lt;/span&gt; ltr, num &lt;span style="font-weight: bold"&gt;in&lt;/span&gt; numerals:
        (k,n) &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #999999"&gt;divmod&lt;/span&gt;(n, num)
        roman&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;append(ltr&lt;span style="font-weight: bold"&gt;*&lt;/span&gt;k)
    &lt;span style="font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #bb8844"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;join(roman)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;David Pollak contributes a Scala unfold and romanize &lt;a
href="http://scala-blogs.org/2008/01/roman-numerals-in-scala.html"&gt;at
his blog&lt;/a&gt;.
</content>
	</entry>
	<entry>
		<title>Beautiful Code</title>
		<link href="http://billmill.org/roman.html" />	
		<id>http://billmill.org/roman.html</id>
		<updated>2008-01-12T03:32:00Z</updated>
		<summary type="html">This bit of code for converting numbers into their Roman numeral equivalents 
was posted by &lt;a 
href="http://programming.reddit.com/info/658ys/comments/c02vm1j"&gt;geezusfreeek&lt;/a&gt; 
on reddit, and I found it so alarmingly pretty that I'm going to spend some 
time to break it down.  This exercise is largely for my own edification, but 
perhaps somebody else will learn a bit along the way. Here's the code:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #555555"&gt;List&lt;/span&gt;
&lt;span style="font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #555555"&gt;Control.Arrow&lt;/span&gt;

&lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next
    &lt;span style="font-weight: bold"&gt;where&lt;/span&gt; next &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;
          next x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
          numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Starting from the inside out is usually the easiest method of understanding 
blocks like this, so we'll look first at the "numerals" list. It is, simply, a 
list of tuples matching Roman numerals to their value. Importantly, it's 
ordered from largest to smallest; we'll see why shortly.&lt;/p&gt;
&lt;p&gt;The meat of this snippet comes in the "next" function:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;
&lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt; x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This function is going to return either Nothing (in the base case) or Just a 
tuple of type (String, Integer), where the string is the largest roman numeral 
that's smaller than x and the int is the difference between x and the value of 
that roman numeral.
&lt;p&gt;The first thing we do is filter out the numerals larger than x; we can run 
some quick tests at the interactive console to see how this works:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="font-weight: bold"&gt;let&lt;/span&gt; numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;)]
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;900&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
[(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;900&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;500&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We use head to grab the largest numeral we can squeeze into the current 
value:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;900&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals 
(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;900&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we subtract the value of the roman numeral from the current value of x, 
in order to return the remainder to be operated on next:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;m &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; second (&lt;span style="color: #009999"&gt;900&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;900&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;0&lt;/span&gt;)
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; second (&lt;span style="color: #009999"&gt;915&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;915&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;15&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And, finally, we wrap it up in a Maybe monad, so that the main function can 
handle the base case transparently:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (&lt;span style="color: #009999"&gt;915&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;)  &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; 
    filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;915&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt;  snd) numerals
&lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;15&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And now we're getting pretty close! What we have is a function that returns 
either Nothing or a tuple containing a letter to add to our roman numeral and 
the next value to operate on. All we have left is the code to drive the 
operation:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The use of unfoldr helps explain why we've wrapped next up in Maybe; here's 
its type:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;m &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;t unfoldr
&lt;span style="color: #990000; font-weight: bold"&gt;unfoldr&lt;/span&gt; &lt;span style="font-weight: bold"&gt;::&lt;/span&gt; (b &lt;span style="font-weight: bold"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Maybe&lt;/span&gt; (a, b)) &lt;span style="font-weight: bold"&gt;-&amp;gt;&lt;/span&gt; b &lt;span style="font-weight: bold"&gt;-&amp;gt;&lt;/span&gt; [a]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So we'll take a function from b -&gt; Maybe (a, b), a value of type b, and 
return a list of [a]. In our case, a = String and b = Integer.
&lt;p&gt;All unfoldr does is call the function it's given as a first argument, which 
returns a tuple or Nothing. If it's a tuple, it appends its first element to a 
list and calls the same function with the second element. If it's Nothing, it 
returns the list it's built up to this point.
&lt;p&gt;As we can see, that matches up perfectly with the "next" function we've 
already defined, and the unfoldr will build up a list of roman numeral strings:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;m &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="font-weight: bold"&gt;let&lt;/span&gt; numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;), 
(&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="font-weight: bold"&gt;let&lt;/span&gt; next x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; x&lt;span style="font-weight: bold"&gt;==&lt;/span&gt;&lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;then&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;               
    &lt;span style="font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; unfoldr next &lt;span style="color: #009999"&gt;9&lt;/span&gt;
[&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;]
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; unfoldr next &lt;span style="color: #009999"&gt;8&lt;/span&gt;
[&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;,&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;,&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;,&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;(We had to mangle the definition of next a bit to fit into the interactive 
interpreter, but we haven't changed anything fundamental about it.)
&lt;p&gt;Finally, all we have to do is concatenate the strings into our final result:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; (concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next) &lt;span style="color: #009999"&gt;8&lt;/span&gt;
&lt;span style="color: #bb8844"&gt;&amp;quot;VIII&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Phew - that's an awful lot of information stuffed into a few pretty lines! I 
still have a hell of a time geting the types to match up so that I can write 
stuff like this, but that doesn't mean that I can't appreciate the beauty of 
how it all fits together. The original function presented here is tight in the 
very best sense of the word.
</summary>
		<content type="html">This bit of code for converting numbers into their Roman numeral equivalents 
was posted by &lt;a 
href="http://programming.reddit.com/info/658ys/comments/c02vm1j"&gt;geezusfreeek&lt;/a&gt; 
on reddit, and I found it so alarmingly pretty that I'm going to spend some 
time to break it down.  This exercise is largely for my own edification, but 
perhaps somebody else will learn a bit along the way. Here's the code:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #555555"&gt;List&lt;/span&gt;
&lt;span style="font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #555555"&gt;Control.Arrow&lt;/span&gt;

&lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next
    &lt;span style="font-weight: bold"&gt;where&lt;/span&gt; next &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;
          next x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
          numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CD&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;400&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;100&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XC&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;90&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;L&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;50&lt;/span&gt;),  (&lt;span style="color: #bb8844"&gt;&amp;quot;XL&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;40&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;),   (&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;),
                      (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Starting from the inside out is usually the easiest method of understanding 
blocks like this, so we'll look first at the "numerals" list. It is, simply, a 
list of tuples matching Roman numerals to their value. Importantly, it's 
ordered from largest to smallest; we'll see why shortly.&lt;/p&gt;
&lt;p&gt;The meat of this snippet comes in the "next" function:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt; &lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;
&lt;span style="color: #990000; font-weight: bold"&gt;next&lt;/span&gt; x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This function is going to return either Nothing (in the base case) or Just a 
tuple of type (String, Integer), where the string is the largest roman numeral 
that's smaller than x and the int is the difference between x and the value of 
that roman numeral.
&lt;p&gt;The first thing we do is filter out the numerals larger than x; we can run 
some quick tests at the interactive console to see how this works:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="font-weight: bold"&gt;let&lt;/span&gt; numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1000&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;900&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;500&lt;/span&gt;)]
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;900&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
[(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;900&lt;/span&gt;),(&lt;span style="color: #bb8844"&gt;&amp;quot;D&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;500&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We use head to grab the largest numeral we can squeeze into the current 
value:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;900&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals 
(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;900&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we subtract the value of the roman numeral from the current value of x, 
in order to return the remainder to be operated on next:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;m &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; second (&lt;span style="color: #009999"&gt;900&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;900&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;0&lt;/span&gt;)
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; second (&lt;span style="color: #009999"&gt;915&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;915&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
(&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;15&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And, finally, we wrap it up in a Maybe monad, so that the main function can 
handle the base case transparently:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (&lt;span style="color: #009999"&gt;915&lt;/span&gt;&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;)  &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; 
    filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: #009999"&gt;915&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt;  snd) numerals
&lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; (&lt;span style="color: #bb8844"&gt;&amp;quot;CM&amp;quot;&lt;/span&gt;,&lt;span style="color: #009999"&gt;15&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And now we're getting pretty close! What we have is a function that returns 
either Nothing or a tuple containing a letter to add to our roman numeral and 
the next value to operate on. All we have left is the code to drive the 
operation:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #990000; font-weight: bold"&gt;romanize&lt;/span&gt; &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The use of unfoldr helps explain why we've wrapped next up in Maybe; here's 
its type:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;m &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;t unfoldr
&lt;span style="color: #990000; font-weight: bold"&gt;unfoldr&lt;/span&gt; &lt;span style="font-weight: bold"&gt;::&lt;/span&gt; (b &lt;span style="font-weight: bold"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Maybe&lt;/span&gt; (a, b)) &lt;span style="font-weight: bold"&gt;-&amp;gt;&lt;/span&gt; b &lt;span style="font-weight: bold"&gt;-&amp;gt;&lt;/span&gt; [a]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So we'll take a function from b -&gt; Maybe (a, b), a value of type b, and 
return a list of [a]. In our case, a = String and b = Integer.
&lt;p&gt;All unfoldr does is call the function it's given as a first argument, which 
returns a tuple or Nothing. If it's a tuple, it appends its first element to a 
list and calls the same function with the second element. If it's Nothing, it 
returns the list it's built up to this point.
&lt;p&gt;As we can see, that matches up perfectly with the "next" function we've 
already defined, and the unfoldr will build up a list of roman numeral strings:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;:&lt;/span&gt;m &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="font-weight: bold"&gt;let&lt;/span&gt; numerals &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; [(&lt;span style="color: #bb8844"&gt;&amp;quot;X&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;10&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;9&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;5&lt;/span&gt;), 
(&lt;span style="color: #bb8844"&gt;&amp;quot;IV&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;4&lt;/span&gt;), (&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;, &lt;span style="color: #009999"&gt;1&lt;/span&gt;)]
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; &lt;span style="font-weight: bold"&gt;let&lt;/span&gt; next x &lt;span style="font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold"&gt;if&lt;/span&gt; x&lt;span style="font-weight: bold"&gt;==&lt;/span&gt;&lt;span style="color: #009999"&gt;0&lt;/span&gt; &lt;span style="font-weight: bold"&gt;then&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Nothing&lt;/span&gt;               
    &lt;span style="font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Just&lt;/span&gt; &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; second (x&lt;span style="font-weight: bold"&gt;-&lt;/span&gt;) &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; head &lt;span style="font-weight: bold"&gt;$&lt;/span&gt; filter ((&lt;span style="font-weight: bold"&gt;&amp;lt;=&lt;/span&gt;x) &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; snd) numerals
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; unfoldr next &lt;span style="color: #009999"&gt;9&lt;/span&gt;
[&lt;span style="color: #bb8844"&gt;&amp;quot;IX&amp;quot;&lt;/span&gt;]
&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; unfoldr next &lt;span style="color: #009999"&gt;8&lt;/span&gt;
[&lt;span style="color: #bb8844"&gt;&amp;quot;V&amp;quot;&lt;/span&gt;,&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;,&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;,&lt;span style="color: #bb8844"&gt;&amp;quot;I&amp;quot;&lt;/span&gt;]
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;(We had to mangle the definition of next a bit to fit into the interactive 
interpreter, but we haven't changed anything fundamental about it.)
&lt;p&gt;Finally, all we have to do is concatenate the strings into our final result:
&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Prelude&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;Control&lt;/span&gt;&lt;span style="font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #445588; font-weight: bold"&gt;Arrow&lt;/span&gt; &lt;span style="color: #445588; font-weight: bold"&gt;List&lt;/span&gt;&lt;span style="font-weight: bold"&gt;&amp;gt;&lt;/span&gt; (concat &lt;span style="font-weight: bold"&gt;.&lt;/span&gt; unfoldr next) &lt;span style="color: #009999"&gt;8&lt;/span&gt;
&lt;span style="color: #bb8844"&gt;&amp;quot;VIII&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Phew - that's an awful lot of information stuffed into a few pretty lines! I 
still have a hell of a time geting the types to match up so that I can write 
stuff like this, but that doesn't mean that I can't appreciate the beauty of 
how it all fits together. The original function presented here is tight in the 
very best sense of the word.
</content>
	</entry>
	<entry>
		<title>Poe on Programming</title>
		<link href="http://billmill.org/poe.html" />	
		<id>http://billmill.org/poe.html</id>
		<updated>2007-12-28T01:14:00Z</updated>
		<summary type="html">One of the strongest influences on my programming has always been an essay I 
read in my sophomore year of high school, around about the same time I began to 
feel the magic of programming. Mr. Carino was a young man just out of college, 
a &lt;a href="http://en.wikipedia.org/wiki/Slayer"&gt;Slayer&lt;/a&gt; fan and ardent Twain 
admirer, and a man who would disappear mysteriously a year later. That year, 
though, he gave the most electric class I've ever taken, and one of the  works 
we read in American Literature was Edgar Allan Poe's &lt;em&gt;&lt;a 
href="http://xroads.virginia.edu/~HYPER/poe/composition.html"&gt;The Philosophy of 
Composition&lt;/a&gt;&lt;/em&gt;.
&lt;p&gt;&lt;h2&gt;Code As Poetry&lt;/h2&gt;
&lt;p&gt;In &lt;em&gt;The Philosophy of Composition&lt;/em&gt;, Poe attempts to describe exactly 
how he composed &lt;a href="http://www.eapoe.org/works/poems/ravent.htm"&gt;The 
Raven&lt;/a&gt;, simply because he's never seen such an attempt before. While we 
might be wise to doubt his motives (as did &lt;a 
href="http://books.google.com/books?id=c3l3ldtUz2EC&amp;pg=PA33&amp;lpg=PA33&amp;dq=%22it+is+difficult+for+us+to+read+that+essay+without+reflecting%22&amp;source=web&amp;ots=hR5I--IYLA&amp;sig=84W7hAjUnNYNDAddaBW3HvtGsrk"&gt;TS  
Eliot&lt;/a&gt;), it nevertheless may be interpreted to provide solid principles for 
the construction of a program.
&lt;p&gt;Before we do so, I must first convince you that code should be read as 
poetry instead of prose. Since this will always be a matter of opinion, and I 
know that many people will disagree, my argument will be short and simple.
&lt;p&gt;When you read prose, you read it as if it is being narrated to you; whether 
by a narrator, a character in the story, or many characters, the distinguishing 
characteristic of prose is its similarity to speech. We find that it is allowed 
a much greater freedom of verbosity, so long as it accomplishes its goal of 
conveying a plot to its reader.
&lt;p&gt;Poetry, on the other hand, is an abstract block of words in which every one 
must carry meaning if the poem is to be any good. We value the poem for the 
beauty not only of the story or image given, but of the way in which it is 
constructed as well. It tends to be much denser and more compact than prose.  
When you read it, you must proceed carefully and consider the meaning of each 
word, and each group of words, and pay attention for double meanings and 
allusions if you are to grasp it fully.
&lt;p&gt;To help us decide how we read code, let's go to a &lt;a 
href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_idx_730"&gt;particularly 
nice bit&lt;/a&gt; of it, and meditate on it for a moment. Did you read it as if it 
were speech, or poetry? Did it have a narrative "flow" for you, or was it 
something of an abstract block of "words"?
&lt;p&gt;Of course, code is really neither prose nor poetry; it is a distinct art 
form of which Poe could not have been aware. I like to think that it may be 
read much more closely to poetry than to prose, and we will proceed from here 
as if this were true.
&lt;p&gt;&lt;h2&gt;Unity of Impression&lt;/h2&gt;
&lt;blockquote&gt;If any literary work is too long to be read at one sitting, we must 
be content to dispense with the immensely important effect derivable from unity 
of impression- for, if two sittings be required, the affairs of the world 
interfere, and everything like totality is at once destroyed. But since, 
ceteris paribus, no poet can afford to dispense with anything that may advance 
his design, it but remains to be seen whether there is, in extent, any 
advantage to counterbalance the loss of unity which attends it. Here I say no, 
at once. What we term a long poem is, in fact, merely a succession of brief 
ones- that is to say, of brief poetical effects. It is needless to demonstrate 
that a poem is such only inasmuch as it intensely excites, by elevating the 
soul; and all intense excitements are, through a psychal necessity, brief. For 
this reason, at least, one-half of the &lt;em&gt;Paradise Lost&lt;/em&gt; is essentially 
prose- a succession of poetical excitements interspersed, inevitably, with 
corresponding depressions- the whole being deprived, through the extremeness of 
its length, of the vastly important artistic element, totality, or unity of 
effect.&lt;/blockquote&gt;
&lt;p&gt;In the first sentence of this paragraph, Poe provides an alternate metric to 
&lt;a href="http://steve-yegge.blogspot.com/2007/12/codes-worst-enemy.html"&gt;Yegge's&lt;/a&gt; 
metric of "code size" - that of "unity of impression". Beautiful code is that 
which is not composed of "a succession of brief.. poetical effects", but of 
just the poem. It is code without filler, bureaucracy, or artifice, regardless 
of how long it is.
&lt;p&gt;As a practical matter, not all programs may be written in this manner. In an 
&lt;a href="http://fxr.watson.org/fxr/source/kern/imgact_elf.c"&gt;operating system 
kernel&lt;/a&gt;, a great deal of "corresponding depressions" &amp;mdash; documentation, 
error handling, and interrupt handling &amp;mdash; must be interspersed in between 
the "poetical effects", making it "essentially prose".
&lt;p&gt;This does not change the fact that the Linux kernel or a BSD kernel is a 
thing of beauty, just as &lt;em&gt;Paradise Lost&lt;/em&gt; is great despite "the 
extremeness of its length". Instead, it should give us motivation for our 
programs, to write them with as few depressions as possible so as not to drag 
down their unity of impression.
&lt;p&gt;A great example of code as poetry is OpenBSD's &lt;a 
href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/tail/"&gt;tail&lt;/a&gt;&lt;sup&gt;&lt;a 
href="#footnote1"&gt;1&lt;/a&gt;&lt;/sup&gt;, which I referenced in a &lt;a 
href="http://billmill.org/reversefile.html"&gt;previous post&lt;/a&gt;. After I sat down 
with it for an hour, it was extremely clear to me what it did. In the &lt;a 
href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/tail/tail.c"&gt;beginning&lt;/a&gt;, 
it set out the patterns of code which would continue throughout, enabling me to 
quickly find my way to &lt;a 
href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/tail/reverse.c"&gt;the part 
that mattered&lt;/a&gt;, despite my relative unfamiliarity with C.
&lt;p&gt;It is a great strength of the Unix philosophy that each bit of code may be 
kept as brief, self-contained, unified, and therefore beautiful. It is a 
pleasure to work with tools which have been pared down to their bare bits 
instead of expanded to encompass ever more functionality.
&lt;p&gt;&lt;h2&gt;Design for a Purpose&lt;/h2&gt;
&lt;blockquote&gt;My next thought concerned the choice of an impression, or effect, 
to be conveyed: and here I may as well observe that throughout the 
construction, I kept steadily in view the design of rendering the work 
universally appreciable. I should be carried too far out of my immediate topic 
were I to demonstrate a point upon which I have repeatedly insisted, and which, 
with the poetical, stands not in the slightest need of demonstration- the 
point, I mean, that Beauty is the sole legitimate province of the 
poem.&lt;/blockquote&gt;
&lt;p&gt;If beauty is the sole province of poetry, I propose that data transformation 
is the sole province of the computer program. (I am not the first to do, 
although I cannot recall where I read it first). Therefore, when designing a 
program, we should at all times keep in mind the transformation which we wish 
to achieve, and discard all those parts which do not assist in that goal.
&lt;p&gt;While this seems at first straightforward, it is important to consider that 
programs are designed for humans and by humans. Unlike poetry, most code is not 
generated by its author for the appreciation of the masses. Instead, it is 
designed to fulfill a purpose, specifically to achieve a certain data 
transformation. 
&lt;p&gt;Just as very few great poems were authored by multiple people, very few 
great programs have been authored by multiple people. If we consider long 
programs to be composed of many poems separated by dull bits, their great parts 
are almost exclusively those parts over which their maintainers have slaved to 
bring to a state of terse beauty.
&lt;p&gt;If you must have many people working on a program, it is of the utmost 
importance that they all know and share an understanding of what 
&lt;em&gt;exactly&lt;/em&gt; it is that the program is intended to accomplish. Without this 
deep shared knowledge of intent, the program will lack a single impression, or 
effect, to be conveyed, and likely fail to impress.
&lt;p&gt;&lt;h2&gt;All The Rest&lt;/h2&gt;
&lt;blockquote&gt;The length, the province, and the tone, being thus determined, I 
betook myself to ordinary induction&lt;/blockquote&gt;
&lt;p&gt;Once you have determined the length, purpose, and tone of your program, the 
rest is, as they say, trivial. Poe dedicates the rest of his essay to applying 
the principles discussed in this essay, and showing how "The Raven" falls ever 
so simply out of them. If you write a program while at all times keeping in 
mind its unity of purpose and the impression you intend to convey, perhaps you 
will find some beauty in it.
&lt;p&gt;I hope that you will read the &lt;a 
href="http://xroads.virginia.edu/~HYPER/poe/composition.html"&gt;essay&lt;/a&gt; in its 
entirety; I'm sure that I have failed to do it justice here. Although it is of 
questionable merit as a method of writing the next "The Raven", perhaps it will 
help you think about how to write your next program.
&lt;p&gt;&lt;div id="footnote"&gt;&lt;h2&gt;Notes&lt;/h2&gt;&lt;/div&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; &lt;a name="footnote1"&gt;&lt;a 
href="http://pyblosxom.svn.sourceforge.net/viewvc/pyblosxom/trunk/pyblosxom/Pyblosxom/"&gt;pybloxsom&lt;/a&gt; 
is another, and reddit readers &lt;a 
href="http://programming.reddit.com/info/63hth/comments/"&gt;provide many 
more&lt;/a&gt;.
</summary>
		<content type="html">One of the strongest influences on my programming has always been an essay I 
read in my sophomore year of high school, around about the same time I began to 
feel the magic of programming. Mr. Carino was a young man just out of college, 
a &lt;a href="http://en.wikipedia.org/wiki/Slayer"&gt;Slayer&lt;/a&gt; fan and ardent Twain 
admirer, and a man who would disappear mysteriously a year later. That year, 
though, he gave the most electric class I've ever taken, and one of the  works 
we read in American Literature was Edgar Allan Poe's &lt;em&gt;&lt;a 
href="http://xroads.virginia.edu/~HYPER/poe/composition.html"&gt;The Philosophy of 
Composition&lt;/a&gt;&lt;/em&gt;.
&lt;p&gt;&lt;h2&gt;Code As Poetry&lt;/h2&gt;
&lt;p&gt;In &lt;em&gt;The Philosophy of Composition&lt;/em&gt;, Poe attempts to describe exactly 
how he composed &lt;a href="http://www.eapoe.org/works/poems/ravent.htm"&gt;The 
Raven&lt;/a&gt;, simply because he's never seen such an attempt before. While we 
might be wise to doubt his motives (as did &lt;a 
href="http://books.google.com/books?id=c3l3ldtUz2EC&amp;pg=PA33&amp;lpg=PA33&amp;dq=%22it+is+difficult+for+us+to+read+that+essay+without+reflecting%22&amp;source=web&amp;ots=hR5I--IYLA&amp;sig=84W7hAjUnNYNDAddaBW3HvtGsrk"&gt;TS  
Eliot&lt;/a&gt;), it nevertheless may be interpreted to provide solid principles for 
the construction of a program.
&lt;p&gt;Before we do so, I must first convince you that code should be read as 
poetry instead of prose. Since this will always be a matter of opinion, and I 
know that many people will disagree, my argument will be short and simple.
&lt;p&gt;When you read prose, you read it as if it is being narrated to you; whether 
by a narrator, a character in the story, or many characters, the distinguishing 
characteristic of prose is its similarity to speech. We find that it is allowed 
a much greater freedom of verbosity, so long as it accomplishes its goal of 
conveying a plot to its reader.
&lt;p&gt;Poetry, on the other hand, is an abstract block of words in which every one 
must carry meaning if the poem is to be any good. We value the poem for the 
beauty not only of the story or image given, but of the way in which it is 
constructed as well. It tends to be much denser and more compact than prose.  
When you read it, you must proceed carefully and consider the meaning of each 
word, and each group of words, and pay attention for double meanings and 
allusions if you are to grasp it fully.
&lt;p&gt;To help us decide how we read code, let's go to a &lt;a 
href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_idx_730"&gt;particularly 
nice bit&lt;/a&gt; of it, and meditate on it for a moment. Did you read it as if it 
were speech, or poetry? Did it have a narrative "flow" for you, or was it 
something of an abstract block of "words"?
&lt;p&gt;Of course, code is really neither prose nor poetry; it is a distinct art 
form of which Poe could not have been aware. I like to think that it may be 
read much more closely to poetry than to prose, and we will proceed from here 
as if this were true.
&lt;p&gt;&lt;h2&gt;Unity of Impression&lt;/h2&gt;
&lt;blockquote&gt;If any literary work is too long to be read at one sitting, we must 
be content to dispense with the immensely important effect derivable from unity 
of impression- for, if two sittings be required, the affairs of the world 
interfere, and everything like totality is at once destroyed. But since, 
ceteris paribus, no poet can afford to dispense with anything that may advance 
his design, it but remains to be seen whether there is, in extent, any 
advantage to counterbalance the loss of unity which attends it. Here I say no, 
at once. What we term a long poem is, in fact, merely a succession of brief 
ones- that is to say, of brief poetical effects. It is needless to demonstrate 
that a poem is such only inasmuch as it intensely excites, by elevating the 
soul; and all intense excitements are, through a psychal necessity, brief. For 
this reason, at least, one-half of the &lt;em&gt;Paradise Lost&lt;/em&gt; is essentially 
prose- a succession of poetical excitements interspersed, inevitably, with 
corresponding depressions- the whole being deprived, through the extremeness of 
its length, of the vastly important artistic element, totality, or unity of 
effect.&lt;/blockquote&gt;
&lt;p&gt;In the first sentence of this paragraph, Poe provides an alternate metric to 
&lt;a href="http://steve-yegge.blogspot.com/2007/12/codes-worst-enemy.html"&gt;Yegge's&lt;/a&gt; 
metric of "code size" - that of "unity of impression". Beautiful code is that 
which is not composed of "a succession of brief.. poetical effects", but of 
just the poem. It is code without filler, bureaucracy, or artifice, regardless 
of how long it is.
&lt;p&gt;As a practical matter, not all programs may be written in this manner. In an 
&lt;a href="http://fxr.watson.org/fxr/source/kern/imgact_elf.c"&gt;operating system 
kernel&lt;/a&gt;, a great deal of "corresponding depressions" &amp;mdash; documentation, 
error handling, and interrupt handling &amp;mdash; must be interspersed in between 
the "poetical effects", making it "essentially prose".
&lt;p&gt;This does not change the fact that the Linux kernel or a BSD kernel is a 
thing of beauty, just as &lt;em&gt;Paradise Lost&lt;/em&gt; is great despite "the 
extremeness of its length". Instead, it should give us motivation for our 
programs, to write them with as few depressions as possible so as not to drag 
down their unity of impression.
&lt;p&gt;A great example of code as poetry is OpenBSD's &lt;a 
href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/tail/"&gt;tail&lt;/a&gt;&lt;sup&gt;&lt;a 
href="#footnote1"&gt;1&lt;/a&gt;&lt;/sup&gt;, which I referenced in a &lt;a 
href="http://billmill.org/reversefile.html"&gt;previous post&lt;/a&gt;. After I sat down 
with it for an hour, it was extremely clear to me what it did. In the &lt;a 
href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/tail/tail.c"&gt;beginning&lt;/a&gt;, 
it set out the patterns of code which would continue throughout, enabling me to 
quickly find my way to &lt;a 
href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/tail/reverse.c"&gt;the part 
that mattered&lt;/a&gt;, despite my relative unfamiliarity with C.
&lt;p&gt;It is a great strength of the Unix philosophy that each bit of code may be 
kept as brief, self-contained, unified, and therefore beautiful. It is a 
pleasure to work with tools which have been pared down to their bare bits 
instead of expanded to encompass ever more functionality.
&lt;p&gt;&lt;h2&gt;Design for a Purpose&lt;/h2&gt;
&lt;blockquote&gt;My next thought concerned the choice of an impression, or effect, 
to be conveyed: and here I may as well observe that throughout the 
construction, I kept steadily in view the design of rendering the work 
universally appreciable. I should be carried too far out of my immediate topic 
were I to demonstrate a point upon which I have repeatedly insisted, and which, 
with the poetical, stands not in the slightest need of demonstration- the 
point, I mean, that Beauty is the sole legitimate province of the 
poem.&lt;/blockquote&gt;
&lt;p&gt;If beauty is the sole province of poetry, I propose that data transformation 
is the sole province of the computer program. (I am not the first to do, 
although I cannot recall where I read it first). Therefore, when designing a 
program, we should at all times keep in mind the transformation which we wish 
to achieve, and discard all those parts which do not assist in that goal.
&lt;p&gt;While this seems at first straightforward, it is important to consider that 
programs are designed for humans and by humans. Unlike poetry, most code is not 
generated by its author for the appreciation of the masses. Instead, it is 
designed to fulfill a purpose, specifically to achieve a certain data 
transformation. 
&lt;p&gt;Just as very few great poems were authored by multiple people, very few 
great programs have been authored by multiple people. If we consider long 
programs to be composed of many poems separated by dull bits, their great parts 
are almost exclusively those parts over which their maintainers have slaved to 
bring to a state of terse beauty.
&lt;p&gt;If you must have many people working on a program, it is of the utmost 
importance that they all know and share an understanding of what 
&lt;em&gt;exactly&lt;/em&gt; it is that the program is intended to accomplish. Without this 
deep shared knowledge of intent, the program will lack a single impression, or 
effect, to be conveyed, and likely fail to impress.
&lt;p&gt;&lt;h2&gt;All The Rest&lt;/h2&gt;
&lt;blockquote&gt;The length, the province, and the tone, being thus determined, I 
betook myself to ordinary induction&lt;/blockquote&gt;
&lt;p&gt;Once you have determined the length, purpose, and tone of your program, the 
rest is, as they say, trivial. Poe dedicates the rest of his essay to applying 
the principles discussed in this essay, and showing how "The Raven" falls ever 
so simply out of them. If you write a program while at all times keeping in 
mind its unity of purpose and the impression you intend to convey, perhaps you 
will find some beauty in it.
&lt;p&gt;I hope that you will read the &lt;a 
href="http://xroads.virginia.edu/~HYPER/poe/composition.html"&gt;essay&lt;/a&gt; in its 
entirety; I'm sure that I have failed to do it justice here. Although it is of 
questionable merit as a method of writing the next "The Raven", perhaps it will 
help you think about how to write your next program.
&lt;p&gt;&lt;div id="footnote"&gt;&lt;h2&gt;Notes&lt;/h2&gt;&lt;/div&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; &lt;a name="footnote1"&gt;&lt;a 
href="http://pyblosxom.svn.sourceforge.net/viewvc/pyblosxom/trunk/pyblosxom/Pyblosxom/"&gt;pybloxsom&lt;/a&gt; 
is another, and reddit readers &lt;a 
href="http://programming.reddit.com/info/63hth/comments/"&gt;provide many 
more&lt;/a&gt;.
</content>
	</entry>
</feed>
