summaryrefslogtreecommitdiff
path: root/46-50/50.hs
diff options
context:
space:
mode:
authorDeterminant <ted.sybil@gmail.com>2017-05-29 16:33:04 -0400
committerDeterminant <ted.sybil@gmail.com>2017-05-29 16:33:04 -0400
commitd0aa856bedb0c0223b4ce2a67f5582b8eadf3682 (patch)
tree98d300c8cef2cc060dfdecd0e2de15575621cdc4 /46-50/50.hs
parent9db0008777cc4cd9f07e14a3458e3d903e3cc4f6 (diff)
finish v5
Diffstat (limited to '46-50/50.hs')
-rw-r--r--46-50/50.hs12
1 files changed, 12 insertions, 0 deletions
diff --git a/46-50/50.hs b/46-50/50.hs
new file mode 100644
index 0000000..0e9c33b
--- /dev/null
+++ b/46-50/50.hs
@@ -0,0 +1,12 @@
+import Data.List (sortBy, insertBy)
+import Data.Ord (comparing)
+
+huffman :: [(Char, Int)] -> [(Char, String)]
+
+huffman l = build $ map (\(a, b) -> ([(a, "")], b)) $ sortBy (comparing snd) l
+ where build [x] = fst x
+ build (x:y:xs) =
+ build $ insertBy
+ (comparing snd)
+ ((add x '0') ++ (add y '1'), snd x + snd y) xs
+ where add e c = (map (\(a, b) -> (a, c:b)) $ fst e)