Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

begun the famous 99-haskell-problems

  • Loading branch information...
commit 51fcd0bb9b2a4d778808a38fceed3bfa1642c69b 1 parent 036de45
@epsilonhalbe authored
View
58 99-haskell-problems/HP0110.hs
@@ -0,0 +1,58 @@
+myLast :: [a] -> a
+myLast [] = error "there is no last in the empty list"
+myLast (x:[]) = x
+myLast (_:xs) = myLast xs
+
+myButLast :: [a] -> a
+myButLast [] = error "there is no last but one element in the empty list"
+myButLast (_:[]) = error "there is no last but one element in the singleton list"
+myButLast (x:_:[]) = x
+myButLast (_:xs) = myButLast xs
+
+elementAt :: [a] -> Int -> a
+elementAt [] _ = undefined
+elementAt (x:xs) n | n == 0 = x
+ | n < 0 = undefined
+ | otherwise = elementAt xs (n-1)
+
+myLengh :: [a] -> Int
+myLengh = sum . (map (const 1))
+
+myReverse :: [a] -> [a]
+myReverse xs = fst $ _myReverse ([],xs)
+ where _myReverse :: ([a],[a]) -> ([a],[a])
+ _myReverse (rev,[]) = (rev,[])
+ _myReverse (rev,t:ts) = _myReverse (t:rev,ts)
+
+isPalindrome ::(Eq a) => [a] -> Bool
+isPalindrome xs = xs == (myReverse xs)
+
+data List a = Elem a | List [List a]
+
+
+myFlatten :: List a -> [a]
+myFlatten (Elem x) = [x]
+myFlatten (List []) = []
+myFlatten (List (x:xs)) = (myFlatten x) ++ (myFlatten (List xs))
+
+myCompress :: (Eq a) => [a] -> [a]
+myCompress [] = []
+myCompress [x] = [x]
+myCompress xs = _myCompress ([],[],xs)
+ where _myCompress ::(Eq a) => ([a],[a],[a]) -> [a]
+ _myCompress (cmprss,c,[]) = cmprss
+ _myCompress (cmprss,c,t:ts) | [t]==c =_myCompress (cmprss,c,ts)
+ | otherwise = _myCompress (cmprss++[t],[t],ts)
+
+myPack :: (Eq a) => [a] -> [[a]]
+myPack [] = [[]]
+myPack [x] = [[x]]
+myPack xs = _myPack ([],[],xs)
+ where _myPack ::(Eq a) => ([[a]],[a],[a]) -> [[a]]
+ _myPack (pck,c,[]) = myReverse pck
+ _myPack (p:pck,c,t:ts) | [t]==c =_myPack ((t:p):pck,c,ts)
+ | otherwise = _myPack ([t]:p:pck,[t],ts)
+
+myEncode :: (Eq a) => [a] -> [(Int,a)]
+myEncode [] = []
+myEncode xs = map (\x -> (length x, head x)) (myPack xs)
View
76 99-haskell-problems/HP1120.hs
@@ -0,0 +1,76 @@
+module HP1120 where
+import Data.List (group)
+import Control.Arrow (first,
+ (&&&))
+
+data Count a = Single a
+ | Multiple Int a
+ deriving (Eq, Show)
+
+encode :: String -> [(Int, Char)]
+encode = map (length &&& head) . group
+
+encodeModified :: String -> [Count Char]
+encodeModified = map transform . encode
+
+transform :: (Int,Char) -> Count Char
+transform (1,x) = Single x
+transform (i,x) = Multiple i x
+
+decodeModified :: [Count Char] -> String
+decodeModified = concatMap stringify
+
+stringify :: Count Char -> String
+stringify (Single x) = [x]
+stringify (Multiple i x) = replicate i x
+
+(.+.) :: Count Char -> [Count Char] -> [Count Char]
+x .+. [] = [x]
+y@(Single x2) .+. xx@(Single x1:xs) | x1 == x2 = Multiple 2 x1:xs
+ | otherwise = y:xx
+y@(Single x2 ) .+. xx@(Multiple i1 x1:xs) | x1 == x2 = Multiple (i1+ 1) x1:xs
+ | otherwise = y:xx
+y@(Multiple i2 x2) .+. xx@(Single x1:xs) | x1 == x2 = Multiple (1 +i2) x1:xs
+ | otherwise = y:xx
+y@(Multiple i2 x2 ) .+. xx@(Multiple i1 x1:xs) | x1 == x2 = Multiple (i1+i2) x1:xs
+ | otherwise = y:xx
+
+encodeDirect :: String -> [Count Char]
+-- encodeDirect = foldr (.+.) [] . map transform . zip (repeat 1)
+encodeDirect = foldr ((.+.) . transform) [] . zip (repeat 1)
+
+dupli :: [a] -> [a]
+dupli = foldr (\x y -> x:x:y) []
+
+repli :: [a] -> Int -> [a]
+repli xx i = foldr (\x y -> replicate i x++y) [] xx
+
+dropEvery :: [a] -> Int -> [a]
+dropEvery xx 0 = xx
+dropEvery xx i = _dropEvery xx i
+ where _dropEvery [] _ = []
+ _dropEvery (x:xs) 1 = _dropEvery xs i
+ _dropEvery (x:xs) n = x:_dropEvery xs (n-1)
+
+split :: [a] -> Int -> ([a],[a])
+split x 0 = ([],x)
+split x i | i < 0 = split x (length x + i)
+ | otherwise = _split ([],x) i
+ where _split xx 0 = xx
+ _split (y,x:xs) i = _split (y++[x],xs) (i-1)
+
+slice :: [a] -> Int -> Int -> [a]
+slice xx i1 i2 = [x| (x,i)<- zip xx [1..], i1<=i,i<=i2]
+
+rotate :: [a] -> Int -> [a]
+rotate x i = bb++aa
+ where (aa,bb) = split x i
+
+removeAt :: Int -> [a] -> (a,[a])
+removeAt i x | i<0 = (b,aa++bs)
+ where (aa,b:bs) = split x i
+removeAt i x = (head a,as++bb)
+ where (aa,bb) = split x (i+1)
+ (as,a ) = split aa (-1)
+
+
View
18 99-haskell-problems/HP3141.hs
@@ -0,0 +1,18 @@
+module HP3141 where
+
+minus :: (Ord a) => [a] -> [a] -> [a]
+minus (x:xs) (y:ys) = case compare x y of
+ LT -> x : minus xs (y:ys)
+ EQ -> minus xs ys
+ GT -> minus (x:xs) ys
+minus xs _ = xs
+
+-- isPrime :: Int -> Bool
+-- isPrime i = foldr (\x y -> i `rem` x /= 0 && y) True (primeList i)
+isPrime i = last (primeList i) == i
+
+primeList :: Int -> [Int]
+primeList m = 2 : sieve [3,5..m]
+ where sieve (p:xs) | p*p > m = p : xs
+ | otherwise = p : sieve (xs `minus` [p*p, p*p+2*p..])
+
View
44 99-haskell-problems/HP4650.hs
@@ -0,0 +1,44 @@
+module HP4150 where
+import Data.List (transpose)
+
+and2 :: Bool -> Bool -> Bool
+True `and2` b = b
+_ `and2` _ = False
+
+or2 :: Bool -> Bool -> Bool
+False `or2` b = b
+_ `or2` _ = True
+
+table :: (Bool -> Bool -> Bool) -> [[Bool]]
+table f = [[a,b,f a b] | a <- [True,False],
+ b <- [True,False]]
+
+inputValues :: Int -> [[Bool]]
+inputValues i = transpose $ map genTable [2^j | j <- [i-1,i-2..0]]
+ where genTable = take (2^i) . cycle . tblSequence
+ tblSequence i = replicate i True++replicate i False
+
+and',or',nor',nand',xor',impl',equ' :: Bool -> Bool -> Bool
+and' = (&&)
+or' = (||)
+nand' a b = not (and' a b)
+nor' a b = not (or' a b)
+xor' a b = not (equ' a b)
+impl' a = or' (not a)
+equ' = (==)
+
+table' :: Int -> ([Bool] -> Bool) -> [[Bool]]
+table' i f = [x++[f x]|x <- xs]
+ where xs = inputValues i
+
+prettyfy :: (Show a) => [[a]] -> String
+prettyfy = (unlines . map unwords . map . map . id) show
+
+--gray :: Int -> [String]
+{-gray i = (map . map) f $ inputValues i
+ where f True = '1'
+ f False = '0'-}
+gray 1 = ["0","1"]
+gray n = map ('0':) gg ++ map ('1':) (reverse gg)
+ where gg = gray (n-1)
+
View
51 99-haskell-problems/t99HP.hs
@@ -0,0 +1,51 @@
+import HP1120
+import HP2130
+import Test.HUnit
+
+main = testAll
+
+testAll = runTestTT $ TestList tests1120
+
+tests1120 :: [Test]
+tests1120 =
+ ["10" ~: "encode" ~: [(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')]
+ ~=? encode "aaaabccaadeeee",
+ "11" ~: "encodeModified" ~: [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e']
+ ~=? encodeModified "aaaabccaadeeee",
+ "12" ~: "decodeModified" ~: "aaaabccaadeeee"
+ ~=? decodeModified [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e'],
+ "13" ~: "encodeDirect" ~: [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e']
+ ~=? encodeDirect"aaaabccaadeeee",
+ "14" ~: "dupli" ~: [1,1,2,2,3,3]
+ ~=? dupli [1,2,3],
+ "15" ~: "dupli" ~: "aaabbbccc"
+ ~=? repli "abc" 3,
+ "16" ~: "dropEvery" ~: "abdeghk"
+ ~=? dropEvery "abcdefghik" 3,
+ "17" ~: "split 3" ~: ("abc", "defghik")
+ ~=? split "abcdefghik" 3,
+ "17" ~: "split 0" ~: ("", "abcdefghik")
+ ~=? split "abcdefghik" 0,
+ "17" ~: "split -3" ~: ("abcdefg", "hik")
+ ~=? split "abcdefghik" (-3),
+ "18" ~: "slice" ~: "cdefg"
+ ~=? slice "abcdefghik" 3 7,
+ "19" ~: "rotate 3" ~: "defghabc"
+ ~=? rotate "abcdefgh" 3,
+ "19" ~: "rotate -2" ~: "ghabcdef"
+ ~=? rotate "abcdefgh" (-2),
+ "20" ~: "removeAt 1" ~: ('b',"acd")
+ ~=? removeAt 1 "abcd",
+ "20" ~: "removeAt -1" ~: ('d',"abc")
+ ~=? removeAt (-1) "abcd",
+ "00" ~: "dummy" ~: True
+ ~=? True
+ ]
+
+tests3140 :: [Test]
+tests3140 =
+ ["31" ~: "isPrime" ~: isPrime 27
+ ~=? isPrime 7,
+ "00" ~: "dummy" ~: True
+ ~=? True
+ ]
Please sign in to comment.
Something went wrong with that request. Please try again.