this repo has no description
1* Listas
2Listas em Haskell tem o mesmo comportamento de listas ligadas, podemos declarar elas da seguinte forma
3#+begin_src haskell
41 : 2 : 3 : []
5#+end_src
6
7#+RESULTS:
8: [1,2,3]
9
10Temos tambem o /sugar syntax/ para declarar listas da forma mais casual e esperada
11#+begin_src haskell
12[1, 2, 3]
13#+end_src
14
15#+RESULTS:
16: [1,2,3]
17
18Com isso, podemos aproveitar para gerar listas de forma lazy (e ate listas infinitas)
19#+begin_src haskell
20[1..10]
21#+end_src
22
23#+RESULTS:
24: [1,2,3,4,5,6,7,8,9,10]
25
26#+begin_src haskell
27:{
28indexa :: [a] -> Int -> a
29indexa xs i = head (drop i xs)
30:}
31
32xs = [0..200]
33xs `indexa` 35
34#+end_src
35
36#+RESULTS:
37: 35
38
39#+begin_src haskell
40:{
41fatorial :: Integer -> Integer
42fatorial n = product [2..n]
43:}
44
45fatorial 10
46#+end_src
47
48#+RESULTS:
49: 3628800
50
51** List Comprehension
52
53Desta maneira eh chamada de compreensao de listas
54#+begin_src haskell
55[x | x <-[0,2..100], x `mod` 6 == 0]
56#+end_src
57
58#+RESULTS:
59| 0 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 72 | 78 | 84 | 90 | 96 |
60
61Podemos tambem usar duas expressoes geradoras ao mesmo tempo
62#+begin_src haskell
63[(x, y) | x <-[0..5], y <-[11..16]]
64#+end_src
65
66#+RESULTS:
67| 0 | 11 |
68| 0 | 12 |
69| 0 | 13 |
70| 0 | 14 |
71| 0 | 15 |
72| 0 | 16 |
73| 1 | 11 |
74| 1 | 12 |
75| 1 | 13 |
76| 1 | 14 |
77| 1 | 15 |
78| 1 | 16 |
79| 2 | 11 |
80| 2 | 12 |
81| 2 | 13 |
82| 2 | 14 |
83| 2 | 15 |
84| 2 | 16 |
85| 3 | 11 |
86| 3 | 12 |
87| 3 | 13 |
88| 3 | 14 |
89| 3 | 15 |
90| 3 | 16 |
91| 4 | 11 |
92| 4 | 12 |
93| 4 | 13 |
94| 4 | 14 |
95| 4 | 15 |
96| 4 | 16 |
97| 5 | 11 |
98| 5 | 12 |
99| 5 | 13 |
100| 5 | 14 |
101| 5 | 15 |
102| 5 | 16 |
103
104** Concat
105
106Its the same as a flatten from languages
107#+begin_src haskell
108concat [[1,2,4], [6,7,8]]
109#+end_src
110
111#+RESULTS:
112| 1 | 2 | 4 | 6 | 7 | 8 |
113
114** Creating ~length~ with list comprehension
115#+begin_src haskell
116tam xs = sum [1 | _ <- xs]
117
118tam [1..10]
119#+end_src
120
121#+RESULTS:
122: Prelude> 10
123
124* Recursion
125Aqui em Haskell nao temos stack overflow, como as funcoes sao de forma lazy ele ira a cada vez da recursao substituir com os novos valores na expressao.
126Seguindo o exemplo seguinte de fatorial:
127#+begin_src haskell
128:{
129fatorial2 :: Integer -> Integer
130fatorial2 0 = 1
131fatorial2 n = n * fatorial2 (n - 1)
132:}
133
134fatorial2 3
135#+end_src
136
137#+RESULTS:
138: Prelude> 6
139
140A execucao do ~fatorial2~ seria dessa forma:
141#+BEGIN_EXAMPLE
142= 3 * fatorial2 (3 - 1)
143= 3 * 2 * fatorial2 (2 - 1)
144= 3 * 2 * 1 * fatorial2 (1 - 1)
145= 3 * 2 * 1 * 1
146#+END_EXAMPLE
147
148Com isso, o Haskell nao ira criar uma pilha de execucao como visto em outras linguagens
149
150** Tail recursion
151Para um exemplo de tail recursion, podemos usar um exemplo para realizar o mdc
152#+begin_src haskell
153:{
154mdc :: Int -> Int -> Int
155mdc a 0 = a
156mdc a b = mdc b (a `mod` b)
157:}
158
159mdc 48 18
160#+end_src
161
162#+RESULTS:
163: Prelude> 6
164
165Neste exemplo, diferente do modo recursivo que a cada iteracao ira ser substituido os valores e aumentando o tamanho de nossa expressao, neste exemplo de MDC a gente mantem um tamanho fixo de expressao e de pilha.
166O Haskell ira realizar as seguintes operacoes:
167#+BEGIN_EXAMPLE
168= mdc 48 18
169= mdc 18 12
170= mdc 12 6
171= mdc 6 0
172= 6
173#+END_EXAMPLE
174
175* Quick Sort implementation
176#+begin_src haskell
177:{
178qsort :: Ord a => [a] -> [a]
179qsort [] = []
180qsort (x:xs) =
181 (qsort menores) ++ [x] ++ (qsort maiores)
182 where
183 menores = [e | e <- xs, e < x]
184 maiores = [e | e <- xs, e >= x]
185:}
186
187qsort [6,7,5,3,4,20,4444,6,89,4]
188#+end_src
189
190#+RESULTS:
191: Prelude> [3,4,4,5,6,6,7,20,89,4444]