Changed repository to a cabal structure
This commit is contained in:
151
app/Main.hs
Normal file
151
app/Main.hs
Normal file
@ -0,0 +1,151 @@
|
||||
import Problems ( myLast, lastTwo, kth, myLength, rev, isPalindrome, flatten, NestedList(..), compress, pack, encode )
|
||||
--import Primes ( aspGetNextPrime, aspIsPrime, aspPrimes )
|
||||
import Primes ( aspGetNextPrime, aspIsPrime )
|
||||
import Data.Numbers.Primes ( isPrime, primes, primeFactors )
|
||||
--import Math ( mandlebrot )
|
||||
|
||||
-- Unit testing
|
||||
main :: IO()
|
||||
main = do
|
||||
testMisc
|
||||
testProblems
|
||||
testPrimes
|
||||
--mandlebrot
|
||||
|
||||
testMisc :: IO()
|
||||
testMisc = do
|
||||
putStrLn ("A ranged list from 1 to 1 is " ++ show (range 1 1))
|
||||
putStrLn ("A ranged list from -2 to 7 is " ++ show (range (-2) 7))
|
||||
putStrLn ("A ranged list from 7 to -2 is " ++ show (range 7 (-2)))
|
||||
|
||||
let tab = [2,1,10,6,3,8,1,1,6,3] :: [Int]
|
||||
putStrLn ("The initial list is " ++ show tab)
|
||||
putStrLn ("The unique list is " ++ show (nub tab))
|
||||
|
||||
putStrLn ("Is the initial list in ascending order ? " ++ show (isAsc tab))
|
||||
putStrLn ("Is 1..10 in ascending order ? " ++ show (isAsc (range 1 10)))
|
||||
putStrLn ("Is 10..1 in ascending order ? " ++ show (isAsc (range 10 1)))
|
||||
|
||||
putStrLn ("Is 3 is the list ? : " ++ show (isInList 3 tab))
|
||||
putStrLn ("Is 4 is the list ? : " ++ show (isInList 4 tab))
|
||||
putStrLn ("Is 5 is the list ? : " ++ show (isInList 5 tab))
|
||||
putStrLn ("Is 10 is the list ? : " ++ show (isInList 10 tab))
|
||||
|
||||
putStrLn ("The sorted list is " ++ show (sortList tab))
|
||||
|
||||
putStrLn ("The reverse sorted list is " ++ show (revSortList tab))
|
||||
|
||||
putStrLn ("FizzBuzz 50 " ++ show (fizzBuzz [0..50]))
|
||||
|
||||
putStrLn ("Fionacci 10 " ++ show (fibonacci 10))
|
||||
putStrLn ("FionacciList 10 " ++ show (fibonaciSequence 10))
|
||||
|
||||
testProblems:: IO()
|
||||
testProblems = do
|
||||
let assert :: (Eq a) => (Show a) => String -> a -> a -> String
|
||||
assert n r e = "Problem " ++ n ++ (if r == e then " Success" else " Failed, expected " ++ show e ++ " but got " ++ show r)
|
||||
|
||||
putStrLn (assert "1a" (myLast [1, 2, 3, 4]) (Just 4))
|
||||
putStrLn (assert "1b" (myLast ([] :: [Bool])) Nothing)
|
||||
putStrLn (assert "1c" (myLast ['x', 'y', 'z']) (Just 'z'))
|
||||
putStrLn (assert "2a" (lastTwo ['a', 'b', 'c', 'd']) (Just ('c', 'd')))
|
||||
putStrLn (assert "2b" (lastTwo ['a']) Nothing)
|
||||
putStrLn (assert "3a" (kth ['a', 'b', 'c', 'd', 'e'] 2) (Just 'c'))
|
||||
putStrLn (assert "3b" (kth ['a'] 2) Nothing)
|
||||
putStrLn (assert "4a" (myLength ['a', 'b', 'c']) 3)
|
||||
putStrLn (assert "4b" (myLength []) 0)
|
||||
putStrLn (assert "4c" (myLength "Hello, world!") 13)
|
||||
putStrLn (assert "5 " (rev ['a', 'b', 'c']) ['c', 'b', 'a'])
|
||||
putStrLn (assert "6a" (isPalindrome ['x', 'a', 'm', 'a', 'x']) True)
|
||||
putStrLn (assert "6b" (isPalindrome ['a', 'b']) False)
|
||||
putStrLn (assert "7a" (flatten (Elem 5)) [5])
|
||||
putStrLn (assert "7b" (flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]])) [1, 2, 3, 4, 5])
|
||||
putStrLn (assert "7c" (flatten (List [])) ([] :: [Int]))
|
||||
putStrLn (assert "8 " (compress "aaabccaadeeee") "abcade")
|
||||
putStrLn (assert "9 " (pack ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']) ["aaaa", "b", "cc", "aa", "d", "eeee"])
|
||||
--putStrLn (assert "10" (encode "aaabccaadeeee") [(4, 'a'),(1, 'b'),(2, 'c'),(2, 'a'),(1, 'd'),(4, 'e')])
|
||||
|
||||
testPrimes:: IO()
|
||||
testPrimes = do
|
||||
putStrLn ("Is 2 prime ? " ++ show (isPrime 2))
|
||||
putStrLn ("Is 15 prime ? " ++ show (isPrime 15))
|
||||
putStrLn ("Is 17 prime ? " ++ show (isPrime 17))
|
||||
|
||||
--putStrLn ("ASP : Is 2 prime ? " ++ show (aspIsPrime 2))
|
||||
--putStrLn ("ASP : Is 15 prime ? " ++ show (aspIsPrime 15))
|
||||
--putStrLn ("ASP : Is 17 prime ? " ++ show (aspIsPrime 17))
|
||||
|
||||
putStrLn ("primeFactors 15 " ++ show (primeFactors 15))
|
||||
putStrLn ("primeFactors 45154 " ++ show (primeFactors 45154))
|
||||
|
||||
--putStrLn ("ASP : primeFactors 15 " ++ show (aspPrimeFactors 15))
|
||||
--putStrLn ("ASP : primeFactors 45154 " ++ show (aspPrimeFactors 45154))
|
||||
|
||||
putStrLn ("primes : " ++ show (take 15 primes))
|
||||
--putStrLn ("ASP : primes : " ++ show (take 15 aspPrimes))
|
||||
|
||||
-- Sorting a given list
|
||||
sortList :: [Int] -> [Int]
|
||||
sortList [] = []
|
||||
sortList (x:xs) = sortList ys ++ [x] ++ sortList zs
|
||||
where
|
||||
ys = [a | a <- xs, a < x]
|
||||
zs = [a | a <- xs, a >= x]
|
||||
|
||||
-- Reverse sorting a given list
|
||||
revSortList :: [Int] -> [Int]
|
||||
revSortList [] = []
|
||||
revSortList (x:xs) = revSortList ys ++ [x] ++ revSortList zs
|
||||
where
|
||||
ys = [a | a <- xs, a >= x]
|
||||
zs = [a | a <- xs, a < x]
|
||||
|
||||
-- Is a certain element in a given list ?
|
||||
isInList :: (Eq a) => a -> [a] -> Bool
|
||||
isInList _ [] = False
|
||||
isInList e (x:xs) = (e == x) || isInList e xs
|
||||
|
||||
-- Create a list between from a to b
|
||||
range :: Int -> Int -> [Int]
|
||||
range a b
|
||||
| a == b = [b]
|
||||
| a > b = rev (range b a)
|
||||
| otherwise = [a..b]
|
||||
|
||||
-- Remove all duplicates from a given list
|
||||
nub :: (Eq a) => [a] -> [a]
|
||||
nub [] = []
|
||||
-- Remove duplicates after (tail recursion)
|
||||
nub (x:xs) = x : nub [a | a <- xs, a /= x]
|
||||
-- Remove duplicates before (no tail recursion)
|
||||
--nub (x:xs) = if x `elem` xs then nub xs else x : nub xs
|
||||
|
||||
-- Is a given list in an ascending order ?
|
||||
isAsc :: (Ord t) => [t] -> Bool
|
||||
isAsc [] = True
|
||||
isAsc [x] = True
|
||||
isAsc (x:y:xs) = (x <= y) && isAsc (y:xs)
|
||||
|
||||
-- FizzBuzz
|
||||
fizzBuzz :: [Int] -> [[Char]]
|
||||
fizzBuzz [] = []
|
||||
fizzBuzz (x:xs) = isFizzBuzz x : fizzBuzz xs
|
||||
where
|
||||
isFizzBuzz :: Int -> [Char]
|
||||
isFizzBuzz x
|
||||
| mod x 15 == 0 = "FizzBuzz"
|
||||
| mod x 5 == 0 = "Buzz"
|
||||
| mod x 3 == 0 = "Fizz"
|
||||
| otherwise = show x
|
||||
|
||||
-- Fibonacci number at indice n
|
||||
fibonacci :: Int -> Int
|
||||
fibonacci 0 = 0
|
||||
fibonacci 1 = 1
|
||||
fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
|
||||
|
||||
-- Fibonaci sequence until indice n
|
||||
fibonaciSequence :: Int -> [Int]
|
||||
fibonaciSequence 0 = [0]
|
||||
fibonaciSequence 1 = [0, 1]
|
||||
fibonaciSequence n = fibonaciSequence (n - 1) ++ [fibonacci n]
|
Reference in New Issue
Block a user