Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions problems/mshibatatt/chapter4/1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
halve :: [a] -> ([a], [a])
halve xs = (take n xs, drop n xs)
where n = (length xs) `div` 2
11 changes: 11 additions & 0 deletions problems/mshibatatt/chapter4/2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- using head and tail
third_a :: [a] -> a
third_a xs = head (tail (tail xs))

-- using index
third_b :: [a] -> a
third_b xs = xs !! 2

-- using pattern match
third_c :: [a] -> a
third_c (_:_:x:_) = x
13 changes: 13 additions & 0 deletions problems/mshibatatt/chapter4/3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- condition
safetail_a :: [a] -> [a]
safetail_a xs = if null xs then [] else tail xs

-- equation with guard
safetail_b :: [a] -> [a]
safetail_b xs | null xs = []
| otherwise = tail xs

-- pattern match
safetail_c :: [a] -> [a]
safetail_c [] = []
safetail_c (_:xs) = xs
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter4/7.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mult :: Int -> Int -> Int -> Int
mult = \x -> (\y -> (\z -> x*y*z))
7 changes: 7 additions & 0 deletions problems/mshibatatt/chapter4/8.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
luhnDouble :: Int -> Int
luhnDouble x = if x2 > 9 then x2 - 9 else x2 where x2 = x*2

luhn :: Int -> Int -> Int -> Int -> Bool
luhn x y z w = (luhnDouble x + y + luhnDouble z + w) `mod` 10 == 0


1 change: 1 addition & 0 deletions problems/mshibatatt/chapter5/1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sum [x^2 | x <- [1..100]]
35 changes: 35 additions & 0 deletions problems/mshibatatt/chapter5/10.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Data.Char
alphas xs = length [x | x <- xs, (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')]
count x xs = length [x' | x' <- xs, x == x']
count_2 x y xs = length [x' | x' <- xs, x == x' || y == x']

let2int_lower c = ord c - ord 'a'
int2let_lower n = chr (ord 'a' + n)
let2int_upper c = ord c - ord 'A'
int2let_upper n = chr (ord 'A' + n)

shift n c | isLower c = int2let_lower ((let2int_lower c + n) `mod` 26)
| isUpper c = int2let_upper ((let2int_upper c + n) `mod` 26)
| otherwise = c

encode n xs = [shift n x | x <- xs]

positions x xs = [i | (x', i) <- zip xs [0..], x == x']

percent n m = (fromIntegral n) / (fromIntegral m) * 100
freqs xs = [percent (count_2 x y xs) n | (x, y) <- zip ['a'..'z'] ['A'..'Z']]
where n = alphas xs

chisqr os es = sum [((o-e)^2)/e | (o,e) <- zip os es]
rotate n xs = drop n xs ++ take n xs

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 xs = encode (-factor) xs
where
factor = head (positions (minimum chitab) chitab)
chitab = [chisqr (rotate n table') table | n <- [0..26]]
table' = freqs xs
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter5/2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
grid :: Int -> Int -> [(Int, Int)]
grid x y = [(a, b) | a <- [0..x], b <- [0..y]]
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter5/3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
square :: Int -> [(Int, Int)]
square n = [(x, y) | x <- [0..n], y <- [0..n], x /= y]
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter5/4.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
replicate_c :: Int -> a -> [a]
replicate_c n x = [x | _ <- [1..n]]
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter5/5.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x, y, z) | x <- [1..n], y <- [1..n], z <- [1..n], x^2 + y^2 == z^2]
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter5/6.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
perfects :: Int -> [Int]
perfects n = [x | x <- [1..n], sum [y | y <- [1..x-1], x `mod` y == 0] == x]
3 changes: 3 additions & 0 deletions problems/mshibatatt/chapter5/7.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[(x, y) | x <- [1, 2, 3], y <- [4, 5, 6]]
-- ?
[(z, y) | z <- [x | x <- [1, 2, 3]], y <- [4, 5, 6]]
8 changes: 8 additions & 0 deletions problems/mshibatatt/chapter5/8.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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..], x == x']

positions_find :: Eq a => a -> [a] -> [Int]
positions_find x xs = find x [xs' | xs' <- zip xs [0..]]
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter5/9.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scalarproduct :: [Int] -> [Int] -> Int
scalarproduct xs ys = sum [x*y |(x, y) <- zip xs ys]
3 changes: 3 additions & 0 deletions problems/mshibatatt/chapter6/1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fac :: Int -> Int
fac 0 = 1
fac n | n > 0 = n * fac(n-1)
3 changes: 3 additions & 0 deletions problems/mshibatatt/chapter6/2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sumdown :: Int -> Int
sumdown 0 = 0
sumdown n | n > 0 = n + sumdown(n-1)
3 changes: 3 additions & 0 deletions problems/mshibatatt/chapter6/3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(^) :: Int -> Int -> Int
m ^ 0 = 1
m ^ n | n > 0 = m * (m Main.^ (n-1))
4 changes: 4 additions & 0 deletions problems/mshibatatt/chapter6/4.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
euclid :: Int -> Int -> Int
euclid m n | m == n = m
| m < n = euclid m (n-m)
| n < m = euclid (m-n) n
20 changes: 20 additions & 0 deletions problems/mshibatatt/chapter6/6.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
and_ :: [Bool] -> Bool
and_ [] = True
and_ (x:xs) = x && and_ xs

concat_ :: [[a]] -> [a]
concat_ [[]] = []
concat_ [x:[xs]] = [x] ++ concat_ [[xs]]

replicate_ :: Int -> a -> [a]
replicate_ 0 a = []
replicate_ n a | n > 0 = [a] ++ (replicate_ (n-1) a)

index :: [a] -> Int -> a
index (x:xs) 0 = x
index (x:xs) n | n > 0 = index xs (n-1)

elem_ :: Eq a => a -> [a] -> Bool
elem_ a [] = False
elem_ a (x:xs) | a == x = True
| otherwise = elem_ a xs
6 changes: 6 additions & 0 deletions problems/mshibatatt/chapter6/7.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge xs (y:ys) = merge (small ++ [y] ++ large) ys
where
small = [x | x <- xs, x <= y]
large = [x | x <- xs, x > y]
15 changes: 15 additions & 0 deletions problems/mshibatatt/chapter6/8.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge xs (y:ys) = merge (small ++ [y] ++ large) ys
where
small = [x | x <- xs, x <= y]
large = [x | x <- xs, x > y]

halve :: [a] -> ([a], [a])
halve xs = (take n xs, drop n xs)
where n = (length xs) `div` 2

msort :: Ord a => [a] -> [a]
msort x | length x == 1 = x
| otherwise = merge (msort a) (msort b)
where (a, b) = halve x
11 changes: 11 additions & 0 deletions problems/mshibatatt/chapter6/9.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sum_a :: [Int] -> Int
sum_a [] = 0
sum_a (x:xs) = x + sum_a xs

take_b :: Int -> [a] -> [a]
take_b 0 xs = []
take_b n (x:xs) = [x] ++ take_b (n-1) xs

last_c :: [a] -> a
last_c [x] = x
last_c (x:xs) = last_c xs
9 changes: 9 additions & 0 deletions problems/mshibatatt/chapter7/10.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
luhnDouble :: Int -> Int
luhnDouble x = if x2 > 9 then x2 - 9 else x2 where x2 = x*2

altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
altMap _ _ [] = []
altMap f g (x:xs) = [f x] ++ altMap g f xs

luhn :: [Int] -> Bool
luhn x = (sum (altMap luhnDouble (\y -> y) x)) `mod` 10 == 0
19 changes: 19 additions & 0 deletions problems/mshibatatt/chapter7/2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
all_a :: (a -> Bool) -> [a] -> Bool
all_a _ [] = True
all_a p (x:xs) = (p x) && (all_a p xs)

any_b :: (a -> Bool) -> [a] -> Bool
any_b _ [] = False
any_b p (x:xs) = (p x) || (any_b p xs)

takewhile_c :: (a -> Bool) -> [a] -> [a]
takewhile_c _ [] = []
takewhile_c p (x:xs) | p x = [x] ++ takewhile_c p xs
| otherwise = []

dropwhile_d :: (a -> Bool) -> [a] -> [a]
dropwhile_d _ [] = []
dropwhile_d p (x:xs) | p x = dropwhile_d p xs
| otherwise = x:xs


2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter7/3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
map_ f = foldr (\x xs -> f x : xs) []
filter_ p = foldr (\x xs -> if p x then x:xs else xs) []
2 changes: 2 additions & 0 deletions problems/mshibatatt/chapter7/4.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dec2int :: [Int] -> Int
dec2int = foldl (\x y -> 10*x+y) 0
5 changes: 5 additions & 0 deletions problems/mshibatatt/chapter7/5.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curry :: ((a, b) -> c) -> (a -> b -> c)
curry f = \x y -> f (x, y)

uncurry :: (a -> b -> c) -> ((a, b) -> c)
uncurry f = \(x, y) -> f x y
3 changes: 3 additions & 0 deletions problems/mshibatatt/chapter7/9.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
altMap _ _ [] = []
altMap f g (x:xs) = [f x] ++ altMap g f xs