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@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe