<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Finally some hacking of my own: a counter datatype</title>
	<atom:link href="http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/feed/" rel="self" type="application/rss+xml" />
	<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/</link>
	<description>Haskell programming journal</description>
	<lastBuildDate>Fri, 17 May 2013 21:48:47 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: dr. k</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-394</link>
		<dc:creator><![CDATA[dr. k]]></dc:creator>
		<pubDate>Tue, 13 Feb 2007 16:03:01 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-394</guid>
		<description><![CDATA[That&#039;s because Show is doing on-the-fly most of the work that in a Map implementation is done upon insertion. This is a conscious design decision, to have cheap inc.]]></description>
		<content:encoded><![CDATA[<p>That&#8217;s because Show is doing on-the-fly most of the work that in a Map implementation is done upon insertion. This is a conscious design decision, to have cheap inc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kalmar</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-389</link>
		<dc:creator><![CDATA[kalmar]]></dc:creator>
		<pubDate>Tue, 13 Feb 2007 03:34:05 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-389</guid>
		<description><![CDATA[No, I mean in response to Mikael&#039;s group . sort thingy.  A Show instance shouldn&#039;t, in my opinion, introduce any new constraints other than Show.  I&#039;m just saying that if your structure doesn&#039;t require Ord, the Show instance definitely shouldn&#039;t add it.  If you change it to use a Map, then you have an Ord constraint, but it&#039;s introduced by the structure not by the Show instance.]]></description>
		<content:encoded><![CDATA[<p>No, I mean in response to Mikael&#8217;s group . sort thingy.  A Show instance shouldn&#8217;t, in my opinion, introduce any new constraints other than Show.  I&#8217;m just saying that if your structure doesn&#8217;t require Ord, the Show instance definitely shouldn&#8217;t add it.  If you change it to use a Map, then you have an Ord constraint, but it&#8217;s introduced by the structure not by the Show instance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dr. k</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-388</link>
		<dc:creator><![CDATA[dr. k]]></dc:creator>
		<pubDate>Tue, 13 Feb 2007 02:41:06 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-388</guid>
		<description><![CDATA[It&#039;s an Eq context. And how else do you propose having a counter over an Eqless map? Data.Map probably requires it too.]]></description>
		<content:encoded><![CDATA[<p>It&#8217;s an Eq context. And how else do you propose having a counter over an Eqless map? Data.Map probably requires it too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kalmar</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-387</link>
		<dc:creator><![CDATA[kalmar]]></dc:creator>
		<pubDate>Tue, 13 Feb 2007 00:52:56 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-387</guid>
		<description><![CDATA[It seems &quot;bad&quot; to have the Show instance require an Ord context.]]></description>
		<content:encoded><![CDATA[<p>It seems &#8220;bad&#8221; to have the Show instance require an Ord context.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dr. k</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-386</link>
		<dc:creator><![CDATA[dr. k]]></dc:creator>
		<pubDate>Mon, 12 Feb 2007 10:08:25 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-386</guid>
		<description><![CDATA[Points taken!]]></description>
		<content:encoded><![CDATA[<p>Points taken!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mikael Johansson</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-385</link>
		<dc:creator><![CDATA[Mikael Johansson]]></dc:creator>
		<pubDate>Mon, 12 Feb 2007 10:00:05 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-385</guid>
		<description><![CDATA[Why use that double list traversal for dropping an element, instead of using 
dec x (C y) = C . deleteFirstsBy (==) y [x]

Your n-inc can also be written using the design choices you&#039;ve made: 
ninc n x (C y) = C (y ++ replicate n x)
I would even go so far as to do 
toCounter xs = C (concatMap (uncurry replicate) xs)

As for show-ing the whole thing, why not just do
show = unlines . map (\(x,y) -&gt; show x ++ &quot;\t&quot; ++ show y) . (map(\x-&gt;(head x,length x)))  . group  . sort]]></description>
		<content:encoded><![CDATA[<p>Why use that double list traversal for dropping an element, instead of using<br />
dec x (C y) = C . deleteFirstsBy (==) y [x]</p>
<p>Your n-inc can also be written using the design choices you&#8217;ve made:<br />
ninc n x (C y) = C (y ++ replicate n x)<br />
I would even go so far as to do<br />
toCounter xs = C (concatMap (uncurry replicate) xs)</p>
<p>As for show-ing the whole thing, why not just do<br />
show = unlines . map (\(x,y) -&gt; show x ++ &#8220;\t&#8221; ++ show y) . (map(\x-&gt;(head x,length x)))  . group  . sort</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dr. k</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-383</link>
		<dc:creator><![CDATA[dr. k]]></dc:creator>
		<pubDate>Mon, 12 Feb 2007 00:19:12 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-383</guid>
		<description><![CDATA[Well, as I said, I explicitly wanted very cheap inc. count will be only occasional, and dec/toCounter/fromCounter are included only for completeness&#039; sake. ]]></description>
		<content:encoded><![CDATA[<p>Well, as I said, I explicitly wanted very cheap inc. count will be only occasional, and dec/toCounter/fromCounter are included only for completeness&#8217; sake. </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Twan van Laarhoven</title>
		<link>http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-382</link>
		<dc:creator><![CDATA[Twan van Laarhoven]]></dc:creator>
		<pubDate>Sun, 11 Feb 2007 23:58:46 +0000</pubDate>
		<guid isPermaLink="false">http://syntaxfree.wordpress.com/2007/02/11/finally-some-hacking-of-my-own-a-counter-datatype/#comment-382</guid>
		<description><![CDATA[&#039;dec&#039; and &#039;count&#039; will (in the worst case) have traverse the entire list. This means they both are O(n). This makes fromCounter O(n^2)! Using a &quot;Map a Int&quot; is a much better choice, fromCounter would be O(n), and all other operations O(log n).

I am also not sure if your semantics of dec are the best choice. If you simply allow a negative count then the following laws hold:
* inc a . dec a == dec a . inc a == id
* totalCount . dec a == subtract 1 . totalCount
    where totalCount = sum . map snd . fromCounter

The implementation also becomes simpler if you use Data.Map, because it handles most stuff for you:
&gt; data Counter a = C { unC :: M.Map a Int }
&gt; empty = C M.empty
&gt; inc a = C . M.insertWith (+) a (+1) . unC
&gt; dec a = C . M.alter dec&#039; a . unC
      where
                dec&#039; Nothing = Just (-1)
                dec&#039; (Just 1)  = Nothing
                dec&#039; (Just c)  = Just (c - 1)
&gt; count a = M.findWithDefault 0 a . unC
&gt; toCounter = C . M.fromList
&gt; fromCounter = M.toList . unC
&gt; showCounter = unlines . map (\a c -&gt; show a ++ &quot;\t&quot; ++ show c) . fromCounter]]></description>
		<content:encoded><![CDATA[<p>&#8216;dec&#8217; and &#8216;count&#8217; will (in the worst case) have traverse the entire list. This means they both are O(n). This makes fromCounter O(n^2)! Using a &#8220;Map a Int&#8221; is a much better choice, fromCounter would be O(n), and all other operations O(log n).</p>
<p>I am also not sure if your semantics of dec are the best choice. If you simply allow a negative count then the following laws hold:<br />
* inc a . dec a == dec a . inc a == id<br />
* totalCount . dec a == subtract 1 . totalCount<br />
    where totalCount = sum . map snd . fromCounter</p>
<p>The implementation also becomes simpler if you use Data.Map, because it handles most stuff for you:<br />
&gt; data Counter a = C { unC :: M.Map a Int }<br />
&gt; empty = C M.empty<br />
&gt; inc a = C . M.insertWith (+) a (+1) . unC<br />
&gt; dec a = C . M.alter dec&#8217; a . unC<br />
      where<br />
                dec&#8217; Nothing = Just (-1)<br />
                dec&#8217; (Just 1)  = Nothing<br />
                dec&#8217; (Just c)  = Just (c &#8211; 1)<br />
&gt; count a = M.findWithDefault 0 a . unC<br />
&gt; toCounter = C . M.fromList<br />
&gt; fromCounter = M.toList . unC<br />
&gt; showCounter = unlines . map (\a c -&gt; show a ++ &#8220;\t&#8221; ++ show c) . fromCounter</p>
]]></content:encoded>
	</item>
</channel>
</rss>
