程序代做CS代考 Haskell – Binary string transmitter example from chapter 7 of Programming – cscodehelp代写

— Binary string transmitter example from chapter 7 of Programming
— in Haskell, , Cambridge University Press, 2016.

import Data.Char

— Base conversion

type Bit = Int

bin2int :: [Bit] -> Int
bin2int = foldr (x y -> x + 2*y) 0

int2bin :: Int -> [Bit]
int2bin 0 = []
int2bin n = n `mod` 2 : int2bin (n `div` 2)

make8 :: [Bit] -> [Bit]
make8 bits = take 8 (bits ++ repeat 0)

— Transmission

encode :: String -> [Bit]
encode = concat . map (make8 . int2bin . ord)

chop8 :: [Bit] -> [[Bit]]
chop8 [] = []
chop8 bits = take 8 bits : chop8 (drop 8 bits)

decode :: [Bit] -> String
decode = map (chr . bin2int) . chop8

transmit :: String -> String
transmit = decode . channel . encode

channel :: [Bit] -> [Bit]
channel = id

Leave a Reply

Your email address will not be published. Required fields are marked *