{-# LANGUAGE ParallelListComp #-} import Text.Printf addTwo :: Num a => a -> a addTwo x = x + 2 threeParam :: Num a => a -> a -> a -> a threeParam x y z = let w = x+y in w*z myFirst :: (a,b) -> a myFirst t = let (x,y)=t in x okko :: [Int] -> [[Char]] okko xs = [if x<10 then "OK" else "KO" | x <- xs] addList :: [Int] -> [Int] -> [Int] addList xs ys = [x+y | x <- xs, y <- ys] length2 :: [a] -> Int length2 xs = last [n | _ <- xs | n <- [2,4..]] greaterTwo :: [Char] -> Bool greaterTwo "one" = False greaterTwo "two" = False greaterTwo _ = True divide :: Fractional a => Eq a => a -> a -> a divide _ 0 = -9999 divide p q = p/q myand :: Bool -> Bool -> Bool myand False False = False myand True False = True myand False True = True myand True True = False myand2 :: Bool -> Bool -> Bool myand2 False b = b myand2 True b = not b compVectOld :: (Double,Double) -> (Double,Double) -> Double compVectOld u v = ((fst u) + (fst v)) * ((snd u) + (snd v)) compVect :: Real a => (a,a) -> (a,a) -> a compVect (x,y) (w,z) = (x+y)*(w+z) myFst :: (a,a,a) -> a myFst (x,y,z) = x mySnd :: (a,a,a) -> a mySnd (x,y,z) = y myThd :: (a,a,a) -> a myThd (x,y,z) = z listPat :: Num a => [(a,a,a)] -> [a] listPat [] = [] listPat ((x,y,z):s) = (x+y*z):(listPat s) myHead :: [a] -> a myHead [] = error "The list is empty !" myHead (x:_) = x twoEq :: Eq a => [a] -> [a] -> Bool twoEq (x:_) (y:_) = (x==y) twoEq [] [] = True twoEq _ _ = False myLength :: PrintfType r => [a] -> r myLength [] = printf "The list has no elements\n" myLength [x] = printf "The list has one element\n" myLength [x,y] = printf "The list has two elements\n" myLength _ = printf "The list has more than two elements\n" myLengthShow :: PrintfType r => Show a => [a] -> r myLengthShow [] = printf "The list has no elements\n" myLengthShow [x] = printf "The list has one element: %s\n" (show x) myLengthShow [x,y] = printf "The list has two elements: %s and %s\n" (show x) (show y) myLengthShow _ = printf "The list has more than two elements\n" goodStudent :: Real a => a -> String goodStudent m | m>12 = "You are a good student" | m>=10 = "You can be do what you want to do" | otherwise = "You are a bad student" myMin :: Ord a => a -> a -> a myMin a b | a (a,a) -> (a,a) -> (a,a) maxVect (x,y) (x',y') | x>=x' && y>=y' = (x,y) | x>=x' = (x,y') | y>=y' = (x',y) | otherwise = (x',y') myMean :: Fractional a => Real a => a -> a -> String myMean x y | (x+y)/2>12 = "You are a good student" | (x+y)/2>=10 = "You can be do what you want to do" | otherwise = "You are a bad student" myMean2 :: Fractional a => Real a => a -> a -> String myMean2 x y | moy>12 = "You are a good student" | moy>=10 = "You can be do what you want to do" | otherwise = "You are a bad student" where moy=(x+y)/2 myMean3 :: Fractional a => Real a => a -> a -> String myMean3 x y | moy>=acceptable = "You are a good student" | moy>=pass = "You can be do what you want to do" | otherwise = "You are a bad student" where moy=(x+y)/2 pass=10 acceptable=12 myMean4 :: Fractional a => Real a => a -> a -> String myMean4 x y | moy>=acceptable = "You are a good student" | moy>=pass = "You can be do what you want to do" | otherwise = "You are a bad student" where (moy,pass,acceptable)=((x+y)/2,10,12) whereList :: Show a => Show b => [a] -> [b] -> String whereList l1 l2 | length l1 > 1 && length l2 > 1 = show secondl1 ++ " " ++ show lastl2 | length l1 <= 1 && length l2 <= 1 = "The lengths of l1 and l2 are too short" | length l1 <= 1 = "The length of l1 is too short" | length l2 <= 1 = "The length of l2 is too short" where _:secondl1:_ = l1 lastl2 = last l2 listMean :: [(Double,Double)] -> [Double] listMean l = [moy x y | (x,y) <- l] where moy u v = (u+v)/2 listMeanlet :: [(Double,Double)] -> [Double] listMeanlet l = [moy x y | d<-l, let (x,y) =d] where moy u v = (u+v)/2 listMeanlet2 :: [(Double,Double)] -> [Double] listMeanlet2 l = [let (x,y) =d in moy x y | d<-l] where moy u v = (u+v)/2 listMeanlet3 :: [(Double,Double)] -> [Double] listMeanlet3 l = [m| d<-l, let (x,y) =d, let m = moy x y, m>=12] where moy u v = (u+v)/2 modifStrings :: [[Char]] -> [[Char]] modifStrings l = [ case s of { [] -> "null"; [e] -> "indefini"; [e,f] -> [e]; _ -> [last s] } | s<-l] allMin :: Ord a => [a] -> a allMin [e] = e allMin (e:s) = min e (allMin s) fact :: Num a => Eq a => a -> a fact 0 = 1 fact n = n*(fact (n-1)) replicate' :: (Num i, Ord i) => i -> a -> [a] replicate' n x | n <= 0 = [ ] | otherwise = x : replicate' (n - 1) x ssum :: Num a => [a] -> a ssum [] = 0 ssum (e:s) = e+(ssum s) ppea :: Ord a => a -> [a] -> [a] ppea x [] = [] ppea x (e:s) | e<=x = e:(ppea x s) | otherwise = (ppea x s) pgq :: Ord a => a -> [a] -> [a] pgq x [] = [] pgq x (e:s) | e>x = e:(pgq x s) | otherwise = (pgq x s) triPivot :: Ord a => [a] -> [a] triPivot [] = [] triPivot (x:s) = (triPivot (ppea x s)) ++ [x] ++ (triPivot (pgq x s)) apply3 :: (a -> a) -> a -> a apply3 f x = f (f (f x)) applyN :: Int -> (a->a) -> a -> a applyN 0 f x = x applyN n f x = f (applyN (n-1) f x) myMap :: (a -> b) -> [a] -> [b] myMap f l = [f x | x <- l] myMap2 :: (a->b)->[a]->[b] myMap2 f [] = [] myMap2 f (e:s) = (f e):(myMap2 f s) myFilter :: (a -> Bool) -> [a] -> [a] myFilter f [] = [] myFilter f (e:s) | f e = e:(filter f s) | otherwise = filter f s myZipWith :: (a -> b -> c) -> [a] -> [b] -> [c] myZipWith o l l' = map applicateur (zip l l') where applicateur (x,y) = o x y minimum' :: [Float] -> Float minimum' [e] = e minimum' (e:s) = min e (minimum' s) minimum'' :: [Float] -> Float minimum'' (u:l) = go l u where go [] x = x go (e:s) x | e>x = go s x | otherwise = go s e and' :: [Bool] -> Bool and' [] = True and' (e:s) = e && (and' s) and'' :: [Bool] -> Bool and'' l = go l True where go [] x = x go (e:s) x = go s (e && x) reduce :: (t->t->t) -> [t] -> t reduce f [e] = e reduce f (e:s) = f e (reduce f s) reduce' :: (t->t->t) -> [t] -> t reduce' f (e:s) = go s e where go [] x = x go (h:t) x = go t (f h x) rminimum l = reduce' min l rand l = reduce' (&&) l reduceTotal :: (t->t->t) -> t -> [t] -> t reduceTotal f x0 [] = x0 reduceTotal f x0 (e:s) = f e (reduceTotal f x0 s) reduceTotal' :: (t->t->t) -> t -> [t] -> t reduceTotal' f x0 l = go l x0 where go [] x = x go (h:t) x = go t (f h x) data Vector = Vec3 Float Float Float magnitude :: Vector -> Float magnitude (Vec3 a b c) = sqrt (a*a+b*b+c*c) data Arbre t = Noeud t (Arbre t) (Arbre t) | Feuille arbreSomme :: Num t => Arbre t -> t arbreSomme Feuille = 0 arbreSomme (Noeud v a b) = v + (arbreSomme a) + (arbreSomme b) headSafe :: [t] -> Maybe t headSafe [] = Nothing headSafe (e:s) = Just e