Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Bitwise-IO/Haskell/bitwise-io.hs
Normal file
30
Task/Bitwise-IO/Haskell/bitwise-io.hs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import Data.List
|
||||
import Data.Char
|
||||
import Control.Monad
|
||||
import Control.Arrow
|
||||
import System.Environment
|
||||
|
||||
int2bin :: Int -> [Int]
|
||||
int2bin = unfoldr(\x -> if x==0 then Nothing
|
||||
else Just (uncurry(flip(,)) (divMod x 2)))
|
||||
|
||||
bin2int :: [Int] -> Int
|
||||
bin2int = foldr ((.(2 *)).(+)) 0
|
||||
|
||||
bitReader = map (chr.bin2int). takeWhile(not.null). unfoldr(Just. splitAt 7)
|
||||
. (take =<< (7 *) . (`div` 7) . length)
|
||||
|
||||
bitWriter xs = tt ++ z00 where
|
||||
tt = concatMap (take 7.(++repeat 0).int2bin.ord) xs
|
||||
z00 = replicate (length xs `mod` 8) 0
|
||||
|
||||
main = do
|
||||
(xs:_) <- getArgs
|
||||
let bits = bitWriter xs
|
||||
|
||||
putStrLn "Text to compress:"
|
||||
putStrLn $ '\t' : xs
|
||||
putStrLn $ "Uncompressed text length is " ++ show (length xs)
|
||||
putStrLn $ "Compressed text has " ++ show (length bits `div` 8) ++ " bytes."
|
||||
putStrLn "Read and decompress:"
|
||||
putStrLn $ '\t' : bitReader bits
|
||||
Loading…
Add table
Add a link
Reference in a new issue