-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTP1.hs
More file actions
172 lines (123 loc) · 3.34 KB
/
TP1.hs
File metadata and controls
172 lines (123 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Data.List
{-
-- :r
-- commentaires, multilignes
-}
-- constante entiere, identifiant, declaration typee, definition
un :: Int
un = 1
deux :: Int
deux = un + un
-- fonction, declaration typee, definition
mySub :: Int -> Int -> Int
mySub x y = x-y
neg :: Int -> Int
--neg x = mySub 0 x
neg = mySub 0 -- application partielle
-- liste d'entier, nil, cons, liste en comprehension
l0 :: [Int]
l0 = []
l1 :: [Int]
l1 = 1:l0
l2 :: [Int]
l2 = 2:l1
--l2 = 2 mal type
l3 :: [Int]
l3 = [1,2,3]
l4 :: [Int]
l4 = [1..10]
l5 :: [Int]
l5 = [1,3..10]
myHead :: [Int] -> Int
myHead (x:xs) = x -- pattern matching
l6 :: [Int]
l6 = [10,8..0]
myTail :: [Int] -> [Int]
myTail (x:xs) = xs
-- booleen (fonctions booleennes : && || not)
myNull12 :: [Int] -> Bool
myNull12 (x:xs) = False
myNull12 xs = True
--myNull12 [] = True
-- fonction recursive
-- la fonction append s'ecrit ++ en haskell
-- la fonction ++ est infix puisqu'elle est constituee de symboles (pas des lettres)
myAppend :: [Int] -> [Int] -> [Int]
myAppend (x:xs) ys = x:myAppend xs ys
myAppend [] ys = ys
--myAppend [x] ys = x:ys
myAppend' :: [Int] -> [Int] -> [Int]
myAppend' xs ys | not (null xs) = head xs : myAppend' (tail xs) ys
| otherwise = ys
myAppend'' :: [Int] -> [Int] -> [Int]
myAppend'' xs ys | null xs = ys
| not (null xs) = head xs : myAppend'' (tail xs) ys
myAppend4 :: [Int] -> [Int] -> [Int]
myAppend4 (x:xs) ys =
let suite = myAppend4 xs ys
in x:suite
myAppend4 [] ys = ys
myAppend5 :: [Int] -> [Int] -> [Int]
myAppend5 (x:xs) ys = x:suite where suite = myAppend5 xs ys
myAppend5 [] ys = ys
myAppend6 :: [Int] -> [Int] -> [Int]
myAppend6 xs ys = myAppend6' xs
where myAppend6' (x:xs) = x:myAppend6' xs
myAppend6' [] = ys
-- a vous...
myInit :: [Int] -> [Int]
myInit (xs:[]) = []
myInit (x1:xs) = x1:myInit(xs)
myLast :: [Int] -> Int
myLast (x:[]) = x
myLast (x:xs) = myLast xs
myNull :: [Int] -> Bool
myNull [] = True
myNull x = False
myNull' :: [Int] -> Bool
myNull' = null
myLength :: [Int] -> Int
myLength (x:xs) = 1 + myLength xs
myLength [] = 0
myReverse :: [Int] -> [Int]
myReverse (x:xs) = myReverse xs ++[x]
myReverse [] = []
-- iteratif, comparer les complexites experimentalement
myReverse' :: [Int] -> [Int]
myReverse' xs = myReverse'' xs []
where myReverse'' (x:xs) ys = myReverse'' xs (x:ys)
myReverse'' [] ys = ys
myConcat :: [[Int]] -> [Int]
myConcat (l:ls) = l++myConcat(ls)
myConcat [] = []
myAnd :: [Bool] -> Bool
myAnd (x:xs)= x && myAnd(xs)
myAnd [] = True
myOr :: [Bool] -> Bool
myOr (x:xs)= x || myOr(xs)
myOr [] = False
myProduct :: [Int] -> Int
myProduct (x:xs)= x * myProduct(xs)
myProduct [] = 1
-- pas d'element neutre pour max et min
myTake :: Int -> [Int] -> [Int]
myTake 0 ls = []
myTake x [] = []
myTake x (l:ls) = l:(myTake (x-1) ls)
myDrop :: Int -> [Int] -> [Int]
myDrop 0 ls = ls
myDrop x [] = []
myDrop x (l:ls) = myDrop (x-1) ls
-- cette fonction existe sous le nom !!
myBangBang :: [Int] -> Int -> Int
myBangBang (x:xs) 0 = x
myBangBang (x:xs) y = myBangBang xs (y-1)
myInsert :: Int -> [Int] -> [Int]
myInsert x (l:ls) | x <= l = x:(l:ls)
| otherwise = l:myInsert x ls
myInsert x [] = [x]
mySort :: [Int] -> [Int]
mySort [] = []
mySort x = mySort'' x []
where mySort'' (x:xs) ys = mySort'' xs (myInsert x ys)
mySort'' [] ys = ys