module HW5 where import Data.Char (chr, isLower, ord) -- **=========[ Ch.05 ]=========** -- Problem #1: define safetail safetail :: _ safetail = _ -- End Problem #1 -- Problem #2: Luhn algorithm luhn :: Int -> Int -> Int -> Int -> Bool luhn = _ -- End Problem #2 -- **=========[ Ch.06 ]=========** -- Problem #1: Caesar crack encode :: Int -> String -> String encode n xs = [ shift n x | x <- xs ] shift :: Int -> Char -> Char shift n c | isLower c = int2let $ mod (let2int c + n) 26 | otherwise = c let2int :: Char -> Int let2int c = ord c - ord 'a' int2let :: Int -> Char int2let n = chr $ ord 'a' + n table :: [Float] table = [ 8.1, 1.5, 2.8, 4.2, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2, 0.8, 4.0, 2.4, 6.7, 7.5, 1.9, 0.1, 6.0, 6.3, 9.0, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1 ] crack :: String -> String crack xs = encode (-factor) xs where factor = position (minimum chitab) chitab chitab = [ chisqr (rotate n table') table | n <- [0..25] ] table' = freqs xs -- you can check : crack "aaaaab" = "eeeeef" position :: Eq a => a -> [a] -> Int position = _ rotate :: Int -> [a] -> [a] rotate = _ freqs :: String -> [Float] freqs = _ chisqr :: [Float] -> [Float] -> Float chisqr = _ -- End Problem #1 -- Problem #2: Pythagorean triples pyths :: Int -> [(Int, Int, Int)] pyths = _ -- End Problem #2 -- Problem #3: perfect integers perfects :: Int -> [Int] perfects = _ -- End Problem #3 -- Problem #4: scalar product scalarProduct :: Num a => [a] -> [a] -> a scalarProduct = _ -- End Problem #4 -- **=========[ Ch.07 ]=========** -- Problem #1: define prelude functions using recursions and' :: [Bool] -> Bool and' = _ concat' :: [[a]] -> [a] concat' = _ (!!!) :: [a] -> Int -> a (!!!) = _ replicate' :: Int -> a -> [a] replicate' = _ elem' :: Eq a => a -> [a] -> Bool elem' = _ -- End Problem #1 -- Problem #2: merge ascending lists merge :: Ord a => [a] -> [a] -> [a] merge = _ -- End Problem #2 -- Problem #3: merge sort msort :: Ord a => [a] -> [a] msort = _ -- End Problem #3