[Haskell-cafe] Suspected stupid Haskell Question

Dan Weston westondan at imageworks.com
Wed Oct 17 15:07:42 EDT 2007


Oh, why didn't you say you were learning Arrows? Then why not

freqs = sort >>> group >>> map (head &&& length)

So much more readable, don't you think? ;)

Either way, if you run into the dreaded monomorphism restriction:

     Ambiguous type variable `a' in the constraint:
       `Ord a' arising from use of `sort' at A.hs:6:40-43
     Possible cause: the monomorphism restriction applied to the following:
       freqs :: [a] -> [(a, Int)] (bound at A.hs:6:0)
     Probable fix: give these definition(s) an explicit type signature
                   or use -fno-monomorphism-restriction

you'll have to either add an explicit type annotation:

freqs :: (Ord a) => [a] -> [(a, Int)]

or else throw an arg onto it:

freqs x = map (head &&& length) . group . sort $ x

The latter hurts too much to write, so I always add the type.

Peter Verswyvelen wrote:
> Nice!!! As I'm learning Arrows now, this is really useful :-)
> 
> Stuart Cook wrote:
>>   import Control.Arrow
>>   import Data.List
>>
>>   freqs = map (head &&& length) . group . sort
>>
>> I have used this function quite a few times already.
>>
>>
>> Stuart
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>>   
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 




More information about the Haskell-Cafe mailing list