<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Butler &#187; Haskell</title>
	<atom:link href="http://paulbutler.org/archives/category/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulbutler.org</link>
	<description></description>
	<lastBuildDate>Mon, 25 Jul 2011 10:41:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Why R doesn&#039;t suck</title>
		<link>http://paulbutler.org/archives/why-r-doesnt-suck/</link>
		<comments>http://paulbutler.org/archives/why-r-doesnt-suck/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 13:45:01 +0000</pubDate>
		<dc:creator>Paul Butler</dc:creator>
				<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://paulbutler.org/?p=336</guid>
		<description><![CDATA[I first encountered the R programming language a few years ago when I needed to make some plots. Although I&#8217;ve used it occasionally since, I always considered it a sort of &#8220;Perl for statisticians&#8221; &#8212; a useful swiss-army knife with &#8230; <a href="http://paulbutler.org/archives/why-r-doesnt-suck/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I first encountered the <a href="http://www.r-project.org/">R programming language</a> a few years ago when I needed to make some plots. Although I&#8217;ve used it occasionally since, I always considered it a sort of &#8220;Perl for statisticians&#8221; &mdash; a useful swiss-army knife with ugly syntax and inconsistent semantics. My workflow generally involved manipulating the data in Python and using R to make a simple plot, minimizing the amount of R code I wrote as much as possible.</p>
<p>When I recently decided to sit down and properly learn the language, I was pleasantly surprised that underneath the line noise was an interesting and unique language. R is a descendant of LISP and, deep down, maintains some of the beauty its ancestor. It also borrows some unique and interesting features from other functional and dynamic languages.</p>
<h3>Code is Data</h3>
<p>R is true to its LISP roots in that you can create, modify, and evaluate parse trees from the code itself. One way to do so is with the <samp>quote()</samp> special-function, which returns its argument, unevaluated, as an expression object that can be traversed, modified and evaluated.</p>
<p>A fun (though not especially useful) consequence of this is that you can write an <a href="http://en.wikipedia.org/wiki/Quine_(computing)">expression which returns itself</a> as a quote:<br />
<code><br />
> (function(x) substitute((x)(x)))(function(x) substitute((x)(x)))<br />
(function(x) substitute((x)(x)))(function(x) substitute((x)(x)))<br />
> expression <- (function(x) substitute((x)(x)))(function(x) substitute((x)(x)))<br />
> expression == eval(expression)<br />
[1] TRUE<br />
</code></p>
<h3>Optional Laziness</h3>
<p>By default, R uses <a href="http://en.wikipedia.org/wiki/Eager_evaluation">eager evaluation</a>, so expressions are evaluated as soon as they are assigned. However, R takes after functional languages like Haskell and OCaml in that it allows lazy evaluation, where expressions are only evaluated at the time they are first used.</p>
<p>For example, consider the Haskell code:<br />
<code><br />
m = sum [1..]<br />
</code></p>
<p>Where <samp>sum</samp> returns the sum of a list and <samp>[1..]</samp> is the (infinite) list of all natural numbers. In most languages, the assignment would cause the program to loop forever trying to sum all the natural numbers so it can assign that value to <samp>m</samp>. In Haskell, the assignment does complete; it simply assigns the expression <samp>sum [1..]</samp> to <samp>m</samp> so that it can be evaluated when the value of <samp>m</samp> is first used.</p>
<p>In R we can accomplish something similar with the <samp>delayedAssign()</samp> function:<br />
<code><br />
delayedAssign("m", sum(1:Inf))<br />
</code></p>
<p>Note that in R, unlike OCaml, the variables may be explicitly made lazy with <samp>delayedAssign</samp>, but are evaluated automatically when they are used.</p>
<p>Unfortunately, R evaluates lazy variables when they are pointed to by a data structure, even if their value is not needed at the time. This means that infinite data structures, one common application of laziness in Haskell, are not possible in R.</p>
<h3>Operators are functions</h3>
<p>When using higher-order functions, it&#8217;s sometimes useful to be able to treat operators as functions. Python accomplishes this in a clunky way: there is an <samp>operator</samp> module which redefines the built-in operators as functions. R takes a more functional approach. As in Haskell and O&#8217;Caml, operators are just syntactic sugar for ordinary functions. Enclosing any operator in backticks lets you use it as if it were an ordinary function. For example, calling <samp>`+`(2, 3)</samp> returns <samp>5</samp>.</p>
<p>In fact, both the infix and prefix form are indistinguishable once they are parsed.<br />
<code><br />
> quote(3 + 4) == quote(`+`(3, 4))<br />
[1] TRUE<br />
</code></p>
<p>One surprising fact in R is that the assignment operators (<samp>&lt;-</samp>, <samp>&lt;&lt;-</samp> and <samp>=</samp>) are functions like any other. As a result, they can be overwritten or passed around as desired, though neither strikes me as a particularly good idea.</p>
<h3>Continuations</h3>
<p><a href="http://en.wikipedia.org/wiki/Continuation">Continuations</a> in R are a way of &#8220;breaking out&#8221; of a computation and jumping down the call stack to return early. The R function <samp>callCC()</samp> (<strong>call</strong> with <strong>c</strong>urrent <strong>c</strong>ontinuation) takes one argument, a function. It then evaluates that function, passing in a special function as an argument. <samp>callCC()</samp> then returns the first value that the special function is called with, or the return value of evaluating its argument if the special function is not called before the function returns.</p>
<p>To give you a better idea of what that looks like, consider this example:<br />
<code><br />
> callCC(function(m) {return(4)})<br />
[1] 4<br />
> callCC(function(m) {m(2); return(4)})<br />
[1] 2<br />
</code></p>
<p>Calling the function <samp>m(2)</samp> essentially cuts the computation short, drops down in the call stack to <samp>callCC</samp>, and returns <samp>2</samp>.</p>
<p>If you&#8217;ve used continuations in another language, note that in R the exit function can only be called before <samp>callCC()</samp> returns. This makes R&#8217;s continuation semantics less powerful than those of languages like Scheme, Smalltalk, and Ruby.</p>
<p>R is not without its flaws and legacy baggage (you can trace its roots back to the <a href="http://en.wikipedia.org/wiki/S_(programming_language)">S programming language</a> 35 years ago), but once you learn to use it right, it&#8217;s a very powerful and indispensable language.</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbutler.org/archives/why-r-doesnt-suck/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>N-Queens in a Tweet</title>
		<link>http://paulbutler.org/archives/n-queens-in-a-tweet/</link>
		<comments>http://paulbutler.org/archives/n-queens-in-a-tweet/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 04:36:32 +0000</pubDate>
		<dc:creator>Paul Butler</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://paulbutler.org/?p=145</guid>
		<description><![CDATA[Most people who like puzzles or study computer science have probably encountered the famous N-Queens problem. If you haven&#8217;t, before reading any further, try this online version of the most popular form, the 8-Queens problem. The 8-Queens problem is to &#8230; <a href="http://paulbutler.org/archives/n-queens-in-a-tweet/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Most people who like puzzles or study computer science have probably encountered the famous <a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle">N-Queens problem</a>. If you haven&#8217;t, before reading any further, try this <a href="http://www.hbmeyer.de/backtrack/achtdamen/eight.htm">online version</a> of the most popular form, the 8-Queens problem.</p>
<p>The 8-Queens problem is to find positions on a chess board for eight <a href="http://en.wikipedia.org/wiki/Queen_(chess)">queen</a> chess pieces so that none of them &#8220;threaten&#8221; the others. Since a queen in chess can move horizontally, vertically, and diagonally, this means placing each queen on her own horizontal, vertical, and diagonal lines.</p>
<p>The N-Queens problem is a generalization of the 8-Queens problem with (surprise) <img src='http://s.wordpress.com/latex.php?latex=N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N' title='N' class='latex' /> queens instead of 8, on an <img src='http://s.wordpress.com/latex.php?latex=N%20%5Ctimes%20N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N \times N' title='N \times N' class='latex' /> board instead of a standard <img src='http://s.wordpress.com/latex.php?latex=8%20%5Ctimes%208&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='8 \times 8' title='8 \times 8' class='latex' /> chess board.</p>
<p>In the spirit of <a href="http://en.wikipedia.org/wiki/Perl#Perl_golf">Perl Golf</a>, I wondered what the minimal amount of code would be to solve the N-Queens problem. As an arbitrary target, I picked 140 characters, also the limit on message length imposed by Twitter. I picked Haskell, knowing its list functions and generally terse syntax would come in handy. Naturally, I broke all the conventions that would cost extra characters, like using whitespace and explicit function signatures.</p>
<p>Since Haskell modules have a bit of overhead that would use up my valuable characters, I had to cheat a bit by leaving the module declaration out. The only way to run the program unmodified is to name it <samp><a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-dot-files.html">.ghci</a></samp> to trick GHCi into using it as a start script.</p>
<p>The result follows. I used 140 characters exactly, including the newline character.</p>
<pre lang="haskell">
import Data.List
let nqueens n=[zipWith(\x y->x:show y)['a'..]x|x<-permutations[1..n],(length$nub$zipWith(+)(x++x)$[0,-1..1-n]++[n..])==n*2]
</pre>
<p>Pretty, eh? Fine, maybe not, but it works:</p>
<pre>
> nqueens 4
[["a2","b4","c1","d3"],["a3","b1","c4","d2"]]
> nqueens 5
[["a2","b4","c1","d3","e5"],["a3","b1","c4","d2","e5"],
["a3","b5","c2","d4","e1"],["a4","b2","c5","d3","e1"],
["a2","b5","c3","d1","e4"],["a1","b3","c5","d2","e4"],
["a4","b1","c3","d5","e2"],["a5","b3","c1","d4","e2"],
["a1","b4","c2","d5","e3"],["a5","b2","c4","d1","e3"]]
</pre>
<p>The result is a list of queen positions given as <a href="http://en.wikipedia.org/wiki/Algebraic_chess_notation#Naming_squares_on_the_board">coordinates on a chess board</a> (generalizing them in the obvious way).</p>
<p>As an example, the result <samp>["a1","b5","c8","d6","e3","f7","g2","h4"]</samp> would look like this:</p>
<div id="attachment_195" class="wp-caption none" style="width: 312px"><a href="http://test.paulbutler.org/wp-content/uploads/2009/06/8queens.png"><img src="http://test.paulbutler.org/wp-content/uploads/2009/06/8queens.png" alt="Eight Queens Solution" width="302" height="302" class="size-full wp-image-195" /></a>
<p class="wp-caption-text">Eight Queens Solution (created with <a href="http://www.chessup.net/">ChessUp</a>)</p>
</div>
<p>Before I explain how it works, I'll put the whitespace back in.</p>
<pre lang="haskell">
import Data.List
let nqueens n = [zipWith (\x y -> x : show y) ['a'..] x | x <- permutations [1..n], (length $ nub $ zipWith (+) (x ++ x) $ [0,-1..1-n] ++ [n..]) == n*2]
</pre>
<p>Ignore diagonal movements for now &mdash; we could call it the N-<a href="http://en.wikipedia.org/wiki/Rook_(chess)">Rooks</a> problem. If we want to place <img src='http://s.wordpress.com/latex.php?latex=N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N' title='N' class='latex' /> rooks on a chess board so none of them are threatened, each needs to be in its own column and row. If you number the rows <img src='http://s.wordpress.com/latex.php?latex=1%2C%202%20%5Cldots%20N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='1, 2 \ldots N' title='1, 2 \ldots N' class='latex' />, each solution corresponds to a permutation of these numbers. The board above, for example, would be <samp>[1, 5, 8, 6, 3, 7, 2, 4]</samp>. So to solve the N-Rooks problem in Haskell, all we need is <samp>permutations [1..n]</samp>.</p>
<p>That's a good start, but we have to consider the diagonal lines as well. The simplest way to do this is to assign the columns numbers from <img src='http://s.wordpress.com/latex.php?latex=1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='1' title='1' class='latex' /> to <img src='http://s.wordpress.com/latex.php?latex=N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N' title='N' class='latex' />. Then add those to the number of the row the queen in that column is in (remember, since we are using permutations, there is exactly one queen in each column).</p>
<p>For example, if we add <samp>[1, 5, 8, 6, 3, 7, 2, 4]</samp> and <samp>[1, 2, 3, 4, 5, 6, 7, 8]</samp>, we get <samp>[2, 7, 11, 10, 8, 13, 9, 12]</samp>. The resulting numbers are all different, so the queens are all on different diagonal lines &mdash; at least for downward diagonals. For upward diagonals, we could do the same thing but use <samp>[N..1]</samp> instead of <samp>[1..N]</samp>.</p>
<p>Since every character counts here, it's better if we can do both diagonal directions at once. This complicates things a bit because without caution, the numbers for upward diagonal lines will overlap with those for downward diagonal lines. Fortunately, we can just add a constant factor of at least <img src='http://s.wordpress.com/latex.php?latex=2N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='2N' title='2N' class='latex' /> to either set of lines. This is essentially what the code above does, although the arithmetic is a bit more cryptic to keep the character count down.</p>
<p>The resulting list will have <img src='http://s.wordpress.com/latex.php?latex=2N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='2N' title='2N' class='latex' /> elements &mdash; <img src='http://s.wordpress.com/latex.php?latex=N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N' title='N' class='latex' /> for the upward diagonal lines and <img src='http://s.wordpress.com/latex.php?latex=N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N' title='N' class='latex' /> more for the downward ones. Now we can use <samp>nub</samp> to rid the list of duplicates. If the list had no duplicates &mdash; that is, each queen is on her own diagonal lines &mdash; the <span>length</span> of that list will still equal <img src='http://s.wordpress.com/latex.php?latex=2N&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='2N' title='2N' class='latex' /> after duplicates are removed. That makes it a valid solution, so it is included in the result.</p>
<p>I should note that this algorithm isn't (nearly) as efficient as a standard backtracking approach as described on Wikipedia's <a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle_solutions">eight queens puzzle solutions article</a>. But good luck getting one of those solutions into a tweet.</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbutler.org/archives/n-queens-in-a-tweet/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

