CS计算机代考程序代写 import Data.Char

import Data.Char

twice :: (a -> a) -> a -> a
twice f x = f (f x)

sumsqreven :: Integral a => [a] -> a
sumsqreven ns = sum (map (^2) (filter even ns))

twice’ :: (a -> a) -> a -> a
twice’ f = f . f

sumsqreven’ :: Integral a => [a] -> a
sumsqreven’ = sum . map (^2) . filter even

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)

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