<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><div id="yiv1009391497">Hi John,<br><br>Yes, I see now what the hullabaloo was about.<br><br>I've only been hacking Haskell for a few weeks, so I'm still seeing the "forest" rather than the "trees". But I'm coming along.<br><br>Thanks.<br><br>Michael<br><br>--- On <b>Sat, 4/18/09, John Dorsey <i><haskell@colquitt.org></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: John Dorsey <haskell@colquitt.org><br>Subject: Re: [Haskell-cafe] General function to count list elements?<br>To: "michael rice" <nowgate@yahoo.com><br>Cc: haskell-cafe@haskell.org<br>Date: Saturday, April 18, 2009, 2:36 PM<br><br><div class="plainMail">Michael,<br><br>> I had a count function that worked fine for an enum type, but thought<br>> why not go polymorphic with it. So I changed it and got
the error I<br>> originally inquired about.<br><br>For variety, I'll go a slightly different direction.<br><br>If you generalize count to use any predicate, instead of
always<br>equality...<br><br> gcount :: (a -> a -> Bool) -> a -> [a] -> Int<br> gcount pred x0 xs = length (filter (pred x0) xs)<br><br> count = gcount (==)<br><br>This will work with any type that you can write a predicate for with the<br>type (a -> a -> Bool). I can even use this with functions, if I'm<br>careful.<br><br> ghci> gcount (\f g -> True) (*2) [id,(const 1),(*3)]<br> 3<br> ghci> gcount (\f g -> f 1 == g 1) (^2) [id,(const 1),(*3)]<br> 2<br><br>By the way, do you see why everyone's bothing you about comparing<br>functions? The type you gave count, which didn't have an Eq constraint,<br>was an assertion that you could compare two values of *any* type. If<br>there's a type that's not comparable, then count's type was wrong.<br>Functions
are the canonical example of an incomparable type.<br><br>When you're bored some time, read a bit about the Curry-Howard<br>correspondence. It's interesting, even if (like me) you don't grok all<br>of its implications.<br><br>Regards,<br>John<br><br></div></blockquote></div></td></tr></table><br>