Skip to content

Instantly share code, notes, and snippets.

@KitaitiMakoto
Created November 3, 2011 19:25
Show Gist options
  • 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
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)
insert :: Ord a => a -> [a] -> [a]
insert x [] = [x]
insert x ys|x <= y = x:ys
|otherwise = y:insert x (drop 1 ys)
where y = head ys
isort :: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert x (isort xs)
-- Exercises
-- 1.
(^) :: Int -> Int -> Int
m ^ 0 = 1
m ^ n = m * (m Main.^ (n - 1))
-- 2 ^ 3
-- = 2 * (2 ^ 2)
-- = 2 * (2 * (2 ^ 1))
-- = 2 * (2 * (2 * (2 ^ 0)))
-- = 2 * (2 * (2 * 1))
-- = 8
-- 2.
-- length [1,2,3]
-- = 1 + length [2,3]
-- = 1 + 1 + length [3]
-- = 1 + 1 + 1 + length []
-- = 1 + 1 + 1 + 0
-- = 3
-- drop 3 [1,2,3,4,5]
-- = drop 2 [2,3,4,5]
-- = drop 1 [3,4,5]
-- = drop 0 [4,5]
-- = [4,5]
-- init [1,2,3]
-- = 1 : init [2,3]
-- = 1 : (2 : init [3])
-- = 1 : (2 : [])
-- = [1,2]
-- 3.
and :: [Bool] -> Bool
and [] = True
and (x:xs) = x && Main.and xs
concat :: [[a]] -> [a]
concat [] = []
concat (xs:xss) = xs ++ Main.concat xss
replicate ::Int -> a -> [a]
replicate 0 x = []
replicate n x = x : Main.replicate (n - 1) x
(!!) :: [a] -> Int -> a
(!!) (x:xs) 0 = x
(!!) (x:xs) n = (Main.!!) xs (n - 1)
elem :: Eq a => a -> [a] -> Bool
elem x [] = False
elem x (y:ys)|x == y = True
|otherwise = Main.elem x ys
-- 4.
merge :: Ord a => [a] -> [a] -> [a]
merge [] xs = xs
merge xs [] = xs
merge (x:xs) (y:ys)|x <= y = x:Main.merge xs (y:ys)
|otherwise = y:Main.merge (x:xs) ys
-- 5.
halve :: [a] -> ([a], [a])
halve xs = (Prelude.take n xs, drop n xs)
where n = length xs `div` 2
msort :: Ord a => [a] -> [a]
msort [] = []
msort [x] = [x]
msort xs = merge (msort x) (msort y)
where (x, y) = halve xs
-- 6.
sum :: Num a => [a] -> a
sum [] = 0
sum (x:xs) = x + Main.sum xs
take :: Int -> [a] -> [a]
take 0 _ = []
take _ [] = []
take n (x:xs) = x : Main.take (n - 1) xs
last :: [a] -> a
last [x] = x
last (x:xs) = Main.last 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