September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -2,23 +2,36 @@ import Data.List
import Control.Arrow
import Data.Ord
data HTree a = Leaf a | Branch (HTree a) (HTree a)
deriving (Show, Eq, Ord)
data HTree a
= Leaf a
| Branch (HTree a)
(HTree a)
deriving (Show, Eq, Ord)
test :: String -> IO ()
test s = mapM_ (\(a,b)-> putStrLn ('\'' : a : "\' : " ++ b))
. serialize . huffmanTree . freq $ s
test =
mapM_ (\(a, b) -> putStrLn ('\'' : a : "\' : " ++ b)) .
serialize . huffmanTree . freq
serialize :: HTree a -> [(a, String)]
serialize (Branch l r) = map (second('0':)) (serialize l) ++ map (second('1':)) (serialize r)
serialize (Leaf x) = [(x, "")]
serialize (Branch l r) =
(second ('0' :) <$> serialize l) ++ (second ('1' :) <$> serialize r)
serialize (Leaf x) = [(x, "")]
huffmanTree :: (Ord w, Num w) => [(w, a)] -> HTree a
huffmanTree = snd . head . until (null.tail) hstep
. sortBy (comparing fst) . map (second Leaf)
huffmanTree
:: (Ord w, Num w)
=> [(w, a)] -> HTree a
huffmanTree =
snd .
head . until (null . tail) hstep . sortBy (comparing fst) . (second Leaf <$>)
hstep :: (Ord a, Num a) => [(a, HTree b)] -> [(a, HTree b)]
hstep ((w1,t1):(w2,t2):wts) = insertBy (comparing fst) (w1 + w2, Branch t1 t2) wts
hstep
:: (Ord a, Num a)
=> [(a, HTree b)] -> [(a, HTree b)]
hstep ((w1, t1):(w2, t2):wts) =
insertBy (comparing fst) (w1 + w2, Branch t1 t2) wts
freq :: Ord a => [a] -> [(Int, a)]
freq = map (length &&& head) . group . sort
freq
:: Ord a
=> [a] -> [(Int, a)]
freq = ((length &&& head) <$>) . group . sort