-
Notifications
You must be signed in to change notification settings - Fork 16
/
ex7.hs
45 lines (32 loc) · 973 Bytes
/
ex7.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Data.Char
type Bit = Int
bin2int :: [Bit] -> Int
bin2int bits = sum [w * b | (w, b) <- zip weights bits]
where
weights = iterate (* 2) 1
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)
parity :: [Bit] -> Bit
parity bits = sum bits `mod` 2
addParity :: [Bit] -> [Bit]
addParity bits = parity bits : bits
encode :: String -> [Bit]
encode = concatMap (addParity . make8 . int2bin . ord)
chop9 :: [Bit] -> [[Bit]]
chop9 [] = []
chop9 bits = take 9 bits : chop9 (drop 9 bits)
checkParity :: [Bit] -> [Bit]
checkParity bits
| head bits == parity (tail bits) = tail bits
| otherwise = error "Parity Error"
decode :: [Bit] -> String
decode = map (chr . bin2int . checkParity) . chop9
channel :: [Bit] -> [Bit]
channel = id
transmit :: String -> String
transmit = decode . channel . encode