<?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>Informatikr</title>
	<atom:link href="http://informatikr.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://informatikr.wordpress.com</link>
	<description></description>
	<lastBuildDate>Fri, 06 Nov 2009 10:53:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='informatikr.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Informatikr</title>
		<link>http://informatikr.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://informatikr.wordpress.com/osd.xml" title="Informatikr" />
	<atom:link rel='hub' href='http://informatikr.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Learning Haskell&#8217;s Basics &#8211; Problems from Project Euler</title>
		<link>http://informatikr.wordpress.com/2008/10/13/learning-haskells-basics-problems-from-project-euler/</link>
		<comments>http://informatikr.wordpress.com/2008/10/13/learning-haskells-basics-problems-from-project-euler/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 14:15:26 +0000</pubDate>
		<dc:creator>Falko</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://informatikr.wordpress.com/?p=30</guid>
		<description><![CDATA[Recently I started to learn Haskell and figured, it would be a good Idea to experiment with some of the Problems from Project Euler I worked through several of the Euler problems some time ago, when I was learning Ruby. Back then I was amazed, how concise my code was, compared to the other languages [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=informatikr.wordpress.com&amp;blog=5086801&amp;post=30&amp;subd=informatikr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I started to learn <a href="http://haskell.org">Haskell</a> and figured, it would be a good Idea to experiment with some of the Problems from <a href="http://projecteuler.net">Project Euler</a></p>
<p>I worked through several of the Euler problems some time ago, when I was learning Ruby. Back then I was amazed, how concise my code was, compared to the other languages I knew (Java and C, mainly). With Haskell the same thing happened, although on a smaller scale.</p>
<p>Below, I will present you four solutions that I especially liked and discuss some of their features, comparing them to my Ruby code.</p>
<h3><a href="http://projecteuler.net/index.php?section=problems&amp;id=1">Problem 1</a></h3>
<p><em>Find the sum of all the multiples of 3 or 5 below 1000.</em></p>
<p>This one is yet another flavour of the notorious <a href="http://www.codinghorror.com/blog/archives/000804.html">FizzBuzz</a> problem. Employers watch closely! Here&#8217;s my solution:</p>
<pre style="border:1px solid;background-color:lightgrey;padding:5px;">euler1 = sum $ filter (divisible [3,5]) [1..999]

divisible :: [Int] -&gt; Int -&gt; Bool
divisible divisors n = any (\d -&gt; (mod n d)==0 ) divisors</pre>
<p>This is solves the problem in quite a general way, by introducing the utility function <code>divisible</code> which takes a list of divisors as well as a number and returns <code>True</code> if the number is evenly divisible by any element of the <code>divisors</code> list.</p>
<p>Compared to Ruby,  I see Haskell slightly ahead in this problem, due to the possibility of <a href="http://www.haskell.org/haskellwiki/Partial_application">partial application</a>. An equivalent Ruby solution would make use of blocks/lambdas that require definitions of formal parameters, which in turn make the code somewhat more verbose.</p>
<h3><a href="http://projecteuler.net/index.php?section=problems&amp;id=2">Problem 2</a></h3>
<p><em>Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.</em></p>
<pre style="border:1px solid;background-color:lightgrey;padding:5px;">euler2 = sum $ filter even $ takeWhile (&lt;= 4000000) fibs

fibs :: [Int]
fibs = 1 : 2 : zipWith (+) fibs (tail fibs)</pre>
<p>Haskell&#8217;s clear win, in this case, is <a href="http://www.haskell.org/haskellwiki/Haskell/Lazy_evaluation">lazy evaluation</a> and the possibility of recursively defining an infinite list containing <em>all</em> the Fibonacci numbers. However, Ruby deserves a golden style-point for allowing the number four million to be written as <code>4_000_000</code>. Is there a similar way of writing numbers in Haskell that I should know of?</p>
<h3><a href="http://projecteuler.net/index.php?section=problems&amp;id=4">Problem 4</a></h3>
<p><em>Find the largest palindrome made from the product of two 3-digit numbers.</em></p>
<p>This is my favourite code snippet in this article. Thanks to the <a href="http://www.haskell.org/haskellwiki/List_comprehension">list comprehension</a> the solution rather looks like the problem definition. Simple and beautiful.</p>
<h3><a href="http://projecteuler.net/index.php?section=problems&amp;id=21">Problem 21</a></h3>
<p><em>Evaluate the sum of all the amicable numbers under 10000.</em></p>
<pre style="border:1px solid;background-color:lightgrey;padding:5px;">euler21 = (sum . filter amicable) [1..9999]

amicable :: Int -&gt; Bool
amicable a = (a /= b) &amp;&amp; ((d b)==a)
    where d = sum . divisors
          b = d a

divisors :: Int -&gt; [Int]
divisors = divisors' 2 [1]
    where divisors' d divs n
            | d^2 &gt; n      = divs
            | (mod n d)==0 = if (div n d) == d
                             then divisors' (d+1) (d : divs) n
                             else divisors' (d+1) (d : (div n d) : divs) n
            | otherwise    = divisors' (d+1) divs n</pre>
<p>My point on this piece of code is Haskell&#8217;s <code>where</code> clause. Locally defined helper functions come in handy all the time in a language without looping constructs (other than recursion), where you want to hide starting value-, accumulation- and similar parameters from the user. Here, the <code>divisors</code> function is just a prettier interface to the one that does the real job: <code>divisors'</code>.</p>
<h3>Conclusion</h3>
<p>Before starting to learn Haskell, Ruby was my favourite programming language. And it still is. But I&#8217;m definitely feeling Haskell&#8217;s gravitational field. As a next step I&#8217;m now looking forward to using Haskell in a slightly more serious context. Let&#8217;s see if I get caught inside it&#8217;s event horizon.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/informatikr.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/informatikr.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/informatikr.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/informatikr.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/informatikr.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/informatikr.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/informatikr.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/informatikr.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=informatikr.wordpress.com&amp;blog=5086801&amp;post=30&amp;subd=informatikr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://informatikr.wordpress.com/2008/10/13/learning-haskells-basics-problems-from-project-euler/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Falko</media:title>
		</media:content>
	</item>
	</channel>
</rss>
