<?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; Haskell</title>
	<atom:link href="http://blog.tty.nl/category/haskell/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>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>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>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>
		<item>
		<title>Real World Haskell &#8211; Exercise Chapter 14</title>
		<link>http://blog.tty.nl/2009/06/21/real-world-haskell-exercise-chapter-14/</link>
		<comments>http://blog.tty.nl/2009/06/21/real-world-haskell-exercise-chapter-14/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 11:11:52 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Real World Haskell]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=83</guid>
		<description><![CDATA[My solution to the exercise on p. 352. 1.Rewrite getRandom to use do notation. getRandom :: Random a =&#62; RandomState a getRandom = do &#160; gen &#60;- get &#160; let &#40;val,gen'&#41; = random gen &#160; put gen' &#160; return val]]></description>
			<content:encoded><![CDATA[<p>My solution to the exercise on p. 352.</p>
<blockquote><p>
1.Rewrite <tt>getRandom</tt> to use <tt>do</tt> notation.
</p></blockquote>
<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">getRandom <span style="color: #339933; font-weight: bold;">::</span> Random a <span style="color: #339933; font-weight: bold;">=&gt;</span> RandomState a<br />
getRandom <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span><br />
&nbsp; gen <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>val<span style="color: #339933; font-weight: bold;">,</span>gen'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> random gen<br />
&nbsp; put gen'<br />
&nbsp; <span style="font-weight: bold;">return</span> val</div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/06/21/real-world-haskell-exercise-chapter-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Haskell Snippets</title>
		<link>http://blog.tty.nl/2009/06/01/haskell-snippets/</link>
		<comments>http://blog.tty.nl/2009/06/01/haskell-snippets/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 12:45:03 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=80</guid>
		<description><![CDATA[I&apos;ve started a repository of Haskell snippets for Emacs and YASnippet. It&apos;s hosted on GitHub.]]></description>
			<content:encoded><![CDATA[<p>I&apos;ve started a repository of <a href="http://haskell.org/" target="_blank">Haskell</a> snippets for <a href="http://www.gnu.org/software/emacs/" target="_blank">Emacs</a> and <a href="http://code.google.com/p/yasnippet/" target="_blank">YASnippet</a>. It&apos;s hosted on <a href="http://github.com/rmies/yasnippet-haskell/tree/master" target="_blank">GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/06/01/haskell-snippets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Real World Haskell &#8211; Exercises Chapter 8</title>
		<link>http://blog.tty.nl/2009/05/05/real-world-haskell-exercises-chapter-8/</link>
		<comments>http://blog.tty.nl/2009/05/05/real-world-haskell-exercises-chapter-8/#comments</comments>
		<pubDate>Tue, 05 May 2009 19:00:01 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Real World Haskell]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=69</guid>
		<description><![CDATA[My answers to the exercises of chapter 8. Page 205: 2. While filesystems on Unix are usually sensitive to case (e.g. &#8220;G&#8221; vs. &#8220;g&#8221;) in file names, Windows filesystems are not. Add a parameter to the globToRegex and matchesGlob functions &#8230; <a href="http://blog.tty.nl/2009/05/05/real-world-haskell-exercises-chapter-8/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My answers to the exercises of chapter 8.</p>
<p>Page 205:<br />
<!-- Exercise 2 --></p>
<blockquote><p>2. While filesystems on Unix are usually sensitive to case (e.g. &#8220;G&#8221; vs. &#8220;g&#8221;) in file names, Windows filesystems are not. Add a parameter to the <tt>globToRegex</tt> and <tt>matchesGlob</tt> functions that allows control over case sensitive matching.</p></blockquote>
<p>Note that the solution below ignores ranges in character classes.</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> GlobRegex<br />
&nbsp; <span style="color: green;">&#40;</span> globToRegex<br />
&nbsp; <span style="color: #339933; font-weight: bold;">,</span> matchesGlob<br />
&nbsp; <span style="color: green;">&#41;</span> <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><span style="color: #cccc00; font-weight: bold;">Char</span> <span style="color: green;">&#40;</span>isLower<span style="color: #339933; font-weight: bold;">,</span>isUpper<span style="color: #339933; font-weight: bold;">,</span>toLower<span style="color: #339933; font-weight: bold;">,</span>toUpper<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>List <span style="color: green;">&#40;</span>nub<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Regex<span style="color: #339933; font-weight: bold;">.</span>Posix <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">=~</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><br />
<br />
globToRegex <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> Bool<span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">String</span><br />
globToRegex cs matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> '<span style="color: #339933; font-weight: bold;">^</span>' : globToRegex' cs matchCase <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;$&quot;</span><br />
<br />
globToRegex' <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;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">String</span><br />
globToRegex' <span style="background-color: #3cb371;">&quot;&quot;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;&quot;</span><br />
<br />
globToRegex' <span style="color: green;">&#40;</span>'<span style="color: #339933; font-weight: bold;">*</span>':cs<span style="color: green;">&#41;</span> matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;.*&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> globToRegex' cs matchCase<br />
<br />
globToRegex' <span style="color: green;">&#40;</span>'<span style="color: #339933; font-weight: bold;">?</span>':cs<span style="color: green;">&#41;</span> matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> '<span style="color: #339933; font-weight: bold;">.</span>' : globToRegex' cs matchCase<br />
<br />
globToRegex' <span style="color: green;">&#40;</span>'<span style="color: green;">&#91;</span>':'<span style="color: #339933; font-weight: bold;">!</span>':c:cs<span style="color: green;">&#41;</span> matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> c' <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> matchCase<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">else</span> nub <span style="color: green;">&#40;</span>c:shiftCase c:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">in</span> <span style="background-color: #3cb371;">&quot;[^&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> c' <span style="color: #339933; font-weight: bold;">++</span> charClass cs matchCase<br />
<br />
globToRegex' <span style="color: green;">&#40;</span>'<span style="color: green;">&#91;</span>':c:cs<span style="color: green;">&#41;</span> matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> c' <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> matchCase<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">else</span> nub <span style="color: green;">&#40;</span>c:shiftCase c:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">in</span> '<span style="color: green;">&#91;</span>' : c' <span style="color: #339933; font-weight: bold;">++</span> charClass cs matchCase<br />
<br />
globToRegex' <span style="color: green;">&#40;</span>'<span style="color: green;">&#91;</span>':<span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">error</span> <span style="background-color: #3cb371;">&quot;unterminated character class&quot;</span><br />
<br />
globToRegex' <span style="color: green;">&#40;</span>c:cs<span style="color: green;">&#41;</span> matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> escape c matchCase <span style="color: #339933; font-weight: bold;">++</span> globToRegex' cs matchCase<br />
<br />
escape <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;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">String</span><br />
escape c matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">|</span> c `<span style="font-weight: bold;">elem</span>` regexChars <span style="color: #339933; font-weight: bold;">=</span> '\' : <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span><br />
&nbsp; <span style="color: #339933; font-weight: bold;">|</span> matchCase <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span><br />
&nbsp; <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> '<span style="color: green;">&#91;</span>' : nub <span style="color: green;">&#40;</span>c:shiftCase c:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;]&quot;</span><br />
&nbsp; <span style="color: #06c; font-weight: bold;">where</span> regexChars <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\\</span>+()^$.{}]|&quot;</span><br />
<br />
charClass <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;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">String</span><br />
charClass <span style="color: green;">&#40;</span>'<span style="color: green;">&#93;</span>':cs<span style="color: green;">&#41;</span> matchCase <span style="color: #339933; font-weight: bold;">=</span> '<span style="color: green;">&#93;</span>' : globToRegex' cs matchCase<br />
charClass <span style="color: green;">&#40;</span>c:cs<span style="color: green;">&#41;</span> True &nbsp;<span style="color: #339933; font-weight: bold;">=</span> c : charClass cs True<br />
charClass <span style="color: green;">&#40;</span>c:cs<span style="color: green;">&#41;</span> False <span style="color: #339933; font-weight: bold;">=</span> c' <span style="color: #339933; font-weight: bold;">++</span> charClass cs False<br />
&nbsp; <span style="color: #06c; font-weight: bold;">where</span> c' <span style="color: #339933; font-weight: bold;">=</span> nub <span style="color: green;">&#40;</span>c:shiftCase c:<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><br />
charClass <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">error</span> <span style="background-color: #3cb371;">&quot;unterminated character class&quot;</span><br />
<br />
shiftCase <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;">Char</span><br />
shiftCase c <span style="color: #339933; font-weight: bold;">|</span> isLower c <span style="color: #339933; font-weight: bold;">=</span> toUpper c<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> isUpper c <span style="color: #339933; font-weight: bold;">=</span> toLower c<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> c<br />
<br />
matchesGlob <span style="color: #339933; font-weight: bold;">::</span> FilePath <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: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span><br />
matchesGlob name pat matchCase<br />
&nbsp; <span style="color: #339933; font-weight: bold;">=</span> name <span style="color: #339933; font-weight: bold;">=~</span> globToRegex pat matchCase</div></div>
<p>Page 212:</p>
<blockquote><p>1. Glob patterns are simple enough to interpret that it&apos;s easy to write a matcher directly in Haskell, rather than going through the regexp machinery. Give it a try.</p></blockquote>
<p>Note: I forgot to implement negated character classes.</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> MatchGlob <span style="color: green;">&#40;</span>match<span style="color: green;">&#41;</span> <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>tails<span style="color: green;">&#41;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">type</span> Glob <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #cccc00; font-weight: bold;">String</span><br />
<br />
match <span style="color: #339933; font-weight: bold;">::</span> Glob <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: #cccc00; font-weight: bold;">Bool</span><br />
<br />
match <span style="background-color: #3cb371;">&quot;&quot;</span> &nbsp;<span style="background-color: #3cb371;">&quot;&quot;</span> <span style="color: #339933; font-weight: bold;">=</span> True<br />
match <span style="background-color: #3cb371;">&quot;&quot;</span> &nbsp;<span style="color: #339933; font-weight: bold;">_</span> &nbsp;<span style="color: #339933; font-weight: bold;">=</span> False<br />
match <span style="background-color: #3cb371;">&quot;*&quot;</span> <span style="background-color: #3cb371;">&quot;&quot;</span> <span style="color: #339933; font-weight: bold;">=</span> True<br />
match <span style="color: #339933; font-weight: bold;">_</span> &nbsp; <span style="background-color: #3cb371;">&quot;&quot;</span> <span style="color: #339933; font-weight: bold;">=</span> False<br />
<br />
match <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> z<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>y:ys<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span><br />
&nbsp; <span style="color: #06c; font-weight: bold;">case</span> x <span style="color: #06c; font-weight: bold;">of</span><br />
&nbsp; &nbsp; '<span style="color: #339933; font-weight: bold;">?</span>' &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">-&gt;</span> match xs ys<br />
&nbsp; &nbsp; '\' &nbsp; &nbsp; &nbsp;<span style="color: #339933; font-weight: bold;">-&gt;</span> matchEscape xs z<br />
&nbsp; &nbsp; '<span style="color: #339933; font-weight: bold;">*</span>' &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">-&gt;</span> matchStar xs z<br />
&nbsp; &nbsp; '<span style="color: green;">&#91;</span>' &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">-&gt;</span> matchClass xs z<br />
&nbsp; &nbsp; <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> x <span style="color: #339933; font-weight: bold;">==</span> y <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> match xs ys<br />
<br />
matchEscape <span style="color: #339933; font-weight: bold;">::</span> Glob <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: #cccc00; font-weight: bold;">Bool</span><br />
matchEscape <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">error</span> <span style="background-color: #3cb371;">&quot;unescaped backslash&quot;</span><br />
matchEscape <span style="color: #339933; font-weight: bold;">_</span> <span style="background-color: #3cb371;">&quot;&quot;</span> <span style="color: #339933; font-weight: bold;">=</span> False<br />
matchEscape <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>y:ys<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">==</span> y <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> match xs ys<br />
<br />
matchStar <span style="color: #339933; font-weight: bold;">::</span> Glob <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: #cccc00; font-weight: bold;">Bool</span><br />
matchStar x y <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">or</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>match x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>tails y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><br />
<br />
matchClass <span style="color: #339933; font-weight: bold;">::</span> Glob <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: #cccc00; font-weight: bold;">Bool</span><br />
matchClass <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>y:ys<span style="color: green;">&#41;</span> <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>xs'<span style="color: #339933; font-weight: bold;">,</span>zs<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> '<span style="color: green;">&#93;</span>'<span style="color: green;">&#41;</span> xs<br />
&nbsp; <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">if</span> zs <span style="color: #339933; font-weight: bold;">==</span> <span style="background-color: #3cb371;">&quot;&quot;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">then</span> <span style="font-weight: bold;">error</span> <span style="background-color: #3cb371;">&quot;unterminated character class&quot;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">else</span> &nbsp;y `<span style="font-weight: bold;">elem</span>` expand <span style="color: green;">&#40;</span>x:xs'<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> match <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> zs<span style="color: green;">&#41;</span> ys<br />
matchClass <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">error</span> <span style="background-color: #3cb371;">&quot;illegal character class&quot;</span><br />
<br />
expand <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><br />
expand <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;&quot;</span><br />
expand <span style="color: green;">&#40;</span>x:'<span style="color: #339933; font-weight: bold;">-</span>':<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> &nbsp; <span style="color: #339933; font-weight: bold;">=</span> x:'<span style="color: #339933; font-weight: bold;">-</span>':<span style="background-color: #3cb371;">&quot;&quot;</span><br />
expand <span style="color: green;">&#40;</span>x:'<span style="color: #339933; font-weight: bold;">-</span>':y:zs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x<span style="color: #339933; font-weight: bold;">..</span>y<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> expand zs<br />
expand <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> x:expand xs</div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/05/05/real-world-haskell-exercises-chapter-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Real World Haskell &#8211; Exercises Chapter 4</title>
		<link>http://blog.tty.nl/2009/04/06/real-world-haskell-exercises-chapter-4/</link>
		<comments>http://blog.tty.nl/2009/04/06/real-world-haskell-exercises-chapter-4/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 04:46:12 +0000</pubDate>
		<dc:creator>Michel Rijnders</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Real World Haskell]]></category>

		<guid isPermaLink="false">http://blog.tty.nl/?p=66</guid>
		<description><![CDATA[My answers to the exercises of the fourth chapter. Page 84: 1. Write your own &#8220;safe&#8221; definitions of the standard partial list functions, but make sure they never fail. safeHead :: &#91;a&#93; -&#62; Maybe a safeHead &#91;&#93; = Nothing safeHead &#8230; <a href="http://blog.tty.nl/2009/04/06/real-world-haskell-exercises-chapter-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My answers to the exercises of the fourth chapter.</p>
<p>Page 84:<br />
<!-- Exercise 1 --></p>
<blockquote><p>
1. Write your own &#8220;safe&#8221; definitions of the standard partial list functions, but make sure they never fail.
</p></blockquote>
<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">safeHead <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Maybe</span> a<br />
safeHead <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> Nothing<br />
safeHead xs <span style="color: #339933; font-weight: bold;">=</span> Just <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> xs<span style="color: green;">&#41;</span><br />
<br />
safeTail <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Maybe</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><br />
safeTail <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> Nothing<br />
safeTail xs <span style="color: #339933; font-weight: bold;">=</span> Just <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> xs<span style="color: green;">&#41;</span><br />
<br />
safeLast <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Maybe</span> a<br />
safeLast <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> Nothing<br />
safeLast xs <span style="color: #339933; font-weight: bold;">=</span> Just <span style="color: green;">&#40;</span><span style="font-weight: bold;">last</span> xs<span style="color: green;">&#41;</span><br />
<br />
safeInit <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Maybe</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><br />
safeInit <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> Nothing<br />
safeInit xs <span style="color: #339933; font-weight: bold;">=</span> Just <span style="color: green;">&#40;</span><span style="font-weight: bold;">init</span> xs<span style="color: green;">&#41;</span></div></div>
<p><!-- Exercise 2 --></p>
<blockquote><p>
2. Write a function <tt>splitWith</tt> that acts similarly to <tt>words</tt> but takes a predicate and a list of any type, and then splits its input list on every element for which the predicate returns <tt>False</tt>.
</p></blockquote>
<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">splitWith <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span><br />
splitWith p xs <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>pre<span style="color: #339933; font-weight: bold;">,</span>suf<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">break</span> p xs<br />
&nbsp; &nbsp; &nbsp; suf' <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">dropWhile</span> p suf <br />
&nbsp; <span style="color: #06c; font-weight: bold;">in</span> pre : <span style="color: #06c; font-weight: bold;">case</span> suf' <span style="color: #06c; font-weight: bold;">of</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: green;">&#91;</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: green;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933; font-weight: bold;">_</span> &nbsp;<span style="color: #339933; font-weight: bold;">-&gt;</span> splitWith p suf'</div></div>
<p>Page 97:<br />
<!-- Exercise 1 --></p>
<blockquote><p>
1. Use a fold (choosing the appropriate fold will make your code much simpler) to rewrite and improve upon the asInt function from the section called “Explicit recursion” on page 85.
</p></blockquote>
<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;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Char</span> <span style="color: green;">&#40;</span>digitToInt<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>List <span style="color: green;">&#40;</span><span style="font-weight: bold;">foldl</span>'<span style="color: green;">&#41;</span><br />
<br />
asInt<span style="color: #339933; font-weight: bold;">_</span>fold <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;">Int</span><br />
asInt<span style="color: #339933; font-weight: bold;">_</span>fold <span style="color: green;">&#40;</span>'<span style="color: #339933; font-weight: bold;">-</span>':xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">*</span> asInt<span style="color: #339933; font-weight: bold;">_</span>fold xs<br />
asInt<span style="color: #339933; font-weight: bold;">_</span>fold xs &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldl</span>' step <span style="color: red;">0</span> xs<br />
&nbsp; <span style="color: #06c; font-weight: bold;">where</span><br />
&nbsp; &nbsp; step <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;">Char</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span><br />
&nbsp; &nbsp; step x y <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">10</span> <span style="color: #339933; font-weight: bold;">*</span> x <span style="color: #339933; font-weight: bold;">+</span> digitToInt y</div></div>
<p><!-- Exercise 6 --></p>
<blockquote><p>
6. Write your own definition of <tt>concat</tt> using <tt>foldr</tt>.
</p></blockquote>
<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">myConcat <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">foldr</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tty.nl/2009/04/06/real-world-haskell-exercises-chapter-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

