Capítulo 03 do Livro Introdução ao Haskell
Autor: Beatriz | Publicado: 2013-09-29T08:16:00-07:00




Respostas do capítulo 02 do Livro Introdução ao Haskell
Autores: Stenio Longo Araújo
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Benedito Melo Acióly

OBS: Não possui a resposta dos exercícios número 08 e 09 

-- EXEMPLOS------------------------------------------------------------------------------------------------------ Objetivo: Calcular o fatorial de um número----------------------------------------------------------------------------------------------------fat :: Int -&gt; Intfat a &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | a == 0 = 1&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | a &gt; 0 = a * fat (a - 1)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Realizar contagem dos acessos totais até aquela semana----------------------------------------------------------------------------------------------------acessoTotal :: Int -&gt; IntacessoTotal n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 0 = numAcesso 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | otherwise = acessoTotal (n - 1) + numAcesso n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Auxiliar na contagem de acessos----------------------------------------------------------------------------------------------------numAcesso :: Int -&gt; IntnumAcesso n &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 0 = 15&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 1 = 5&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 2 = 7&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 3 = 18&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 4 = 7&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 5 = 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 6 = 5&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | otherwise = 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Calcular o fatorial de um número usando casamento padrão----------------------------------------------------------------------------------------------------fatcp :: Int -&gt; Intfatcp 0 = 1fatcp b = b * fat (b - 1)------------------------------------------------------------------------------------------------------ Objetivo: Testar se um número é 0 ou não----------------------------------------------------------------------------------------------------ehZero :: Int -&gt; BoolehZero 0 = TrueehZero _ = False-- EXERCÍCIOS------------------------------------------------------------------------------------------------------ Objetivo: Testar eficiência do fatorial----------------------------------------------------------------------------------------------------fat2 :: Int -&gt; Intfat2 n = fatAcum 1 n------------------------------------------------------------------------------------------------------ Objetivo: Auxiliar função fat2----------------------------------------------------------------------------------------------------fatAcum :: Int -&gt; Int -&gt; IntfatAcum ac n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | (n == 0) = ac&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | (n &gt; 0) = fatAcum (ac * n) (n - 1)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Calcular potência de número----------------------------------------------------------------------------------------------------pot :: Int -&gt; Int -&gt; Int pot l b&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | b == 0 = 1&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | b == 1 = l&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | b &gt; 1 = l * pot l (b - 1)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Calcular a soma de todos os números pares num intervalo ---------------------------------------------------------------------------------------------------somaPares :: Int -&gt; IntsomaPares n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 0 = 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | n == 1 = 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | mod n 2 == 0 = n + (n - 2)------------------------------------------------------------------------------------------------------ Objetivo: Somar naturais utilizando função sucessor-- succ já é uma função definda no pacote da linguagem---------------------------------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; soma :: Int -&gt; Int -&gt; Intsoma l 0 = lsoma 0 b = bsoma l b&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | l &lt; b = succ (soma (l - 1) b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | otherwise = succ (soma l (b - 1))&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Soma de fracionários---------------------------------------------------------------------------------------------------somaFra :: Float -&gt; FloatsomaFra l &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | l == 0 = 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | l == 1 = 1&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | l &gt; 1 = 1/l + somaFra (l - 1)------------------------------------------------------------------------------------------------------ Objetivo: Calcular a potência de um número diividido pelo fatorial do mesmo---------------------------------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Questão anterior organizada em tabela---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Objetivo: Algoritmo de Euclides ----------------------------------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; euclides :: Int -&gt; Int -&gt; Inteuclides l 0 = l euclides l b = euclides b (mod l b)------------------------------------------------------------------------------------------------------ Objetivo: Realizar o somatório ----------------------------------------------------------------------------------------------------somatorio :: Int -&gt; Intsomatorio 1 = 1somatorio n = (n*n) + somatorio(n-1)&nbsp;&nbsp;&nbsp; somatorio2 :: Int -&gt; Intsomatorio2 1 = 1somatorio2 n = fat(n) + somatorio(n-1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ------------------------------------------------------------------------------------------------------ Objetivo: Realizar a tabuada----------------------------------------------------------------------------------------------------tabuada :: Int -&gt; Stringtabuada l = valoresImpressos l 10valoresImpressos :: Int -&gt; Int -&gt; StringvaloresImpressos l 1 = resultadoFinal l 1valoresImpressos l b= valoresImpressos l (b-1) ++ resultadoFinal l b&nbsp; resultadoFinal :: Int -&gt; Int -&gt; StringresultadoFinal l i = show(l) ++ "X" ++ show (i) ++ " = " ++show (l*i)&nbsp;&nbsp;&nbsp; ++ "\n"&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;

Ótimos Estudos