import Data.List import Control.Monad import Control.Applicative import Data.Functor.Contravariant --- SECTION 1 / syntaxe --- mysorting :: [Int] -> [Int] mysorting l = sort q where q = take (n `div` 2) l n = length l mysorting' :: [Int] -> [Int] mysorting' l = let n = length l in let q = take (n `div` 2) l in sort q ------ --- -- EXERCICE ##### --- ------ myLast :: [a] -> a myLast [] = error "No end for empty lists!" myLast [x] = x myLast (_:xs) = myLast xs myLastByComposition = head . reverse myLastByFolding :: [a] -> a myLastByFolding l = foldr u v l where u :: a -> a -> a u = undefined v :: a v = undefined ------ --- -- EXERCICE ##### --- ------ -- String = [Char] isPalindrome :: String -> String isPalindrome x = undefined ------ --- -- EXERCICE ##### --- ------ qsort :: [Int] -> [Int] qsort [] = [] qsort [x] = [x] qsort (x:xs) = qsort l1 ++ [x] ++ qsort l2 where l1 = [ y | y <- xs, y <= x ] l2 = undefined ------ --- -- EXERCICE ##### --- ------ data Comparable a = Comparable (a -> a -> Bool) qsort':: Comparable a -> [a] -> [a] qsort' (Comparable cmp) l = undefined ------ --- -- EXERCICE ##### --- ------ class Serialisable a where serialise :: a -> String instance Serialisable Int where serialise = undefined instance Serialisable a => Serialisable [a] where serialise = undefined instance (Serialisable a, Serialisable b) => Serialisable (a,b) where serialise = undefined example :: (Int, [(Int,[Int])]) example = (10, [(8, [10,10]), (7, [8,9])]) test :: String test = serialise example ----- -- FUNCTOR EXERCICES ----- div1 :: Int -> Int -> Maybe Int div1 = undefined div2 :: Int -> [Int] -> [ Maybe Int ] div2 a l2 = undefined distributiveLaw1 :: [Maybe a] -> Maybe [a] distributiveLaw1 = undefined distributiveLaw2 :: Maybe [a] -> [Maybe a] distributiveLaw2 = undefined -- -- SPECIAL FUNCTOR -- newtype Probas a = Probas (a -> Int) newtype P2 a = P2 [(a,Int)] instance Functor P2 where fmap f (P2 l) = undefined instance Contravariant Probas where contramap f (Probas g) = undefined instance Contravariant P2 where contramap f (P2 g) = undefined instance Functor Probas where fmap f (Probas g) = undefined -- -- APPLICATIVE FUNCTORS -- allDivs :: [Int] -> [Int] -> [Maybe Int] allDivs l1 l2 = undefined safeDivs :: Int -> Int -> Maybe (Int, Int) safeDivs a b = undefined <$> div1 a b <*> div1 b a instance Applicative P2 where pure x = undefined (<*>) (P2 f) (P2 g) = undefined -- -- ALTERNATIVE FUNCTORS -- instance Alternative P2 where empty = undefined (<|>) (P2 f) (P2 g) = undefined {- instance Alternative [] where empty = undefined (<|>) l1 l2 = undefined instance Alternative Maybe where empty = undefined (<|>) a b = undefined -} superDivision :: Int -> Int -> Maybe Int superDivision a b = (+1) <$> (div1 a b <|> div1 a b) -- -- MONAD -- instance Monad P2 where return x = undefined (>>=) f x = undefined allPythagorianSquares :: Int -> [(Int,Int,Int)] allPythagorianSquares n = do x <- [1..n] undefined return undefined main :: IO () main = do putStrLn ""