Skip to content

Instantly share code, notes, and snippets.

@MarkArts
Last active May 24, 2016 10:34
Show Gist options
  • Save MarkArts/d1997952ef26c8f75eb28247681c3397 to your computer and use it in GitHub Desktop.
Save MarkArts/d1997952ef26c8f75eb28247681c3397 to your computer and use it in GitHub Desktop.
99 haskell problems progress
main = do
let myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 3, 3, 4, 7]
putStrLn $ show myList
putStrLn $ show $ myLast myList
putStrLn $ show $ myButLast myList
putStrLn $ show $ elementAt myList 3
putStrLn $ show $ myLength myList
putStrLn $ show $ myReverse myList
putStrLn $ show $ isPalindrome myList
putStrLn $ show $ flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]])
putStrLn $ show $ compress myList
putStrLn $ show $ pack myList
putStrLn $ show $ encode myList
let encodedMod = encodeModified myList
putStrLn $ show $ encodedMod
putStrLn $ show $ decodeModified encodedMod
putStrLn $ show $ encodeDirect myList
putStrLn $ show $ dupli myList
putStrLn $ show $ repli myList 4
putStrLn $ show $ dropEvery myList 3
putStrLn $ show $ dropEvery' myList 3
putStrLn $ show $ split myList 3
putStrLn $ show $ slice myList 3 7
putStrLn $ show $ rotate myList (-4)
putStrLn $ show $ removeAt myList 3
myLast :: [a] -> a
myLast [x] = x
myLast (_:xs) = myLast xs
myButLast :: [a] -> a
myButLast [x, _] = x
myButLast (_:xs) = myButLast xs
elementAt :: (Num n, Eq n) => [a] -> n -> a
elementAt (x:_) 1 = x
elementAt (_:xs) n = elementAt xs (n - 1)
myLength :: (Num n, Eq n) => [a] -> n
myLength [] = 0
myLength [x] = 1
myLength (_:xs) = count xs 1
where count [] n = n
count (_:xs) n = count xs (n+1)
myReverse :: [a] -> [a]
myReverse [] = []
myReverse [x] = [x]
myReverse (x:xs) = myReverse xs ++ [x]
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome xs = myReverse xs == xs
data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem x) = [x]
flatten (List (x:xs)) = flatten x ++ flatten (List xs)
flatten (List []) = []
compress :: (Eq a) => [a] -> [a]
compress (x:ys@(y:_))
| x == y = compress ys
| otherwise = x : compress ys
compress xs = xs
pack :: (Eq a) => [a] -> [[a]]
pack [] = []
pack (x:xs) = let (first, rest) = span (==x) xs
in ( x : first) : pack rest
encode :: (Eq a) => [a] -> [(Int, a)]
encode [] = []
encode xs = map func packed
where packed = pack xs
func ys = (length ys, head ys)
data Encoded a = Multiple Int a | Single a
deriving (Show)
encodeModified :: (Eq a) => [a] -> [Encoded a]
encodeModified = map func . pack
where func ys = if length ys > 1
then Multiple (length ys) (head ys)
else Single (head ys)
decodeModified :: (Eq a) => [Encoded a] -> [a]
decodeModified = concatMap func
where func (Single x) = [x]
func (Multiple n x) = replicate n x
encodeDirect :: (Eq a) => [a] -> [Encoded a]
encodeDirect [] = []
encodeDirect (x:xs) = let (first, rest) = span (==x) xs
encoded = if (length first < 1)
then Single x
else Multiple (length first + 1) x
in encoded : encodeDirect rest
dupli :: [a] -> [a]
dupli xs = concat [ [x,x] | x <- xs]
repli :: [a] -> Int -> [a]
repli [] _ = []
repli (x:xs) n = concat $ replHelper x n : [(repli xs n)]
where replHelper y 0 = []
replHelper y nn = y : (replHelper y (nn-1))
dropEvery :: [a] -> Int -> [a]
dropEvery xs n = func xs n
where func [] _ = []
func (_:xs) 1 = func xs n
func (x:xs) count = x : (func xs (count-1))
dropEvery' :: [a] -> Int -> [a]
dropEvery' [] _ = []
dropEvery' xs n = take (n-1) xs ++ dropEvery' (drop n xs) n
split :: [a] -> Int -> ([a], [a])
split [] _ = ([], [])
split xs n = helper ([], []) xs
where helper acc [] = acc
helper (ys,zs) (x:xs)
| length ys < n = helper (ys++[x], zs) xs
| otherwise = helper (ys, zs++[x]) xs
slice :: [a] -> Int -> Int -> [a]
slice [] _ _ = []
slice xs start end = take (end - start) rest
where (_, rest) = split xs start
rotate :: [a] -> Int -> [a]
rotate [] _ = []
rotate xs n
| n > 0 = drop n xs ++ take n xs
| n < 0 = rotate xs (length xs + n)
removeAt :: [a] -> Int -> Maybe (a, [a])
removeAt xs n
| n > 0 && n <= length xs = Just $ ((xs !! (n-1)), init (take n xs) ++ drop n xs)
| otherwise = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment