Last active
August 29, 2015 14:01
-
-
Save volgar1x/87cd608d2ab0a8db17fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
main = run tests where | |
run [] = putStrLn "no tests to run" | |
run [x] = x | |
run (x:xs) = x >> sep >> run xs | |
tests = [ | |
myConcatTest, | |
concatManyTest, | |
myReverseTest, | |
sliceTest, | |
removeAtTest | |
] | |
sep = putStrLn "---------------------" | |
myConcat :: [a] -> [a] -> [a] | |
myConcat left right = rec left right | |
where | |
rec [] [] = [] | |
rec [] (x:xs) = x : rec [] xs | |
rec (x:xs) right = x : rec xs right | |
myConcatTest :: IO () | |
myConcatTest = do | |
print $ myConcat "abc" "def" | |
concatMany :: [[a]] -> [a] | |
concatMany list = rec list [] | |
where | |
rec :: [[a]] -> [a] -> [a] | |
rec [] acc = acc | |
rec (x:xs) acc = rec xs (myConcat acc x) | |
concatManyTest :: IO () | |
concatManyTest = do | |
print $ concatMany ["abc", "def", "ghi"] | |
myReverse :: [a] -> [a] | |
myReverse list = rec list [] | |
where | |
rec [] acc = acc | |
rec (x:xs) acc = rec xs (x:acc) | |
myReverseTest :: IO () | |
myReverseTest = do | |
print $ myReverse "abcd" | |
slice :: Int -> Int -> [a] -> [a] | |
slice start len list = rec start len list | |
where | |
rec 0 0 _ = [] | |
rec _ _ [] = [] | |
rec 0 len (x:xs) = x : rec 0 (len-1) xs | |
rec start len (x:xs) = rec (start-1) len xs | |
sliceTest :: IO () | |
sliceTest = do | |
print $ slice 1 1 [1, 2, 3] | |
print $ slice 4 3 "abcdefghijklm" | |
removeAt :: Int -> [a] -> (a, [a]) | |
removeAt 0 (x:xs) = (x, xs) | |
removeAt i list = rec i list [] | |
where | |
rec 0 (x:xs) acc = (x, myReverse $ myConcat xs acc) | |
rec i (x:xs) acc = rec (i-1) xs (x:acc) | |
removeAtTest :: IO () | |
removeAtTest = do | |
print $ removeAt 1 "abc" | |
print $ removeAt 2 [1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment