Fiche Haskell 13
This commit is contained in:
parent
49e4c0148b
commit
6a106374d5
85
HSK13.md
Normal file
85
HSK13.md
Normal file
@ -0,0 +1,85 @@
|
||||
# Haskell 13
|
||||
|
||||
### Exercice 1
|
||||
1. Le pattern matching est un moyen d'extraire des variables d'une variable *composite* comme une liste, un tuple par exemple. Cela permet aussi de vérifier qu'une variable a bien une forme donnée, ou de faire une disjonction de cas.
|
||||
2. Cela permet d'écrire des fonctions très proches de la notation mathématiques et très simple à écrire et surtout à comprendre ca très visuelles.
|
||||
3.
|
||||
```
|
||||
greaterTwo :: [Char] -> Bool
|
||||
greaterTwo "one" = False
|
||||
greaterTwo "two" = False
|
||||
greaterTwo _ = True
|
||||
```
|
||||
4. Les motifs sont lus dans l'ordre dans lequel ils ont été écrits. C'est à dire que les premiers motifs prévalent.
|
||||
5.
|
||||
```
|
||||
divide :: Fractional a => Eq a => a -> a -> a
|
||||
divide _ 0 = -9999
|
||||
divide p q = p/q
|
||||
```
|
||||
|
||||
### Exercice 2
|
||||
```
|
||||
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
|
||||
```
|
||||
|
||||
### Exercice 3
|
||||
```
|
||||
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)
|
||||
```
|
||||
|
||||
L'intéret du *pattern matching* est de rendre la lecture et la compréhension du code beaucoup plus simple.
|
||||
|
||||
### Exercice 4
|
||||
Voir fstfunc.hs
|
||||
|
||||
### Exercice 5
|
||||
Voir fstfunc.hs
|
||||
|
||||
### Exercice 6
|
||||
1. OK
|
||||
2. Pour ajouter un élément en tête de liste, il suffit d'utiliser l'opérateur *cons* dénoté par `:`.
|
||||
3. Le premier pattern correspond uniquement à la liste vide. Le second correspond à toute liste avec pour dernier constructeur *cons*. Ce qui veut dire qu'il correspond à toute liste non vide (il n'y a que deux constructeurs). La variable de tête est assignée au nom `x` et le reste est ignoré.
|
||||
4. Correspondent toutes les listes non vides.
|
||||
|
||||
### Exercice 7
|
||||
1.
|
||||
```
|
||||
twoEq :: Eq a => [a] -> [a] -> Bool
|
||||
twoEq (x:_) (y:_) = (x==y)
|
||||
twoEq [] [] = True
|
||||
twoEq _ _ = False
|
||||
```
|
||||
2. Les éléments doivent être de la classe `Eq` afin de pouvoir être comparés.
|
||||
|
||||
### Exercice 8
|
||||
1.
|
||||
```
|
||||
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"
|
||||
```
|
||||
2. Les objets doivent étendrent la classe `Show` pour être affichés.
|
||||
|
||||
3.
|
||||
```
|
||||
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"
|
||||
```
|
||||
63
fstfunc.hs
63
fstfunc.hs
@ -1,4 +1,6 @@
|
||||
{-# LANGUAGE ParallelListComp #-}
|
||||
import Text.Printf
|
||||
|
||||
addTwo :: Num a => a -> a
|
||||
addTwo x = x + 2
|
||||
|
||||
@ -17,3 +19,64 @@ 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"
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user