<?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; Ruby Quiz</title>
	<atom:link href="http://blog.tty.nl/category/ruby/ruby-quiz/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>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>
	</channel>
</rss>

