プログラミング Haskell 第7章 高階関数 練習問題その3

8.
int2bin, bin2int, make8 は本文中のものをそのまま使っているものとします。また chop9 は chop8 を 8->9 に変更しただけのものです。

parity :: [Bit] -> Bit
parity = foldl bitadd 0

addparity :: [Bit] -> [Bit]
addparity bits = bits ++ [parity bits]

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

paritycheck :: [Bit] -> [Bit]
paritycheck bits = if parity body == pb
                   then body
                   else error "parity error"
                   where body = take 8 bits
                         pb = head (drop 8 bits)

decode = map (chr . bin2int . paritycheck) . chop9

9.

error_channel :: [Bit] -> [Bit]
error_channel = tail

error_transmit :: String -> String
error_transmit = decode . error_channel . encode

このように定義して

error_transmit "test"
"*** Exception: parity error

このように ghci で実行してエラーを検出できました。

ようやく第7章の練習問題を終えました。この先もスピードダウンするでしょうがしっかり身につけつつ進みます。