LZW compression: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Replaced older liftM2 and ap with <$> ... <*> etc. Added main and GHC output.) |
|||
Line 2,036: | Line 2,036: | ||
<lang Haskell>import Data.List (elemIndex, tails) |
<lang Haskell>import Data.List (elemIndex, tails) |
||
import Control.Applicative (liftA2) |
|||
import Data.Maybe (fromJust) |
import Data.Maybe (fromJust) |
||
⚫ | |||
⚫ | |||
doLZW :: String -> String -> [Int] |
doLZW :: String -> String -> [Int] |
||
Line 2,059: | Line 2,055: | ||
(!!) |
(!!) |
||
(foldl |
(foldl |
||
( |
((.) <$> (++) <*> |
||
⚫ | |||
(.) |
|||
(++) |
|||
⚫ | |||
(return <$> a) |
(return <$> a) |
||
(take2 cs)) |
(take2 cs)) |
||
⚫ | |||
Testing: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
main :: IO () |
|||
⚫ | |||
main = do |
|||
⚫ | |||
⚫ | |||
Encode --> decode --> compare with original text. |
|||
print $ |
|||
<lang Haskell>*Main> (ap (==) . liftM2 (.) undoLZW doLZW) ['\0'..'\255'] "TOBEORNOTTOBEORTOBEORNOT" |
|||
undoLZW |
|||
⚫ | |||
['\0' .. '\255'] |
|||
⚫ | |||
print $ |
|||
((==) <*> ((.) <$> undoLZW <*> doLZW) ['\NUL' .. '\255']) |
|||
⚫ | |||
{{Out}} |
|||
⚫ | |||
"TOBEORNOTTOBEORTOBEORNOT" |
|||
⚫ | |||
Other (elegant) code can be found at Haskell wiki [http://www.haskell.org/haskellwiki/Toy_compression_implementations Toy compression] |
Other (elegant) code can be found at Haskell wiki [http://www.haskell.org/haskellwiki/Toy_compression_implementations Toy compression] |