Created
June 11, 2010 19:41
-
-
Save andrewhavck/434938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- file:HuffmanEncoding/Frequency.hs | |
module Frequency where | |
import Data.List(nub,foldl') | |
frequencies :: (Eq a) => [a] -> [(a, Double)] | |
frequencies s = zip (nub s) (map divide qset) | |
where divide (_,d) = (d / (total qset)) | |
qset = quantities (nub s) s | |
total :: [(a,Double)] -> Double | |
total xs = foldl' step 0 xs | |
where step acc (_,d) = acc + d | |
quantities :: (Eq a) => [a] -> [a] -> [(a, Double)] | |
quantities ls xs = foldr ((:) . step) [] ls | |
where step l = (l, quantity l xs) | |
quantity :: (Eq a) => a -> [a] -> Double | |
quantity s xs = foldl' step 0 xs | |
where step acc x | x == s = 1 + acc | |
| otherwise = acc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment