-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20150319.hs
71 lines (48 loc) · 1.63 KB
/
20150319.hs
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
double :: [Int] -> [Int]
double lista
| lista == [] = []
| otherwise = (++) [(head lista) * 2] (double (tail lista))
--outra forma (casamento de padrões)
double2 [] = []
double2 (h:t) = (2*h):(double2 t)
member :: [Int] -> Int -> Bool
member list a
| list == [] = False
| head list == a = True
| otherwise = (member (tail list) a)
--outra forma (casamento de padrões)
member2 [] a = False
member2 (h:t) a = (h==a) || (member2 t a)
digits :: String -> String
digits cadeia
| cadeia == [] = []
| (head cadeia >= '0' && head cadeia <= '9') = (++) [(head cadeia)] (digits (tail cadeia))
| otherwise = digits (tail cadeia)
--outra forma (casamento de padrões)
sumPairs :: [Int] -> [Int] -> [Int]
sumPairs lista1 lista2
| lista1 == [] && lista2 == [] = []
| lista1 == [] = lista2
| lista2 == [] = lista1
| otherwise = (++) [(head lista1 + head lista2)] (sumPairs (tail lista1) (tail lista2))
--outra forma (casamento de padrões)
sumPairs2 [] [] = []
sumPairs2 [] (h:t) = (h:t)
sumPairs2 (h:t) [] = (h:t)
sumPairs2 (h1:t1) (h2:t2) = (h1+h2):(sumPairs2 t1 t2)
--quicksort
qsort :: [Int] -> [Int]
qsort lista
| lista == [] = []
| (tail lista) == [] = [head lista]
| otherwise = (++) (qsort (menor (tail lista) (head lista))) ((head lista):(qsort (maior (tail lista) (head lista))))
menor :: [Int] -> Int -> [Int]
menor listam val
| listam == [] = []
| ((head listam) <= val) = (head listam):(menor (tail listam) val)
| otherwise = menor (tail listam) val
maior :: [Int] -> Int -> [Int]
maior listaM val
| listaM == [] =[]
| ((head listaM) > val) = ((head listaM):(maior (tail listaM) val))
| otherwise = maior (tail listaM) val