<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>In Search of Lost Time &#187; Haskell</title>
	<atom:link href="http://lativy.org/tag/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://lativy.org</link>
	<description>Thoughts on music, technology, life, et cetera</description>
	<lastBuildDate>Mon, 15 Aug 2011 14:26:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='lativy.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>In Search of Lost Time &#187; Haskell</title>
		<link>http://lativy.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://lativy.org/osd.xml" title="In Search of Lost Time" />
	<atom:link rel='hub' href='http://lativy.org/?pushpress=hub'/>
		<item>
		<title>Haskell in less than five minutes</title>
		<link>http://lativy.org/2008/08/13/haskell-in-less-than-five-minutes/</link>
		<comments>http://lativy.org/2008/08/13/haskell-in-less-than-five-minutes/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 01:41:08 +0000</pubDate>
		<dc:creator>Nicholas Lativy</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[GHC]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://lativy.org/?p=121</guid>
		<description><![CDATA[I have been playing with Haskell and the Glasgow Haskell Compiler today, refreshing my memory of the language. After spending the last two years coding mostly in C++ and Python with a smattering of C it is a rather pleasant experience. Note: the &#8230; <a href="http://lativy.org/2008/08/13/haskell-in-less-than-five-minutes/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lativy.org&#038;blog=13379655&#038;post=121&#038;subd=nlativy&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been playing with Haskell and the <a href="http://haskell.org/haskellwiki/GHC">Glasgow Haskell Compiler</a> today, refreshing my memory of the language. After spending the last two years coding mostly in C++ and Python with a smattering of C it is a rather pleasant experience. Note: the simplest way to try out these examples is with ghci or hugs; simply place the code in <code>file.hs</code> and load it from within the interpreter with <code>:load file.hs</code> and then call the function as normal.</p>
<p>To start with a simple example of the language, the following calculates the nth Fibonacci number:<br />
<code></p>
<pre>fib n | n &lt; 2 = 1
      | n &gt;= 2 = fib(n-1) + fib(n-2)</pre>
<p></code><br />
The boolean guards <code>n &lt; 2</code> and <code>n &gt;= 2</code> determine what is evaluated. We can then map this function onto the natural numbers using:<br />
<code></p>
<pre>fibs = map fib [1..]</pre>
<p></code><br />
and subsequently get the first 10 Fibonacci numbers with <code>take 10 fibs</code> which returns the expected list. Higher order functions such as map are simple to define, such as:<br />
<code></p>
<pre>map f xs = [f x | x &lt;- xs]</pre>
<p></code><br />
The expression on the right of the equals is an example of list comprehension. The generator <code>x &lt;- xs</code> takes each <code>x</code> from the input list <code>xs</code> in turn and the expression <code>f x</code> is applied forming a new list. Multiple generators can be used, for example the expression <code>[(a,b) | a &lt;- [1..3], b &lt;- ['a', 'b']]</code> evaluates to the list<br />
<code></p>
<pre>[(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]</pre>
<p></code><br />
Making use of list comprehension gives us the rather lovely Haskell rendering of the <a href="http://en.wikipedia.org/wiki/Quicksort">quicksort</a> algorithm:<br />
<code></p>
<pre>qsort []     = []
qsort (x:xs) = qsort [y | y &lt;- xs, y &lt; x] ++ [x]
                     ++ qsort [y | y &lt;- xs, y &gt;= x]</pre>
<p></code><br />
Simple, concise and, in comparison to C or the pseudo code rendition in the textbooks I have to hand, very easy to understand. When supplied with an empty list the <code>qsort</code> function returns an empty list. When given a list (consisting of the first element, <code>x</code>, and the remainder of the list <code>xs</code>) we use the first element as the pivot and apply <code>qsort</code> recursively to all the elements less than x, concatenate that list, using the <code>++</code> operator, with <code>[x]</code> and then concatenate that with the result of qsort applied to all elements greater than x. We could even make this slightly shorter by replacing <code>[y | y &lt;- xs, y &lt; x]</code> with <code>filter (&lt; x) xs</code>. Of course I have glossed over the fact that using the head of the list as the pivot is not a very good idea but it&#8217;s still a very aesthetically pleasing piece of code. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nlativy.wordpress.com/121/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nlativy.wordpress.com/121/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nlativy.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nlativy.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nlativy.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nlativy.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nlativy.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nlativy.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nlativy.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nlativy.wordpress.com/121/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lativy.org&#038;blog=13379655&#038;post=121&#038;subd=nlativy&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lativy.org/2008/08/13/haskell-in-less-than-five-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3cff300e1922220e3b6949df2b48438?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">nlativy</media:title>
		</media:content>
	</item>
	</channel>
</rss>
