September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,19 +1,25 @@
|
|||
import qualified Data.List as L
|
||||
import qualified Data.Maybe as M
|
||||
import Data.List (delete, elemIndex, mapAccumL)
|
||||
|
||||
import Data.Maybe (fromJust)
|
||||
|
||||
table :: String
|
||||
table = ['a'..'z']
|
||||
table = ['a' .. 'z']
|
||||
|
||||
encode :: String -> [Int]
|
||||
encode = snd . L.mapAccumL f table
|
||||
where
|
||||
f t s = ((s : L.delete s t), M.fromJust (L.elemIndex s t))
|
||||
encode =
|
||||
let f t s = (s : delete s t, fromJust (elemIndex s t))
|
||||
in snd . mapAccumL f table
|
||||
|
||||
decode :: [Int] -> String
|
||||
decode = snd . L.mapAccumL f table
|
||||
decode = snd . mapAccumL f table
|
||||
where
|
||||
f t i = let s = (t !! i) in ((s : L.delete s t), s)
|
||||
f t i =
|
||||
let s = (t !! i)
|
||||
in (s : delete s t, s)
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ (\s -> let t@(e, _) = (encode s, decode e) in print t)
|
||||
["broood", "bananaaa", "hiphophiphop"]
|
||||
main =
|
||||
mapM_ print $
|
||||
(,) <*> uncurry ((==) . fst) <$> -- Test that ((fst . fst) x) == snd x)
|
||||
((,) <*> (decode . snd) <$>
|
||||
((,) <*> encode <$> ["broood", "bananaaa", "hiphophiphop"]))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue