projprog/fstfunc.hs
2022-01-08 04:21:26 +01:00

212 lines
4.8 KiB
Haskell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{-# 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<b = a
| otherwise = b
maxVect :: Ord 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]
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