A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
20
Task/LZW-compression/Haskell/lzw-compression-1.hs
Normal file
20
Task/LZW-compression/Haskell/lzw-compression-1.hs
Normal 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)
|
||||
5
Task/LZW-compression/Haskell/lzw-compression-2.hs
Normal file
5
Task/LZW-compression/Haskell/lzw-compression-2.hs
Normal 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"
|
||||
2
Task/LZW-compression/Haskell/lzw-compression-3.hs
Normal file
2
Task/LZW-compression/Haskell/lzw-compression-3.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*Main> (ap (==) . liftM2 (.) undoLZW doLZW) ['\0'..'\255'] "TOBEORNOTTOBEORTOBEORNOT"
|
||||
True
|
||||
Loading…
Add table
Add a link
Reference in a new issue