domingo, 29 de setembro de 2013

Capítulo 03 do Livro Introdução ao Haskell




Respostas do capítulo 02 do Livro Introdução ao Haskell
Autores: Stenio Longo Araújo
                 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 -> Int
fat a
        | a == 0 = 1
        | a > 0 = a * fat (a - 1)
       
----------------------------------------------------------------------------------------------------
-- Objetivo: Realizar contagem dos acessos totais até aquela semana
----------------------------------------------------------------------------------------------------
acessoTotal :: Int -> Int
acessoTotal n
        | n == 0 = numAcesso 0
        | otherwise = acessoTotal (n - 1) + numAcesso n
       
----------------------------------------------------------------------------------------------------
-- Objetivo: Auxiliar na contagem de acessos
----------------------------------------------------------------------------------------------------
numAcesso :: Int -> Int
numAcesso n
        | n == 0 = 15
        | n == 1 = 5
        | n == 2 = 7
        | n == 3 = 18
        | n == 4 = 7
        | n == 5 = 0
        | n == 6 = 5
        | otherwise = 0
       
----------------------------------------------------------------------------------------------------
-- Objetivo: Calcular o fatorial de um número usando casamento padrão
----------------------------------------------------------------------------------------------------
fatcp :: Int -> Int
fatcp 0 = 1
fatcp b = b * fat (b - 1)

----------------------------------------------------------------------------------------------------
-- Objetivo: Testar se um número é 0 ou não
----------------------------------------------------------------------------------------------------
ehZero :: Int -> Bool
ehZero 0 = True
ehZero _ = False

-- EXERCÍCIOS
----------------------------------------------------------------------------------------------------
-- Objetivo: Testar eficiência do fatorial
----------------------------------------------------------------------------------------------------
fat2 :: Int -> Int
fat2 n = fatAcum 1 n

----------------------------------------------------------------------------------------------------
-- Objetivo: Auxiliar função fat2
----------------------------------------------------------------------------------------------------
fatAcum :: Int -> Int -> Int
fatAcum ac n
        | (n == 0) = ac
        | (n > 0) = fatAcum (ac * n) (n - 1)
       
----------------------------------------------------------------------------------------------------
-- Objetivo: Calcular potência de número
----------------------------------------------------------------------------------------------------
pot :: Int -> Int -> Int
pot l b
        | b == 0 = 1
        | b == 1 = l
        | b > 1 = l * pot l (b - 1)
       
----------------------------------------------------------------------------------------------------
-- Objetivo: Calcular a soma de todos os números pares num intervalo
---------------------------------------------------------------------------------------------------
somaPares :: Int -> Int
somaPares n
        | n == 0 = 0
        | n == 1 = 0
        | mod n 2 == 0 = n + (n - 2)

----------------------------------------------------------------------------------------------------
-- Objetivo: Somar naturais utilizando função sucessor
-- succ já é uma função definda no pacote da linguagem
---------------------------------------------------------------------------------------------------   
soma :: Int -> Int -> Int
soma l 0 = l
soma 0 b = b
soma l b
        | l < b = succ (soma (l - 1) b)
        | otherwise = succ (soma l (b - 1))
       
----------------------------------------------------------------------------------------------------
-- Objetivo: Soma de fracionários
---------------------------------------------------------------------------------------------------
somaFra :: Float -> Float
somaFra l
        | l == 0 = 0
        | l == 1 = 1
        | l > 1 = 1/l + somaFra (l - 1)

----------------------------------------------------------------------------------------------------
-- Objetivo: Calcular a potência de um número diividido pelo fatorial do mesmo
---------------------------------------------------------------------------------------------------       



----------------------------------------------------------------------------------------------------
-- Objetivo: Questão anterior organizada em tabela
----------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------
-- Objetivo: Algoritmo de Euclides
----------------------------------------------------------------------------------------------------   
euclides :: Int -> Int -> Int
euclides l 0 = l
euclides l b = euclides b (mod l b)

----------------------------------------------------------------------------------------------------
-- Objetivo: Realizar o somatório
----------------------------------------------------------------------------------------------------
somatorio :: Int -> Int
somatorio 1 = 1
somatorio n = (n*n) + somatorio(n-1)   

somatorio2 :: Int -> Int
somatorio2 1 = 1
somatorio2 n = fat(n) + somatorio(n-1)      

----------------------------------------------------------------------------------------------------
-- Objetivo: Realizar a tabuada
----------------------------------------------------------------------------------------------------
tabuada :: Int -> String
tabuada l = valoresImpressos l 10

valoresImpressos :: Int -> Int -> String
valoresImpressos l 1 = resultadoFinal l 1
valoresImpressos l b= valoresImpressos l (b-1) ++ resultadoFinal l b 

resultadoFinal :: Int -> Int -> String
resultadoFinal l i = show(l) ++ "X" ++ show (i) ++ " = " ++show (l*i)    ++ "\n"        


Ótimos Estudos

0 comentários:

Postar um comentário