this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

:sparkles: haskell studies

* Conditions
* Functions
* Lists

+317
+91
Haskell/Conditions.org
··· 1 + * Condicionais 2 + E como toda linguagem de programacao, tambem temos condicionais para criar logicas nos nossos codigos. 3 + 4 + *lembrete que a identacao de nosso codigo eh de extrema importancia* 5 + 6 + #+begin_src haskell 7 + :{ 8 + signum :: (Num a, Ord a) => a -> a 9 + signum n = if n < 0 10 + then -1 11 + else if n == 0 12 + then 0 13 + else 1 14 + :} 15 + 16 + signum 10 17 + #+end_src 18 + 19 + #+RESULTS: 20 + : 1 21 + 22 + #+begin_src haskell 23 + :{ 24 + raizes2GrauV4 :: (Ord a, Floating a) => a -> a -> a -> (a, a) 25 + raizes2GrauV4 a b c = 26 + if delta < 0 27 + then error "Delta negativo" 28 + else (x1, x2) 29 + where 30 + x1 = ((-b) + sqDelta) / (2 * a) 31 + x2 = ((-b) - sqDelta) / (2 * a) 32 + sqDelta = sqrt delta 33 + delta = b^(2 :: Int) - 4 * a * c 34 + :} 35 + 36 + raizes2GrauV4 2 3 5 37 + #+end_src 38 + 39 + #+RESULTS: 40 + #+begin_example 41 + ,*** Exception: Delta negativo 42 + CallStack (from HasCallStack): 43 + error, called at <interactive>:55:8 in interactive:Ghci19 44 + #+end_example 45 + 46 + ** Guards 47 + Tambem para facilitar a leitura e diminuir a verbosidade, podemos utilizar o *Guards* nas nossas funcoes para declarar a mesma encadeacao de condicionais feitas. 48 + #+begin_src haskell 49 + :{ 50 + signumV2 :: (Num a, Ord a) => a -> a 51 + signumV2 n 52 + | n < 0 = -1 53 + | n == 0 = 0 54 + | otherwise = 1 55 + :} 56 + 57 + signumV2 (-20) 58 + #+end_src 59 + 60 + #+RESULTS: 61 + : ghci> 62 + : <interactive>:96:1-14: warning: [-Wtype-defaults] 63 + : • Defaulting the following constraints to type ‘Integer’ 64 + : (Show a0) arising from a use of ‘print’ at <interactive>:96:1-14 65 + : (Num a0) arising from a use of ‘it’ at <interactive>:96:1-14 66 + : (Ord a0) arising from a use of ‘it’ at <interactive>:96:1-14 67 + : • In a stmt of an interactive GHCi command: print it 68 + : -1 69 + 70 + ** Pattern Matching 71 + Ou podemos tambem criar pattern matching para deixar as nossas declaracoes de funcoes mais explicitas e sem sintaxe de logica no nosso codigo, apenas declarando algumas verdades para serem executadas e avalidas mais tarde. 72 + 73 + #+begin_src haskell 74 + :{ 75 + soma 0 y = y 76 + soma x 0 = x 77 + soma x y = x + y 78 + :} 79 + #+end_src 80 + 81 + Ou podemos ignorar completamente as variavel nas nossas declaracoes de variavel nos pattern matching utilizando o ~_~ como parametro 82 + #+begin_src haskell 83 + :{ 84 + mult 0 _ = 0 85 + mult _ 0 = 0 86 + mult 1 y = y 87 + mult x 1 = x 88 + mult x y = x * y 89 + :} 90 + #+end_src 91 +
+35
Haskell/Functions.org
··· 1 + * Assinatura de funcao 2 + As assinaturas de funcoes servem para declarar a tipagem que nossa funcao ira usar, retornar e os parametros que utilizaremos na funcao. 3 + 4 + #+begin_src haskell 5 + :{ 6 + raizes2Grau :: Floating a => a -> a -> a -> (a, a) 7 + raizes2Grau a b c = (((-b) + sqrt(b^(2 :: Int) - 4 * a * c))/ (2 * a), 8 + ((-b) - sqrt(b^(2 :: Int) - 4 * a * c))/ (2 * a)) 9 + :} 10 + 11 + raizes2Grau 2 3 5 12 + #+end_src 13 + 14 + #+RESULTS: 15 + : (NaN,NaN) 16 + 17 + ** Clausula "where" 18 + Para facilitar a leitura e dividir as funcionalidades e facilitar a leitura, podemos declarar trechos do codigo para variaveis dentro da funcao a fim de facilitar a leitura e poder reaproveitar codigo dentro de nossas funcoes. 19 + 20 + #+begin_src haskell 21 + :{ 22 + raizes2GrauV2 :: Floating a => a -> a -> a -> (a, a) 23 + raizes2GrauV2 a b c = (x1, x2) 24 + where 25 + x1 = ((-b) + sqDelta) / (2 * a) 26 + x2 = ((-b) - sqDelta) / (2 * a) 27 + sqDelta = sqrt delta 28 + delta = b^2 - 4 * a * c 29 + :} 30 + 31 + raizes2GrauV2 2 3 (-5) 32 + #+end_src 33 + 34 + #+RESULTS: 35 + : (1.0,-2.5)
+191
Haskell/Lists.org
··· 1 + * Listas 2 + Listas em Haskell tem o mesmo comportamento de listas ligadas, podemos declarar elas da seguinte forma 3 + #+begin_src haskell 4 + 1 : 2 : 3 : [] 5 + #+end_src 6 + 7 + #+RESULTS: 8 + : [1,2,3] 9 + 10 + Temos 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 + 18 + Com 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 + :{ 28 + indexa :: [a] -> Int -> a 29 + indexa xs i = head (drop i xs) 30 + :} 31 + 32 + xs = [0..200] 33 + xs `indexa` 35 34 + #+end_src 35 + 36 + #+RESULTS: 37 + : 35 38 + 39 + #+begin_src haskell 40 + :{ 41 + fatorial :: Integer -> Integer 42 + fatorial n = product [2..n] 43 + :} 44 + 45 + fatorial 10 46 + #+end_src 47 + 48 + #+RESULTS: 49 + : 3628800 50 + 51 + ** List Comprehension 52 + 53 + Desta 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 + 61 + Podemos 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 + 106 + Its the same as a flatten from languages 107 + #+begin_src haskell 108 + concat [[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 116 + tam xs = sum [1 | _ <- xs] 117 + 118 + tam [1..10] 119 + #+end_src 120 + 121 + #+RESULTS: 122 + : Prelude> 10 123 + 124 + * Recursion 125 + Aqui 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. 126 + Seguindo o exemplo seguinte de fatorial: 127 + #+begin_src haskell 128 + :{ 129 + fatorial2 :: Integer -> Integer 130 + fatorial2 0 = 1 131 + fatorial2 n = n * fatorial2 (n - 1) 132 + :} 133 + 134 + fatorial2 3 135 + #+end_src 136 + 137 + #+RESULTS: 138 + : Prelude> 6 139 + 140 + A 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 + 148 + Com isso, o Haskell nao ira criar uma pilha de execucao como visto em outras linguagens 149 + 150 + ** Tail recursion 151 + Para um exemplo de tail recursion, podemos usar um exemplo para realizar o mdc 152 + #+begin_src haskell 153 + :{ 154 + mdc :: Int -> Int -> Int 155 + mdc a 0 = a 156 + mdc a b = mdc b (a `mod` b) 157 + :} 158 + 159 + mdc 48 18 160 + #+end_src 161 + 162 + #+RESULTS: 163 + : Prelude> 6 164 + 165 + Neste 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. 166 + O 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 + :{ 178 + qsort :: Ord a => [a] -> [a] 179 + qsort [] = [] 180 + qsort (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 + 187 + qsort [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]