style: lint
This commit is contained in:
parent
69c54f4e56
commit
d83dcba48d
18
index.hs
18
index.hs
@ -36,8 +36,8 @@ module Huffman ( tree, charSequence, decode )
|
|||||||
charSequence node ch =
|
charSequence node ch =
|
||||||
fromJust $ helper node ch ""
|
fromJust $ helper node ch ""
|
||||||
where
|
where
|
||||||
helper (Node { symbol = s, left = l, right = r }) ch sequ
|
helper Node { symbol = s, left = l, right = r } ch sequ
|
||||||
| (isJust s) && (fromJust s == ch) = Just sequ
|
| s == Just ch = Just sequ
|
||||||
| isNothing l && isNothing r = Nothing
|
| isNothing l && isNothing r = Nothing
|
||||||
| otherwise = let leftPath = helper (fromJust l) ch (sequ ++ "0")
|
| otherwise = let leftPath = helper (fromJust l) ch (sequ ++ "0")
|
||||||
rightPath = helper (fromJust r) ch (sequ ++ "1")
|
rightPath = helper (fromJust r) ch (sequ ++ "1")
|
||||||
@ -45,8 +45,8 @@ module Huffman ( tree, charSequence, decode )
|
|||||||
|
|
||||||
-- Root-to-leaf search, find a character based on sequence string
|
-- Root-to-leaf search, find a character based on sequence string
|
||||||
findChar :: Node -> String -> Maybe Char
|
findChar :: Node -> String -> Maybe Char
|
||||||
findChar n@(Node { symbol = s, left = l, right = r}) sequ
|
findChar n@Node { symbol = s, left = l, right = r} sequ
|
||||||
| length sequ > 0 = let path = if head sequ == '0' then l else r
|
| not $ null sequ = let path = if head sequ == '0' then l else r
|
||||||
in if isJust path then
|
in if isJust path then
|
||||||
findChar (fromJust path) (tail sequ)
|
findChar (fromJust path) (tail sequ)
|
||||||
else
|
else
|
||||||
@ -59,7 +59,7 @@ module Huffman ( tree, charSequence, decode )
|
|||||||
encode :: String -> String
|
encode :: String -> String
|
||||||
encode input = let t = tree input
|
encode input = let t = tree input
|
||||||
table = charTable t input
|
table = charTable t input
|
||||||
in concat $ map (\a -> fromJust $ Map.lookup a table) input
|
in concatMap (\a -> fromJust $ Map.lookup a table) input
|
||||||
|
|
||||||
-- Character table, a Map representing each character's bit sequence
|
-- Character table, a Map representing each character's bit sequence
|
||||||
charTable :: Node -> String -> Map.Map Char String
|
charTable :: Node -> String -> Map.Map Char String
|
||||||
@ -72,10 +72,10 @@ module Huffman ( tree, charSequence, decode )
|
|||||||
-- Decode a string, given the tree representing it
|
-- Decode a string, given the tree representing it
|
||||||
decode :: Node -> String -> String
|
decode :: Node -> String -> String
|
||||||
decode t input = let (valid, next) = span (isNothing . findChar t) $ inits input
|
decode t input = let (valid, next) = span (isNothing . findChar t) $ inits input
|
||||||
sequ = (head next)
|
sequ = head next
|
||||||
ninput = (tails input) !! (length valid)
|
ninput = tails input !! length valid
|
||||||
ch = fromJust $ findChar t sequ
|
ch = fromJust $ findChar t sequ
|
||||||
in if length ninput > 0 then
|
in if not $ null ninput then
|
||||||
ch:(decode t ninput)
|
ch : decode t ninput
|
||||||
else
|
else
|
||||||
[ch]
|
[ch]
|
||||||
|
Loading…
Reference in New Issue
Block a user