<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Tag sorting</title>
    <link>http://blog.moertel.com/articles/tag/sorting?tag=sorting</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Introductory Haskell: Solving the Sorting-It-Out Kata</title>
      <description>&lt;p&gt;Last Tuesday, my friend &lt;a href="http://caseywest.com/"&gt;Casey&lt;/a&gt; and I were hanging
out at &lt;a href="http://aldocoffee.com/"&gt;Aldo Coffee&lt;/a&gt;.  We planned on enjoying
some espresso, doing some work, and then heading over to the &lt;a href="http://pghcodingdojo.org/"&gt;Pittsburgh Coding
Dojo&lt;/a&gt;, where we could hang out with
other geekly folks.
We ended up
not having enough time to go to the meeting, but we decided to hack
on the challenge problem anyway, using Aldo&amp;#8217;s ever-handy free
wireless to access the Internet.&lt;/p&gt;


	&lt;p&gt;The Dojo problem was PragDave&amp;#8217;s &lt;a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Practices/Kata/KataEleven.rdoc"&gt;Kata Eleven &amp;#8211; Sorting it
Out&lt;/a&gt;.  (It&amp;#8217;s short;
read it now.)  We decided to use Haskell for our implementation
language.&lt;/p&gt;


	&lt;p&gt;In this post, I&amp;#8217;ll walk through our coding session and explain how our
solution evolved.  To better fit the session into a blog post, I
have removed a lot of back-and-forth micro iterations, and I have
edited some of the code for clarity.&lt;/p&gt;


	&lt;h3&gt; The first part of the problem&lt;/h3&gt;


	&lt;p&gt;The first part of the problem was &amp;#8220;Sorting Balls.&amp;#8221;  The story: You
need to implement a &amp;#8220;rack&amp;#8221; to hold the balls drawn at random (without
replacement) from a bin containing sixty balls, numbered 0 to 59.
Regardless of the order in which the balls are added to the rack, you
need to present them in sorted order whenever you&amp;#8217;re asked for them.&lt;/p&gt;


	&lt;p&gt;Upon reading this part of the challenge, a couple of thoughts sprung to mind:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Because the range of balls is so small, the problem was begging for a solution based on a &lt;a href="http://en.wikipedia.org/wiki/Counting_sort"&gt;counting sort&lt;/a&gt;.&lt;/li&gt;
		&lt;li&gt;Because the balls are uniquely numbered and drawn without replacement, we could even use a bit vector to represent counts.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Nevertheless, we decided to ignore these thoughts and implement a
more-general solution that would work for any (orderable) values,
not just small ranges of integers.&lt;/p&gt;


	&lt;h3&gt; Sketching the interface&lt;/h3&gt;


	&lt;p&gt;The first step, then, was to sketch out an interface.  Our
interface mirrored the one from the problem statement but
was tweaked for Haskell:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;
&lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;
&lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The function &lt;em&gt;mkRack&lt;/em&gt; makes a new rack to hold values (&amp;#8220;balls&amp;#8221;) of
type &lt;em&gt;a&lt;/em&gt;.  It&amp;#8217;s equivalent to &lt;code&gt;Rack.new&lt;/code&gt; in Ruby.&lt;/p&gt;


	&lt;p&gt;The &lt;em&gt;add&lt;/em&gt; function adds a ball to a rack.  You give it a ball and a
rack, and it returns a new rack that is the same as the original rack
but also contains the ball.  (If you&amp;#8217;re accustomed to stateful
programming, this may seem weird.  Why return a new rack instead of
modifying the original rack? Because, in Haskell, you can&amp;#8217;t change
values: you can only create new values.  At first, this constraint may
seem limiting, but after you get used to it, you&amp;#8217;ll find it
empowering.)&lt;/p&gt;


	&lt;p&gt;Note: the&lt;code&gt; Ord a &lt;/code&gt;qualification on the type signature of
&lt;em&gt;add&lt;/em&gt; says that it will work for any type &lt;em&gt;a&lt;/em&gt; whose values can be
ordered.  The qualification is necessary because values of some types,
like IO actions, cannot be compared to see which are less than the
others.&lt;/p&gt;


	&lt;p&gt;The &lt;em&gt;balls&lt;/em&gt; function is an &amp;#8220;observer&amp;#8221;: it lets you observe the balls
in a rack by returning them as an ordered list.&lt;/p&gt;


	&lt;p&gt;And that&amp;#8217;s the interface.&lt;/p&gt;


	&lt;p&gt;With the interface sketched, we gave it meaning by defining its
properties.&lt;/p&gt;


	&lt;h3&gt; Giving our interface meaning: defining properties using QuickCheck&lt;/h3&gt;


	&lt;p&gt;&lt;a href="http://www.md.chalmers.se/~rjmh/QuickCheck/"&gt;QuickCheck&lt;/a&gt; is a
powerful, easy-to-use testing tool.  Instead of checking test cases,
it checks &lt;em&gt;properties&lt;/em&gt; &amp;#8211; statements about what your code ought to do
&lt;em&gt;in general&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;The great thing about QuickCheck properties is that they are
&lt;em&gt;testable documentation&lt;/em&gt;.  They tell the world what your code
is supposed to do,
and they do so in a concise, formal language that just happens to be
easily readable by humans and automatically testable by computers.&lt;/p&gt;


	&lt;p&gt;To specify the desired properties of our Rack interface, we first had
to import QuickCheck:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Test&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;QuickCheck&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Then, we defined our first property.  It said, simply, that a new rack
must be empty when observed:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_New&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Our second property said that, when you add a ball &lt;em&gt;x&lt;/em&gt; to
a rack, the resulting rack must contain the same
balls as the original rack plus &lt;em&gt;x&lt;/em&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_AddAddsElement&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Both of the properties above rely upon a special, order-insensitive
equality test that we defined for lists of &lt;code&gt;Int&lt;/code&gt; values:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;=~&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;Int&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;Int&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Bool&lt;/span&gt;
&lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;sort&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='varid'&gt;sort&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Note that under this test, &lt;code&gt;[1,2]&lt;/code&gt; &amp;#8220;equals&amp;#8221; 
both &lt;code&gt;[1,2]&lt;/code&gt; and &lt;code&gt;[2,1]&lt;/code&gt;, but it does not &amp;#8220;equal&amp;#8221; 
any other values.&lt;/p&gt;


	&lt;p&gt;The reason we defined this operator was to help us specify the two
essential properties of &lt;em&gt;add&lt;/em&gt; separately: (1) it must insert a ball
into a rack, and (2) the new ball&amp;#8217;s position, when observed, must
preserve the rack&amp;#8217;s ordering invariant.  The previous property
definition used the &lt;code&gt;=~&lt;/code&gt; operator to specify the first of
these two properties.  The next property we defined specified the
second:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_AddPreservesOrdering&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;==&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This definition specifies that, for all racks &lt;em&gt;rack&lt;/em&gt; and all balls
&lt;em&gt;x&lt;/em&gt;, if the balls in &lt;em&gt;rack&lt;/em&gt; are ordered, the balls in the rack that
results from adding &lt;em&gt;x&lt;/em&gt; to &lt;em&gt;rack&lt;/em&gt; must also be ordered.  If you
are familiar with &lt;a href="http://en.wikipedia.org/wiki/Mathematical_induction"&gt;proof by
induction&lt;/a&gt;, you&amp;#8217;ll
know why we went this route.  In short, if we can prove that this
property holds (and, trivially, that an empty rack is ordered), we can
prove that &lt;em&gt;add&lt;/em&gt; preserves the ordering invariant.&lt;/p&gt;


	&lt;p&gt;To round out the property definition, we needed to define the &lt;em&gt;isOrdered&lt;/em&gt; test:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;Int&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Bool&lt;/span&gt;
&lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='varid'&gt;sort&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And those are the properties we needed to check the correctness
of our implementation.  Of course, we still needed to &lt;em&gt;write&lt;/em&gt; our
implementation, and we turned to that task next.&lt;/p&gt;


	&lt;h3&gt; A simple, list-based Rack implementation&lt;/h3&gt;


	&lt;p&gt;For our first implementation, we decided upon a drop-dead-simple
list-based representation.  We would keep the elements of the list
in sorted order by inserting them into the correct positions when
&lt;em&gt;add&lt;/em&gt; was called.&lt;/p&gt;


	&lt;p&gt;Here, then, was our code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Our list-based implementation of a Rack&lt;/span&gt;

&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;mkRack&lt;/span&gt;   &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;span class='varid'&gt;balls&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;

&lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;ys&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;&amp;lt;&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;         &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;otherwise&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;That&amp;#8217;s it.&lt;/p&gt;


	&lt;p&gt;We took our new implementation for a spin in GHCi:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; balls mkRack
[]

*Rack&amp;gt; balls (add 3 mkRack)
[3]

*Rack&amp;gt; balls (add 4 (add 3 mkRack))
[3,4]

*Rack&amp;gt; balls (add 1 (add 4 (add 3 mkRack)))
[1,3,4]

*Rack&amp;gt; balls (foldr add mkRack [4,2,6,3,-9,0,33,9])
[-9,0,2,3,4,6,9,33]
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;To &lt;em&gt;really&lt;/em&gt; test our implementation, we asked QuickCheck to check its
properties:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; quickCheck prop_New
OK, passed 100 tests.

*Rack&amp;gt; quickCheck prop_AddAddsElement
OK, passed 100 tests.

*Rack&amp;gt; quickCheck prop_AddPreservesOrdering
OK, passed 100 tests.
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I should point out that QuickCheck did not &lt;em&gt;prove&lt;/em&gt; that our properties
held.  Rather, it gathered evidence that we could use to &lt;em&gt;argue&lt;/em&gt; that
our properties held.  The evidence was that each of our properties&amp;#8217;
claims was subjected to 100 randomly generated tests, and none of
the tests was able to disprove a claim.&lt;/p&gt;


	&lt;p&gt;Was this evidence sufficient for us to rest satisfied that our
implementation was correct?  Given how simple our implementation
was, I felt that the evidence was sufficient. Casey agreed, and we moved on.&lt;/p&gt;


	&lt;p&gt;With the first implementation done, we decided to try a more-sophisticated
implementation.&lt;/p&gt;


	&lt;h3&gt; Generalizing the interface&lt;/h3&gt;


	&lt;p&gt;Since we were about to have multiple implementations, it made sense
for us to define a generalized interface that any &amp;#8220;Rack-like&amp;#8221; 
implementation could use.  For that, Haskell&amp;#8217;s type classes were
perfect:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Our interface for "Rack-like" data types&lt;/span&gt;

&lt;span class='keyword'&gt;class&lt;/span&gt; &lt;span class='conid'&gt;Racklike&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt;
    &lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The interface was essentially the same as before, except that the data
type behind the rack implementation was not given by a specific type
&lt;em&gt;Rack a&lt;/em&gt; but rather by the type variable &lt;em&gt;ra&lt;/em&gt;, which represents some
type of rack container for balls of type &lt;em&gt;a&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Note that &lt;em&gt;ra&lt;/em&gt; determines &lt;em&gt;a&lt;/em&gt;.  If, for example, you know that
the container type &lt;em&gt;ra&lt;/em&gt; equals &amp;#8220;a list of &lt;code&gt;Int&lt;/code&gt; values,&amp;#8221; 
you know that &lt;em&gt;a&lt;/em&gt; must equal &lt;code&gt;Int&lt;/code&gt;.  (To represent this
relationship, we used &lt;a href="http://haskell.org/haskellwiki/Functional_dependencies"&gt;functional
dependencies&lt;/a&gt;,
a popular extension to the Haskell 98 standard.)&lt;/p&gt;


	&lt;p&gt;With the &lt;em&gt;Racklike&lt;/em&gt; type class in place, we moved our list-based
implementation inside of the interface:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Our list-based implementation of a Rack&lt;/span&gt;

&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;ListRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='keyword'&gt;instance&lt;/span&gt; &lt;span class='conid'&gt;Racklike&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;ListRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
    &lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;insertList&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Next, we modified our QuickCheck property definitions.  Where before
it was fine to assume that we would be testing our single, list-based
implementation, now we needed to allow for testing other
implementation types.  We did this by adding a &lt;em&gt;rackType&lt;/em&gt; parameter to
our property definitions.  We used the type, not the value, of this
parameter to determine the type of rack to test:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_New&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;prop_AddAddsElement&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt;

&lt;span class='varid'&gt;prop_AddPreservesOrdering&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;==&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Because we could no longer assume the rack would be represented
as a list of integers, we wrote &lt;em&gt;rackFromList&lt;/em&gt; to convert such
a list into a rack:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;foldr&lt;/span&gt; &lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;With these modifications in place, we re-ran our tests, specifying
(via type annotations) that we wanted to run them for the &lt;em&gt;ListRack&lt;/em&gt;
implementation:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; quickCheck $ prop_New (undefined :: ListRack Int)
OK, passed 100 tests.

*Rack&amp;gt; quickCheck $ prop_AddAddsElement (undefined :: ListRack Int)
OK, passed 100 tests.

*Rack&amp;gt; quickCheck $ prop_AddPreservesOrdering (undefined :: ListRack Int)
OK, passed 100 tests.
&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;A tree-based Rack implementation&lt;/h3&gt;


	&lt;p&gt;Now that we were free to add additional implementation types,
we created one based on binary trees.  We started by defining
the tree data type:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;
    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='keyword'&gt;deriving&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Ord&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;Eq&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;Show&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This definition says that a tree can be either empty or a root node.
A root node has a single value and left and right sub-trees.&lt;/p&gt;


	&lt;p&gt;Further, root nodes must satisfy an ordering invariant: if a root
node&amp;#8217;s value is &lt;em&gt;x&lt;/em&gt;, all of the values in its left subtree must be
less than &lt;em&gt;x&lt;/em&gt;, and all of the values in its right subtree must be
greater than or equal to &lt;em&gt;x&lt;/em&gt;.  The data type doesn&amp;#8217;t enforce this
invariant, so we would need to enforce it in our implementation.&lt;/p&gt;


	&lt;p&gt;Next, we wrote the basic functions for creating, adding elements to,
and observing our trees.&lt;/p&gt;


	&lt;p&gt;We needed to be able to create empty trees:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;emptyTree&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='conid'&gt;Empty&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Inserting an element into a tree requires us to walk the tree and
append the element as a new leaf node in the correct location, being
mindful of our ordering invariant.  Because our data structure is
inherently recursive, a recursive implementation was straightforward
to code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;
&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;&amp;lt;&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;         &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;otherwise&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Note that we don&amp;#8217;t try to ensure that the tree is balanced.  The
problem statement says that the balls are randomly selected, and thus
we can expect our trees, on average, to be balanced naturally.&lt;/p&gt;


	&lt;p&gt;Next, we wrote the code to observe the elements of a tree.
We used a functional-programming idiom
for efficiently flattening a tree into a list:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;elemsTree&lt;/span&gt; &lt;span class='varid'&gt;rx&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='varid'&gt;rx&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;               &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;
&lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Finally, we defined a new tree-based rack type and declared
it to be an instance of the &lt;em&gt;Racklike&lt;/em&gt; type class:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;TreeRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;

&lt;span class='keyword'&gt;instance&lt;/span&gt; &lt;span class='conid'&gt;Racklike&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;TreeRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;emptyTree&lt;/span&gt;
    &lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;insertTree&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;elemsTree&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;With the implementation done, we took it for a test drive:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; add 1 mkRack :: TreeRack Int
Root Empty 1 Empty

*Rack&amp;gt; add 3 (add 1 mkRack) :: TreeRack Int
Root Empty 1 (Root Empty 3 Empty)

*Rack&amp;gt; balls (add 3 (add 1 mkRack) :: TreeRack Int)
[1,3]
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then, for the real test, we checked that our properties held for
TreeRacks:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; quickCheck $ prop_New (undefined :: TreeRack Int)
OK, passed 100 tests.

*Rack&amp;gt; quickCheck $ prop_AddAddsElement (undefined :: TreeRack Int)
OK, passed 100 tests.

quickCheck $ prop_AddPreservesOrdering (undefined :: TreeRack Int)
OK, passed 100 tests.
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Satisfied with these results,  we moved on to part two of the problem.&lt;/p&gt;


	&lt;h3&gt; The second part of the problem&lt;/h3&gt;


	&lt;p&gt;The second part of the problem was about sorting the letters within a
block of text, ignoring white space and punctuation, and converting
upper case letters into lower case: &amp;#8220;Are there any ways to
perform this sort cheaply, and without using built-in libraries?&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Again, a counting sort seemed like an obvious ideal solution, but
we decided to recycle our existing code since we had to leave soon.
Because our Rack implementations were generic, they would work on
letters just as well as on numbers or other kinds of balls:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; balls (rackFromList "this is a test" :: TreeRack Char)
"   aehiisssttt" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;With our existing code already doing the hard work
for us, it was trivial to code up the letter-sorting function:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;sortLetters&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;letters&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;TreeRack&lt;/span&gt; &lt;span class='conid'&gt;Char&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;letters&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;toLower&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;isAlpha&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;(Note: Because of the nature of the problem, I interpreted the
question&amp;#8217;s &amp;#8220;without using built-in libraries&amp;#8221; to mean &amp;#8220;without
built-in &lt;em&gt;sorting&lt;/em&gt; libraries.&amp;#8221;)&lt;/p&gt;


	&lt;p&gt;We took the new function for a test drive, and it worked
as expected:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; sortLetters "This is a test, pal." 
"aaehiilpsssttt" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And that ended our coding session.&lt;/p&gt;


&lt;div class="update"&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Tweaked the revised definition of the AddAddsElement
property for greater parallelism with the original.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update 2007-03-03:&lt;/strong&gt; Minor edits for clarity.&lt;/p&gt;
&lt;/div&gt;</description>
      <pubDate>Tue, 31 Oct 2006 14:44:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8704669e-0d37-4b41-9107-96f72ec20218</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/10/31/introductory-haskell-solving-the-sorting-it-out-kata</link>
      <category>programming</category>
      <category>functional programming</category>
      <category>haskell</category>
      <category>testing</category>
      <category>haskell</category>
      <category>testing</category>
      <category>kata</category>
      <category>sorting</category>
      <category>quickcheck</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/205</trackback:ping>
    </item>
  </channel>
</rss>
