This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,20 @@
import Data.List
import Data.Char
import Data.Maybe
import Control.Monad
import Control.Arrow
take2 = filter((==2).length). map (take 2). tails
doLZW _ [] = []
doLZW as (x:xs) = lzw (map return as) [x] xs
where lzw a w [] = [fromJust $ elemIndex w a]
lzw a w (x:xs) | w' `elem` a = lzw a w' xs
| otherwise = fromJust (elemIndex w a) : lzw (a++[w']) [x] xs
where w' = w++[x]
undoLZW _ [] = []
undoLZW a cs =
((cs >>=).(!!)) $
foldl (liftM2 (.) (++) (((return. liftM2 (++) head (take 1. last)).). map. (!!)))
(map return a) (take2 cs)

View file

@ -0,0 +1,5 @@
*Main> doLZW ['\0'..'\255'] "TOBEORNOTTOBEORTOBEORNOT"
[84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263]
*Main> undoLZW ['\0'..'\255'] [84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263]
"TOBEORNOTTOBEORTOBEORNOT"

View file

@ -0,0 +1,2 @@
*Main> (ap (==) . liftM2 (.) undoLZW doLZW) ['\0'..'\255'] "TOBEORNOTTOBEORTOBEORNOT"
True