Commits

Maxime Henrion committed 4366843

* Define a type synonym for the type of binary codes.
* Rename the "code" function to "codewords".
* Explicitely list exported symbols.

Comments (0)

Files changed (1)

Data/Compression/Huffman.hs

-module Data.Compression.Huffman where
+module Data.Compression.Huffman
+  ( HuffmanTree(..)
+  , Bit(..)
+  , Code
+
+  , huffman
+  , huffmanSorted
+  , codewords
+  , ppCode
+  ) where
 
 import Data.List (intercalate)
 import Control.Arrow (first,second)
                    | Leaf a
   deriving Show
 
-newtype Code a = Code [(a,[Bit])]
+type Code a = [(a,[Bit])]
 
 -- Simple implementation, O(n log n).
 huffman :: (Ord w, Num w) => [(a,w)] -> HuffmanTree a
            Nothing -> x
            Just ((y,w'),s'',t'') -> build (s'' |> (Node x y, w+w')) t''
 
--- Derive the binary code from a huffman tree.
-code :: HuffmanTree a -> [(a,[Bit])]
-code = code' []
+-- Derive the prefix-free binary code from a huffman tree.
+codewords :: HuffmanTree a -> Code a
+codewords = code' []
   where code' _    Empty      = []
         code' bits (Leaf x)   = [(x,bits)]
         code' bits (Node l r) = map (second (Zero:)) (code' bits l) ++
                                 map (second (One:)) (code' bits r)
 
--- Pretty-print a binary code, mostly useful for debugging.
-ppCode :: Show a => [(a,[Bit])] -> String
+-- Pretty-print a binary code.  Mostly useful for debugging.
+ppCode :: Show a => Code a -> String
 ppCode = intercalate "\n" .
            map (\(x,bits) -> show x ++ ": " ++ concat (map show bits))