Plus de Haskell. On y est presque !
This commit is contained in:
parent
6a106374d5
commit
50586a7a9c
11
HSK14.md
Normal file
11
HSK14.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Haskell 14
|
||||
|
||||
### Exercice 1
|
||||
|
||||
1. Le pattern matching a pour objectif de «déconstruire» un objets en ses «composants». Là où un guarde a pour but de réagir différement selon certaines valeurs booléenes indiquées.
|
||||
2. On peut approcher ce concept des structures `switch` présents dans certains langages impératifs, ou de suites `if`,`else if`, `else if` , … , `else if`,`else`
|
||||
3. Les différents conditions d'un garde sont écrites en les commençant par un *pipe* `|`. La structure est `| expressionBooleene = valeurSiEvaluéeÀTrue`
|
||||
4. Le `else` correspond au mot clé `otherwise` qui prend la place d'une expression booléenne.
|
||||
|
||||
### Exercice 2
|
||||
Voir fstfunc.hs
|
||||
69
HSK15.md
Normal file
69
HSK15.md
Normal file
@ -0,0 +1,69 @@
|
||||
# Haskell 15
|
||||
|
||||
### Exercice 1
|
||||
|
||||
1.
|
||||
```
|
||||
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"
|
||||
```
|
||||
2. Le mot-clé `where` sert en Haskell à définir des variables temporaires afin d'éviter de réutiliser une expression à plusieurs emplacement du code. Cela permet d'assure que le calcul n'est effectué qu'une seule fois.
|
||||
3.
|
||||
```
|
||||
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
|
||||
```
|
||||
L'intéret est que les variables on plus de sens (car elles ont maintenant un nom) que des formules.
|
||||
4.
|
||||
```
|
||||
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
|
||||
```
|
||||
|
||||
### Exercice 2
|
||||
|
||||
1. Il est tout à fait possible d'utiliser le *pattern matching* dans un `where`.
|
||||
2.
|
||||
```
|
||||
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)
|
||||
```
|
||||
3.
|
||||
```
|
||||
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
|
||||
```
|
||||
|
||||
### Exercice 3
|
||||
1. Il est tout à fait possible de définir des fonctions dans un `where`.
|
||||
2.
|
||||
```
|
||||
listMean :: [(Double,Double)] -> [Double]
|
||||
listMean l = [moy x y | (x,y) <- l]
|
||||
where moy u v = (u+v)/2
|
||||
```
|
||||
50
HSK16.md
Normal file
50
HSK16.md
Normal file
@ -0,0 +1,50 @@
|
||||
# Haskell 16
|
||||
|
||||
### Exercice 1
|
||||
1. `let` reste à travers l'execution (une définition) alors que `where` crée une définition uniquement valable dans le bloc le précédent.
|
||||
2. On ne peut pas utiliser `let` dans un *pattern matching* car aucun pattern ne «correspond» à un `let`.
|
||||
3. La syntaxe du `let` est la suivante: `let nomDeVariable = définition`.
|
||||
4. Les noms sont disponibles avant les signes `=` et les définitions sont juste après.
|
||||
5.
|
||||
```
|
||||
Prelude> let a=2
|
||||
Prelude> let b=98
|
||||
Prelude> let c=34
|
||||
Prelude> let f="Bonjour"
|
||||
Prelude> let g=" je suis "
|
||||
Prelude> let h="un let."
|
||||
Prelude> (a+b+c,f++g++h)
|
||||
(134,"Bonjour je suis un let.")
|
||||
```
|
||||
|
||||
6.
|
||||
```
|
||||
Prelude> let pow3 x = x*x*x
|
||||
Prelude> let t=(pow3 12, pow3 9)
|
||||
Prelude> t
|
||||
(1728,729)
|
||||
```
|
||||
|
||||
### Exercice 2
|
||||
|
||||
1.
|
||||
```
|
||||
listMeanlet :: [(Double,Double)] -> [Double]
|
||||
listMeanlet l = [moy x y | d<-l, let (x,y) =d]
|
||||
where moy u v = (u+v)/2
|
||||
```
|
||||
2. Le `let` utilise le *pattern matching* et crée des définitions utilisables uniquement à l'interieur de la liste en compréhension.
|
||||
3.
|
||||
```
|
||||
listMeanlet2 :: [(Double,Double)] -> [Double]
|
||||
listMeanlet2 l = [let (x,y) =d in moy x y | d<-l]
|
||||
where moy u v = (u+v)/2
|
||||
```
|
||||
4. Ici, le `let` est «à l'interieur» de la liste. Cette liste est comprise par haskell comme la liste des `let (x,y) = d in moy x y` pour `d` dans `l`. Le `let` crée une expression plutôt qu'une définition.
|
||||
5.
|
||||
```
|
||||
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
|
||||
```
|
||||
6. On ne pourrait pas ajouter (simplement) cette condition sans réécrire une décomposition dans `listMeanlet2` car les variables `x` et `y` ne sont pas définies dans le contexte de la liste.
|
||||
9
HSK17.md
Normal file
9
HSK17.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Haskell 17
|
||||
|
||||
### Exercice 1
|
||||
|
||||
1. La structure `case` en Haskell permet de gérer différents *cas*, de réagir différement suivant les différentes formes que peut avoir une variable.
|
||||
2. La définition au pattern matching définit la fonction «par parties» (entre les deux lignes, `longueurListe` n'est pas défini pour les listes non vide), là ou avec le case, nous définissons directement la fonction sur tout type de liste.
|
||||
|
||||
### Exercice 2
|
||||
Voir fstfunc.hs
|
||||
118
fstfunc.hs
118
fstfunc.hs
@ -78,5 +78,123 @@ 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]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user