<?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>/dev/tty &#187; Michel Rijnders</title>
	<atom:link href="http://blog.tty.nl/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tty.nl</link>
	<description>Notes on Web Development, Computer Programming, and Software Engineering</description>
	<lastBuildDate>Thu, 29 Dec 2011 10:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>NLTK&#8217;s dispersion_plot on Mac OS X</title>
		<link>http://blog.tty.nl/2010/02/28/nltks-dispersion_plot-on-mac-os-x/</link>
		<comments>http://blog.tty.nl/2010/02/28/nltks-dispersion_plot-on-mac-os-x/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 11:33:10 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[NLTK]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=344</guid>
		<description><![CDATA[While reading &#8220;Natural Language Processing with Python&#8221; I ran into problems on my Mac with examples that were using the dispersion_plot function: calls to the function returned immediately without displaying anything. Turns out matplotlib&#8216;s back-end wasn&#8217;t configured properly. To fix &#8230; <a href="http://blog.tty.nl/2010/02/28/nltks-dispersion_plot-on-mac-os-x/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While reading <a href="http://www.nltk.org/book">&#8220;Natural Language Processing with Python&#8221;</a> I ran into problems on my Mac with examples that were using the <tt>dispersion_plot</tt> function: calls to the function returned immediately without displaying anything.</p>
<p>Turns out <a href="http://matplotlib.sourceforge.net/">matplotlib</a>&#8216;s back-end wasn&#8217;t configured properly. To fix this I had to add a rc file (<tt>matplotlibrc</tt>) to my <tt>~/.matplotlib</tt> directory. The rc file contains the following:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">backend: TkAgg</div></div>
<p>And, hey presto:<br />
<a href="http://blog.tty.nl/wp-content/uploads/Screen-shot-2010-02-28-at-12.26.21-PM.png"><img src="http://blog.tty.nl/wp-content/uploads/Screen-shot-2010-02-28-at-12.26.21-PM.png" alt="Screen shot 2010-02-28 at 12.26.21 PM" title="Screen shot 2010-02-28 at 12.26.21 PM" width="395" height="316" class="alignnone size-full wp-image-358" /></a></p>
<p>(disclaimer: &#8220;Works on my machine!&#8221;)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2010/02/28/nltks-dispersion_plot-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Reading List for 2010</title>
		<link>http://blog.tty.nl/2010/01/09/my-reading-list-for-2010/</link>
		<comments>http://blog.tty.nl/2010/01/09/my-reading-list-for-2010/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 14:59:07 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming Language Theory]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=316</guid>
		<description><![CDATA[One of the suggestions of &#8220;The Pragmatic Programmer&#8221; is that you should learn at least one new programming language every year. This is a great suggestion, but after a couple of years its usefulness diminishes, e.g. if one already knows &#8230; <a href="http://blog.tty.nl/2010/01/09/my-reading-list-for-2010/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the suggestions of &#8220;The Pragmatic Programmer&#8221; is that you should learn at least one new programming language every year. This is a great suggestion, but after a couple of years its usefulness diminishes, e.g. if one already knows Perl and Python, then the payback on learning Ruby is rather small. Therefore I&#8217;m going to concentrate on the foundations of programming languages this year. Here&#8217;s my tentative reading list:</p>
<ul>
<li><a href="http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&#038;tid=11656">Design Concepts in Programming Languages</a></li>
<li><a href="http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&#038;tid=10142">Concepts, Techniques, and Models of Computer Programming</a></li>
<li><a href="http://www.cis.upenn.edu/~bcpierce/tapl/">Types and Programming Languages </a></li>
</ul>
<p>Suggestions welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2010/01/09/my-reading-list-for-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Quiz, Haskell Solution: LCD Numbers</title>
		<link>http://blog.tty.nl/2009/12/17/ruby-quiz-haskell-solution-lcd-numbers/</link>
		<comments>http://blog.tty.nl/2009/12/17/ruby-quiz-haskell-solution-lcd-numbers/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 13:26:34 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Ruby Quiz]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=288</guid>
		<description><![CDATA[A solution to Ruby Quiz #14 in literate Haskell: LCD Numbers =========== Problem ------- [original source](http://rubyquiz.com/quiz14.html) This week's quiz is to write a program that displays LCD style numbers at adjustable sizes. The digits to be displayed will be passed &#8230; <a href="http://blog.tty.nl/2009/12/17/ruby-quiz-haskell-solution-lcd-numbers/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A solution to Ruby Quiz #14 in literate Haskell:</p>
<pre>
LCD Numbers
===========

Problem
-------

[original source](http://rubyquiz.com/quiz14.html)

This week's quiz is to write a program that displays LCD style numbers
at adjustable sizes.

The digits to be displayed will be passed as an argument to the
program. Size should be controlled with the command-line option -s
follow up by a positive integer. The default value for -s is 2.

For example, if your program is called with:

    $ lcd.rb 012345

The correct display is:

     --        --   --        --
    |  |    |    |    | |  | |
    |  |    |    |    | |  | |
               --   --   --   --
    |  |    | |       |    |    |
    |  |    | |       |    |    |
     --        --   --        -- 

And for:

    $ lcd.rb -s 1 6789

Your program should print:

     -   -   -   -
    |     | | | | |
     -       -   -
    | |   | | |   |
     -       -   - 

Note the single column of space between digits in both examples. For
other values of -s, simply lengthen the - and | bars.

Solution
--------

Module declaration and imports:

> module Main where
>
> import Data.Char (digitToInt)
> import Data.List (intersperse)
> import System.Console.GetOpt
> import System.Environment (getArgs)

First we define the numbers at size 1:

> n0 = [ " - "
>      , "| |"
>      , "   "
>      , "| |"
>      , " - "
>      ]
>
> n1 = [ "   "
>      , "  |"
>      , "   "
>      , "  |"
>      , "   "
>      ]
>
> n2 = [ " - "
>      , "  |"
>      , " - "
>      , "|  "
>      , " - "
>      ]
>
> n3 = [ " - "
>      , "  |"
>      , " - "
>      , "  |"
>      , " - "
>      ]
>
> n4 = [ "   "
>      , "| |"
>      , " - "
>      , "  |"
>      , "   "
>      ]
>
> n5 = [ " - "
>      , "|  "
>      , " - "
>      , "  |"
>      , " - "
>      ]
>
> n6 = [ " - "
>      , "|  "
>      , " - "
>      , "| |"
>      , " - "
>      ]
>
> n7 = [ " - "
>      , "  |"
>      , "   "
>      , "  |"
>      , "   "
>      ]
>
> n8 = [ " - "
>      , "| |"
>      , " - "
>      , "| |"
>      , " - "
>      ]
>
> n9 = [ " - "
>      , "| |"
>      , " - "
>      , "  |"
>      , " - "
>      ]
>

Put the numbers in  a list:

> numbers = [n0,n1,n2,n3,n4,n5,n6,n7,n8,n9]

Horizontal scaling function, given a string replicate the second
character n times:

> hscale n cs = head cs : replicate n (cs!!1) ++ [last cs]

Vertical scaling function, repeat the second and fourth row n times:

> vscale n css = head css : replicate n cs1 ++ [cs2] ++ replicate n cs3 ++ [cs4]
>   where cs1 = css !! 1
>         cs2 = css !! 2
>         cs3 = css !! 3
>         cs4 = last css

Scale function; note this function scales a single number:

> scale n = vscale n . map (hscale n)

Function that converts a list of numbers to a string of LCD numbers:

> lcd n = concat .
>         intersperse "\n" .
>         foldr1 (zipWith (++)) .
>         intersperse (replicate (3 + 2*n) " ") .
>         map (scale n . (numbers !!))

`main` function:

> main = do
>   args <- getArgs
>   let (n, digits) = parseArgs args
>   putStrLn $ lcd n $ map digitToInt digits

Command-line argument parsing:

> data Flag = Scale Int
>             deriving Eq
>
> options = [Option "s" [] (ReqArg (Scale . read) "") ""]
>
> parseArgs args =
>   case parse args of
>    (_, [], _)              -> error "Usage: lcd [-s n] digits"
>    ([], digits, [])        -> (2, head digits)
>    ([Scale n], digits, []) -> (n, head digits)
>    (_, _, _)               -> error "Usage: lcd [-s n] digits"
>   where
>     parse = getOpt RequireOrder options
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/12/17/ruby-quiz-haskell-solution-lcd-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Myth of the Page Fold</title>
		<link>http://blog.tty.nl/2009/11/09/the-myth-of-the-page-fold/</link>
		<comments>http://blog.tty.nl/2009/11/09/the-myth-of-the-page-fold/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 09:17:47 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=256</guid>
		<description><![CDATA[Nice article dispelling the myth of the page fold being a impenetrable barrier for users. Update: page fold: myth or reality? The story continues: Scrolling and Attention]]></description>
			<content:encoded><![CDATA[<p>Nice <a href="http://www.cxpartners.co.uk/thoughts/the_myth_of_the_page_fold_evidence_from_user_testing.htm" target="_new">article</a> dispelling the myth of the page fold being a impenetrable barrier for users.</p>
<p>Update: <a href="http://webusability-blog.com/page-fold-myth-or-reality/" target="_blank">page fold: myth or reality</a>?</p>
<p>The story continues: <a href="http://www.useit.com/alertbox/scrolling-attention.html" target="_blank">Scrolling and Attention</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/11/09/the-myth-of-the-page-fold/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slides Haskell Workshop</title>
		<link>http://blog.tty.nl/2009/11/08/slides-haskell-workshop/</link>
		<comments>http://blog.tty.nl/2009/11/08/slides-haskell-workshop/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 12:05:01 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[devnology]]></category>
		<category><![CDATA[workshop]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=248</guid>
		<description><![CDATA[The slides for the workshop on Haskell and functional programming I gave yesterday at Devnology&#8216;s Community Day.]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.tty.nl/wp-content/uploads/devnology.jpg" alt="Haskell Workshop" title="Haskell Workshop" width="500" height="334" class="alignnone size-full wp-image-259" /></p>
<p>The slides for the workshop on Haskell and functional programming I gave yesterday at <a href="http://devnology.nl/" target="_blank">Devnology</a>&#8216;s Community Day.</p>
<div>
<object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=ifpuh-091108054940-phpapp02&#038;rel=0&#038;stripped_title=an-introduction-to-functional-programming-using-haskell" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=ifpuh-091108054940-phpapp02&#038;rel=0&#038;stripped_title=an-introduction-to-functional-programming-using-haskell" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/11/08/slides-haskell-workshop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby Quiz, Haskell Solution: Sampling</title>
		<link>http://blog.tty.nl/2009/09/27/ruby-quiz-haskell-solution-sampling/</link>
		<comments>http://blog.tty.nl/2009/09/27/ruby-quiz-haskell-solution-sampling/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 07:54:57 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Ruby Quiz]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=222</guid>
		<description><![CDATA[The Quiz A classic sampling problem: write a program sample which takes two integers n and m as input. n is the size of the sample. m is the size of the population. The program should print out n random &#8230; <a href="http://blog.tty.nl/2009/09/27/ruby-quiz-haskell-solution-sampling/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Quiz</h2>
<p>A classic sampling problem: write a program <tt>sample</tt> which takes two integers <tt>n</tt> and <tt>m</tt> as input. <tt>n</tt> is the size of the sample. <tt>m</tt> is the size of the population. The program should print out <tt>n</tt> random unique indices. Two example runs:</p>
<pre>
$ ./sample 3 10
0
2
8
$ ./sample 3 10
1
2
9
</pre>
<p>The output must be sorted. The complete, original quiz is <a href="http://rubyquiz.com/quiz39.html" target="_blank">here</a>.
</p>
<h2>A Haskell Solution</h2>
<h3>Take One</h3>
<p>My first (naïve) attempt uses a list of integers to represent the pool still available (i.e. the population not sampled yet). When it has to draw a sample it takes a random number <tt>i</tt> between 0 and the length of the list and removes the element at index <tt>i</tt> from the list, thus guaranteeing the uniqueness of the generated indices. It works correctly but it runs out of memory for the &quot;big sample&quot; (<tt>n= 5,000,000</tt> and <tt>m = 1,000,000,000</tt>) mentioned in the original quiz, not very suprising since it keeps both the current samples as well as the pool still availabe in memory. It is also quite slow because of the use of a plain list.</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span><br />
<br />
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span><span style="color: #339933; font-weight: bold;">.</span>State<br />
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List <span style="color: green;">&#40;</span>delete<span style="color: #339933; font-weight: bold;">,</span> sort<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> System <span style="color: green;">&#40;</span>getArgs<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Random<br />
<br />
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs<br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> n <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span>args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span><span style="color: #cccc00; font-weight: bold;">Int</span><br />
&nbsp; &nbsp; &nbsp; m <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span>args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><br />
&nbsp; gen <span style="color: #339933; font-weight: bold;">&lt;-</span> getStdGen<br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> <span style="font-weight: bold;">init</span> <span style="color: #339933; font-weight: bold;">=</span> RandomPool <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">..</span>m<span style="color: green;">&#93;</span> gen<br />
&nbsp; &nbsp; &nbsp; result <span style="color: #339933; font-weight: bold;">=</span> evalState <span style="color: green;">&#40;</span>sample n<span style="color: green;">&#41;</span> <span style="font-weight: bold;">init</span><br />
&nbsp; <span style="font-weight: bold;">mapM_</span> <span style="font-weight: bold;">print</span> <span style="color: green;">&#40;</span>sort result<span style="color: green;">&#41;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">data</span> RandomPool <span style="color: #339933; font-weight: bold;">=</span> RandomPool <span style="color: green;">&#123;</span> pool <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> gen <span style="color: #339933; font-weight: bold;">::</span> StdGen <span style="color: green;">&#125;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">type</span> StateRP <span style="color: #339933; font-weight: bold;">=</span> State RandomPool<br />
<br />
sample <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> StateRP <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span><br />
sample <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">return</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><br />
sample n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; st <span style="color: #339933; font-weight: bold;">&lt;-</span> get<br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> hi <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>pool st<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: green;">&#40;</span>i<span style="color: #339933; font-weight: bold;">,</span> gen'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> randomR <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span> hi<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>gen st<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; x <span style="color: #339933; font-weight: bold;">=</span> pool st <span style="color: #339933; font-weight: bold;">!!</span>i<br />
&nbsp; &nbsp; &nbsp; pool' <span style="color: #339933; font-weight: bold;">=</span> delete x <span style="color: green;">&#40;</span>pool st<span style="color: green;">&#41;</span><br />
&nbsp; put RandomPool <span style="color: green;">&#123;</span> pool <span style="color: #339933; font-weight: bold;">=</span> pool'<span style="color: #339933; font-weight: bold;">,</span> gen <span style="color: #339933; font-weight: bold;">=</span> gen' <span style="color: green;">&#125;</span><br />
&nbsp; xs <span style="color: #339933; font-weight: bold;">&lt;-</span> sample <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span><br />
&nbsp; <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span></div></div>
<h3>Take Two</h3>
<p>My second attempt solves the memory problem by keeping only the current samples in memory. When it has to draw a sample it takes a random number <tt>x</tt> between 0 and <tt>m</tt> and checks if that number has already been used. If the number has been used it tries agian. This solution also uses the <tt>Data.Set</tt> module for increased performance.</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span><br />
<br />
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span><span style="color: #339933; font-weight: bold;">.</span>State<br />
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List <span style="color: green;">&#40;</span>sort<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>Set <span style="color: #06c; font-weight: bold;">as</span> S <br />
<span style="color: #06c; font-weight: bold;">import</span> System <span style="color: green;">&#40;</span>getArgs<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Random<br />
<br />
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs<br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> n <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span>args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span><span style="color: #cccc00; font-weight: bold;">Int</span><br />
&nbsp; &nbsp; &nbsp; m <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span>args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><br />
&nbsp; gen <span style="color: #339933; font-weight: bold;">&lt;-</span> getStdGen<br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> <span style="font-weight: bold;">init</span> <span style="color: #339933; font-weight: bold;">=</span> RandomSet S<span style="color: #339933; font-weight: bold;">.</span>empty gen<br />
&nbsp; &nbsp; &nbsp; result <span style="color: #339933; font-weight: bold;">=</span> evalState <span style="color: green;">&#40;</span>sample m n<span style="color: green;">&#41;</span> <span style="font-weight: bold;">init</span><br />
&nbsp; <span style="font-weight: bold;">mapM_</span> <span style="font-weight: bold;">print</span> <span style="color: green;">&#40;</span>sort result<span style="color: green;">&#41;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">data</span> RandomSet <span style="color: #339933; font-weight: bold;">=</span> RandomSet <span style="color: green;">&#123;</span> set <span style="color: #339933; font-weight: bold;">::</span> S<span style="color: #339933; font-weight: bold;">.</span>Set <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">,</span> gen <span style="color: #339933; font-weight: bold;">::</span> StdGen <span style="color: green;">&#125;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">type</span> StateRS <span style="color: #339933; font-weight: bold;">=</span> State RandomSet<br />
<br />
sample <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> StateRS <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span><br />
sample hi n <span style="color: #339933; font-weight: bold;">=</span><br />
&nbsp; <span style="color: #06c; font-weight: bold;">if</span> n <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">then</span> <span style="color: #06c; font-weight: bold;">do</span> st <span style="color: #339933; font-weight: bold;">&lt;-</span> get<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span>toList <span style="color: green;">&#40;</span>set st<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">do</span> draw hi<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sample hi <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span><br />
<br />
draw <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> StateRS <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
draw hi <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; st <span style="color: #339933; font-weight: bold;">&lt;-</span> get<br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> gen'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> randomR <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span> hi <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>gen st<span style="color: green;">&#41;</span><br />
&nbsp; put st <span style="color: green;">&#123;</span> gen <span style="color: #339933; font-weight: bold;">=</span> gen' <span style="color: green;">&#125;</span><br />
&nbsp; <span style="color: #06c; font-weight: bold;">if</span> x `S<span style="color: #339933; font-weight: bold;">.</span>member` set st<br />
&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">then</span> draw hi<br />
&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;put st <span style="color: green;">&#123;</span> set <span style="color: #339933; font-weight: bold;">=</span> insert x <span style="color: green;">&#40;</span>set st<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></div></div>
<p>Here&apos;s an example run for the big sample. Note that I have to increase the maximum stack size for individual threads (<tt>+RTS -K250m</tt>) to prevent a stack space overflow:</p>
<pre>
$ time ./sample 5000000 1000000000 +RTS -K250m > big_sample.txt 

real    23m24.355s
user    23m1.658s
sys     0m9.548s
$ ls -l big_sample.txt
-rw-r--r--  1 mies  staff  49483467 Sep 27 17:13 big_sample.txt
$ head big_sample.txt
243
280
416
494
556
602
804
909
970
1126
$ tail big_sample.txt
999998483
999998863
999999002
999999028
999999052
999999053
999999115
999999291
999999853
999999870
</pre>
<p>The code plus solutions to other quizes is available on <a href="http://github.com/rmies/Quiz/" target="_blank">GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/09/27/ruby-quiz-haskell-solution-sampling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Quiz, Haskell Solution: Maximum Sub-Array</title>
		<link>http://blog.tty.nl/2009/08/30/ruby-quiz-haskell-solution-maximum-sub-array/</link>
		<comments>http://blog.tty.nl/2009/08/30/ruby-quiz-haskell-solution-maximum-sub-array/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 13:35:23 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Ruby Quiz]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=157</guid>
		<description><![CDATA[The Quiz Given an array of integers, find the sub-array with maximum sum. (The complete, original quiz is here.) A Haskell Solution module Main where import Data.List &#40;inits, maximumBy, tails&#41; import System &#40;getArgs&#41; maxSubArray :: &#91;Int&#93; -&#62; &#91;Int&#93; maxSubArray = &#8230; <a href="http://blog.tty.nl/2009/08/30/ruby-quiz-haskell-solution-maximum-sub-array/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Quiz</h2>
<p>Given an array of integers, find the sub-array with maximum sum. (The complete, original quiz is <a href="http://rubyquiz.com/quiz131.html" target="_blank">here</a>.)</p>
<h2>A Haskell Solution</h2>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span><br />
<br />
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List <span style="color: green;">&#40;</span>inits<span style="color: #339933; font-weight: bold;">,</span> maximumBy<span style="color: #339933; font-weight: bold;">,</span> tails<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> System <span style="color: green;">&#40;</span>getArgs<span style="color: green;">&#41;</span><br />
<br />
maxSubArray <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span><br />
maxSubArray <span style="color: #339933; font-weight: bold;">=</span><br />
&nbsp; maximumBy <span style="color: green;">&#40;</span>\ x y <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">compare</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">sum</span> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">sum</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">concatMap</span> inits <span style="color: #339933; font-weight: bold;">.</span> tails<br />
<br />
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs<br />
&nbsp; <span style="font-weight: bold;">print</span> <span style="color: green;">&#40;</span>maxSubArray <span style="color: green;">&#40;</span><span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> args<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/08/30/ruby-quiz-haskell-solution-maximum-sub-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Quiz, Haskell Solution: Happy Numbers</title>
		<link>http://blog.tty.nl/2009/08/24/ruby-quiz-haskell-solution-happy-numbers/</link>
		<comments>http://blog.tty.nl/2009/08/24/ruby-quiz-haskell-solution-happy-numbers/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 21:21:16 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Ruby Quiz]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=125</guid>
		<description><![CDATA[The Quiz Write a program that tells whether a given integer is happy. A happy number is found using the following process: Take the sum of the squares of its digits, and continue iterating this process until it yields 1, &#8230; <a href="http://blog.tty.nl/2009/08/24/ruby-quiz-haskell-solution-happy-numbers/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Quiz</h2>
<p>Write a program that tells whether a given integer is happy. A happy number is found using the following process: Take the sum of the squares of its digits, and continue iterating this process until it yields 1, or produces an infinite loop. (The complete, original quiz is <a href="http://rubyquiz.com/quiz93.html" target="_blank">here</a>.)</p>
<h2>A Haskell Solution</h2>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span><br />
<br />
<span style="color: #06c; font-weight: bold;">import</span> System <span style="color: green;">&#40;</span>getArgs<span style="color: green;">&#41;</span><br />
<br />
digits <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span><br />
digits <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\c <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">read</span> <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span><br />
<br />
happy <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span><br />
happy n <span style="color: #339933; font-weight: bold;">=</span> happy' n <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><br />
&nbsp; <span style="color: #06c; font-weight: bold;">where</span><br />
&nbsp; &nbsp; s <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> x <span style="color: #339933; font-weight: bold;">*</span> x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> digits<br />
&nbsp; &nbsp; happy' n ns<br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> s n <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">1</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933; font-weight: bold;">=</span> True<br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> s n `<span style="font-weight: bold;">elem</span>` ns <span style="color: #339933; font-weight: bold;">=</span> False<br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> happy' <span style="color: green;">&#40;</span>s n<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n : ns<span style="color: green;">&#41;</span><br />
<br />
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs<br />
&nbsp; <span style="color: #06c; font-weight: bold;">if</span> happy <span style="color: green;">&#40;</span><span style="font-weight: bold;">read</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> args<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">then</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;:-)&quot;</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">else</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;:-(&quot;</span><br />
&nbsp; <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/08/24/ruby-quiz-haskell-solution-happy-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cake&#8217;s Progress</title>
		<link>http://blog.tty.nl/2009/08/24/cakes-progress/</link>
		<comments>http://blog.tty.nl/2009/08/24/cakes-progress/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 17:56:25 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Cake]]></category>
		<category><![CDATA[Open Source Projects]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=113</guid>
		<description><![CDATA[Cake is progressing rather slowly at the moment. Its next release (0.2) is about receiving requests, and thus will involve lots of networking code. Since I know very little about network programming I am first working my way through &#34;UNIX &#8230; <a href="http://blog.tty.nl/2009/08/24/cakes-progress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/rmies/Cake/tree/master" target="_blank">Cake</a> is progressing rather slowly at the moment. Its next release (0.2) is about receiving requests, and thus will involve lots of networking code. Since I know very little about network programming I am first working my way through <a href="http://www.unpbook.com/" target="_blank">&quot;UNIX Network Programming&quot;, Volume 1</a>.</p>
<p>In the meantime, to still do some Haskell coding, I&#8217;m trying to solve some <a href="http://perl.plover.com/qotw/" target="_blank">Perl</a> and <a href="http://rubyquiz.com/" target="_blank">Ruby</a> quizzes in Haskell. I&#8217;ll post my solutions plus explanations here.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/08/24/cakes-progress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl Quiz, Haskell Solution: Plusified Equations</title>
		<link>http://blog.tty.nl/2009/08/19/haskell-solution-to-perl-quiz-of-the-week-plusified-equations/</link>
		<comments>http://blog.tty.nl/2009/08/19/haskell-solution-to-perl-quiz-of-the-week-plusified-equations/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 11:24:01 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Perl Quiz of the Week]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=89</guid>
		<description><![CDATA[The Quiz We are given two positive integers $L and $R we need to find Plusified expressions of both for which Eval($E_L) == Eval($E_R). So what is a plusified expression? It is an expression where we can choose whether to &#8230; <a href="http://blog.tty.nl/2009/08/19/haskell-solution-to-perl-quiz-of-the-week-plusified-equations/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Quiz</h2>
<p>
We are given two positive integers <tt>$L</tt> and <tt>$R</tt> we need to find Plusified<br />
expressions of both for which <tt>Eval($E_L) == Eval($E_R)</tt>. So what is a plusified<br />
expression? It is an expression where we can choose whether to add a single<br />
&quot;+&quot; between any consecutive digit. So for example the number 123 has the<br />
following plusified expression:</p>
<ul>
<li>123</li>
<li>12+3</li>
<li>1+23</li>
<li>1+2+3</li>
</ul>
<p>So if we are given 123 and 96 we can form the following plusified equation:</p>
<ul>
<li>12+3 == 9+6</li>
</ul>
<p>
Your mission is to write a Perl program (or an equivalent program in any<br />
programming language) that will find all solutions to the plusified equation<br />
of two numbers given as input. To normalise the output we&#8217;ll rule that:</p>
<ol>
<li>The equations should be given one at each line.</li>
<li>They will be sorted so consecutive digits will take precedence over &quot;+&quot;&apos;s.</li>
<li>A &quot;+&quot; has no surrounding spaces.</li>
<li>The = sign does have a preceding and following space.</li>
</ol>
<h2>A Haskell Solution</h2>
<p>Listing of the complete solution:</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span><br />
<br />
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List <span style="color: green;">&#40;</span><span style="font-weight: bold;">iterate</span><span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> System <span style="color: green;">&#40;</span>getArgs<span style="color: green;">&#41;</span><br />
<br />
split <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Char</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">String</span><span style="color: green;">&#93;</span><br />
split delim s <span style="color: #339933; font-weight: bold;">=</span> <br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>s'<span style="color: #339933; font-weight: bold;">,</span> s''<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">break</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">==</span> delim<span style="color: green;">&#41;</span> s<br />
&nbsp; <span style="color: #06c; font-weight: bold;">in</span> s' : <span style="color: #06c; font-weight: bold;">case</span> s'' <span style="color: #06c; font-weight: bold;">of</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; delim : <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> split delim <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> s''<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><br />
<br />
plusify <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">String</span><span style="color: green;">&#93;</span><br />
plusify <span style="background-color: #3cb371;">&quot;&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;&quot;</span><span style="color: green;">&#93;</span><br />
plusify <span style="color: green;">&#40;</span>c : <span style="background-color: #3cb371;">&quot;&quot;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>c : <span style="background-color: #3cb371;">&quot;&quot;</span><span style="color: green;">&#93;</span><br />
plusify <span style="color: green;">&#40;</span>c : cs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">concatMap</span> <span style="color: green;">&#40;</span>\cs' <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>c : cs'<span style="color: #339933; font-weight: bold;">,</span> c : '<span style="color: #339933; font-weight: bold;">+</span>' : cs'<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>plusify cs<span style="color: green;">&#41;</span><br />
<br />
combis <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">String</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">String</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><br />
combis x y <span style="color: #339933; font-weight: bold;">=</span><br />
&nbsp; <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> l <span style="color: #339933; font-weight: bold;">&lt;-</span> plusify x<span style="color: #339933; font-weight: bold;">,</span> r <span style="color: #339933; font-weight: bold;">&lt;-</span> plusify y<span style="color: #339933; font-weight: bold;">,</span> <span style="font-weight: bold;">sum</span>' l <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">sum</span>' r<span style="color: green;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">where</span> <span style="font-weight: bold;">sum</span>' <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\s <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">read</span> s <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> split '<span style="color: #339933; font-weight: bold;">+</span>'<br />
<br />
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs<br />
&nbsp; <span style="font-weight: bold;">mapM_</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">.</span> \<span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> l <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot; = &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> r<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>combis <span style="color: green;">&#40;</span>args<span style="color: #339933; font-weight: bold;">!!</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>args<span style="color: #339933; font-weight: bold;">!!</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><br />
&nbsp; <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/08/19/haskell-solution-to-perl-quiz-of-the-week-plusified-equations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

