Skip to content

Instantly share code, notes, and snippets.

@FranklinChen
Created November 18, 2013 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FranklinChen/7534830 to your computer and use it in GitHub Desktop.
Save FranklinChen/7534830 to your computer and use it in GitHub Desktop.
For @BillLaboon hey @FranklinChen is there an easy way in Haskell to find out which element in a list is most common, e.g. [1,1,1,2,3] -> 1 ?
import Test.HUnit
import qualified Data.List as List
import qualified Data.Ord as Ord
import Data.PSQueue as PSQueue
{-
For @BillLaboon
hey @franklinchen is there an easy way in Haskell to find out which element in a list is most common, e.g. [1,1,1,2,3] -> 1 ?
-}
testMostCommon = TestCase $ mostCommon [1, 1, 1, 2, 3] @?= Just 1
-- Want a max priority queue with updateable priorities
mostCommon :: Ord a => [a] -> Maybe a
mostCommon xs =
do
k :-> _ <- PSQueue.findMin $ List.foldl' updateQueue PSQueue.empty xs
return k
updateQueue :: (Num a, Ord k, Ord a) => PSQ k (Ord.Down a) -> k -> PSQ k (Ord.Down a)
updateQueue q key = PSQueue.alter updatePriority key q
updatePriority :: Num a => Maybe (Ord.Down a) -> Maybe (Ord.Down a)
updatePriority Nothing = Just (Ord.Down 1)
updatePriority (Just (Ord.Down p)) = Just (Ord.Down (p+1))
@FranklinChen
Copy link
Author

Since we only want what is most common, there is no need to sort. A priority queue is most efficient. PSQueue is a purely functional priority search queue by Ralf Hinze: http://www.cs.ox.ac.uk/ralf.hinze/publications/ICFP01.pdf

If there is more than one element of the same max count, we just pull off one. It would be trivial (an extra line of code) to return a list of all the elements of the same max count.

@FranklinChen
Copy link
Author

I like how there are 7 lines of code, 4 lines of imports, 6 lines of comments, and 3 lines of (completely optional) type annotations. This is what my Haskell code typically looks like.

Oh, and I was lazy and had just 1 test case. In practice I would write some tests using QuickCheck to generate a couple hundred test cases automatically.

And the code is generic in key type (as long as can be ordered) and count type (as long as numeric and can be ordered).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment