Skip to content

Instantly share code, notes, and snippets.

@KitaitiMakoto
Created November 3, 2011 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KitaitiMakoto/1337526 to your computer and use it in GitHub Desktop.
Save KitaitiMakoto/1337526 to your computer and use it in GitHub Desktop.
Excersises of Pragramming in Haskell
-- 3.
n = a `div` length xs
where
a = 10
xs = [1,2,3,4,5]
-- 4.
last1 xs = xs!!(length xs - 1)
last2 xs = head (reverse xs)
last3 xs = head (drop (length xs - 1) xs)
-- 5.
init1 xs = take (length xs - 1) xs
init2 xs = reverse (drop 1 (reverse xs))
-- halve xs = (take (length xs `div` 2) xs, drop (length xs `div` 2) xs)
halve xs = (take n xs, drop n xs)
where n = length xs `div` 2
safetail1 xs = if null xs
then []
else tail xs
safetail2 xs | null xs = []
| otherwise = tail xs
safetail3 [] = []
safetail3 xs = tail xs
-- True || True = True
-- True || False = True
-- False || True = True
-- False || False = False
-- False || False = False
-- _ || _ = True
-- False || b = b
-- True || _ = True
-- b || c | b /= c = True
-- | otherwise = b
(||) x y = if x
then True
else if x /= y
then True
else False
-- (&&) x y = if x == True
-- then if y == True
-- then True
-- else False
-- else False
(&&) x y = if x == True
then y
else False
lmmult x y z = (\a -> x * a)((\b -> y * b)z)
import Data.Char
factors :: Int -> [Int]
factors n = [x|x <- [1..n], n `mod` x == 0]
prime :: Int -> Bool
prime n = factors n == [1, n]
primes :: Int -> [Int]
primes n = [x|x <- [2..n], prime x]
find ::Eq a => a -> [(a, b)] -> [b]
find k t = [v|(k', v) <- t, k == k']
positions :: Eq a => a -> [a] -> [Int]
positions x xs = [i|(x', i) <- zip xs [0..n], x' == x]
where n = length xs - 1
lowers :: String -> Int
lowers xs = length [x|x <- xs, isLower x]
count :: Char -> String -> Int
count x xs = length [x'|x' <- xs, x == x']
let2int :: Char -> Int
let2int c = ord c - ord 'a'
int2let :: Int -> Char
int2let n = chr(ord 'a' + n)
shift :: Int -> Char -> Char
shift n c|isLower c = int2let((let2int c + n) `mod` 26)
|otherwise = c
encode :: Int -> String -> String
encode n xs = [shift n x|x <- xs]
table :: [Float]
table = [8.2, 1.5, 2.8, 4.3, 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.1, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1]
percent :: Int -> Int -> Float
percent n m = (fromIntegral n / fromIntegral m) * 100
freqs :: String -> [Float]
freqs xs = [percent (count x xs) n|x <- ['a'..'z']]
where n = lowers xs
chisqr :: [Float] -> [Float] -> Float
chisqr os es = sum [((o - e)^2) / e|(o, e) <- zip os es]
rotate :: Int -> [a] -> [a]
rotate n xs = drop n xs ++ take n xs
crack :: String -> String
crack xs = encode (-factor) xs
where
factor = head (positions (minimum chitab) chitab)
chitab = [chisqr (rotate n table') table|n <- [0..25]]
table' = freqs xs
-- exercises
-- 1.
-- 1^2 + 2^2 + ... + 100^2
-- sum [x^2|x <- [1..100]] / 100
-- 2.
replicate :: Int -> a -> [a]
replicate n x = [x|i <- [1..n]]
-- 3.
pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x,y,z)|z <- [1..n], x <- [1..z], y <- [1..z], x^2 + y^2 == z^2]
-- 4.
perfects :: Int -> [Int]
perfects n = [x|x <- [1..n], sum [y|y <- factors x, y /= x] == x ]
-- 5.
prod = concat [
[ (x, y) | y <- [4, 5, 6] ]
| x <- [1, 2, 3]
]
-- 6.
positions2 :: Eq a => a -> [a] -> [Int]
positions2 x xs = find x (zip xs [0..(length xs - 1)])
-- 7.
scalarproduct :: [Int] -> [Int] -> Int
scalarproduct xs ys = sum [x * y|(x, y) <- zip xs ys]
scalarproductexact :: [Int] -> [Int] -> Int
scalarproductexact xs ys|length xs == length ys = sum [x * y|(x, y) <- zip xs ys]
-- 8.
alphas :: String -> Int
alphas xs = length [x|x <- xs, isAlpha x]
let2int2 :: Char -> Int
let2int2 c = ord c - ord 'A'
int2let2 :: Int -> Char
int2let2 n = chr(ord 'A' + n)
shift2 :: Int -> Char -> Char
shift2 n c|isAlpha c = int2let2((let2int2 c + n) `mod` 52)
|otherwise = c
encode2 :: Int -> String -> String
encode2 n xs = [shift2 n x|x <- xs]
table2 :: [Float]
table2 = table' ++ table'
where table' = [x / 2|x <- table]
freqs2 :: String -> [Float]
freqs2 xs = [percent (count x xs) n| x <- ['A'..'z']]
where n = alphas xs
crack2 :: String -> String
crack2 xs = encode2 (-factor) xs
where
factor = head (positions (minimum chitab) chitab)
chitab = [chisqr (rotate n table') (table ++ table)|n <- [0..51]]
table' = freqs2 xs
@KitaitiMakoto
Copy link
Author

Answer for excersize 8 of Chapter 5 doesn't look like goot cracker.
What's better implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment