Lista 01 - Recursão
Autor: Beatriz | Publicado: 2013-10-06T14:38:00-07:00

Alguns exercícios de recursão

-- Lista de Exercicio 01

vendas :: Int -&gt; Int
vendas 1 = 20
vendas 2 = 32
vendas 3 = 21
vendas 4 = 60
vendas 5 = 25
vendas 6 = 12
vendas 7 = 52
vendas 8 = 28
vendas 9 = 29
vendas 10 = 40
vendas 11 = 50
vendas 12 = 33

-- Questao 01

desloca :: Int -&gt; String -&gt; String
desloca d palavra
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | d == 0 = palavra
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | d &gt; 0 =&nbsp; " " ++ desloca (d - 1) palavra

-- Questao 02

impAsterisco :: Int -&gt; String
impAsterisco&nbsp; qtd
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | qtd == 0 = ""
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | qtd &gt; 0 = "*" ++ impAsterisco (qtd - 1)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
-- Questao 03

centraliza:: String -&gt; String
centraliza palavra = desloca (length (palavra)) palavra ++ desloca (length (palavra)) ""

-- Questao 04

tamanhoLinha :: Int
tamanhoLinha = 40

cabecalho :: String 
titulo = "Relatorio de Venda de Microcomputadores"
cabecalho = impAsterisco tamanhoLinha ++ centraliza titulo ++ impAsterisco (tamanhoLinha +1) ++ "\n"

-- Questao 05

mes :: Int -&gt; String
mes m
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 00 = "Este mes nao existe."
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 01 = "Janeiro"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 02 = "Fevereiro"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 03 = "Marco"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 04 = "Abril"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 05 = "Maio"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 06 = "Junho"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 07 = "Julho"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 08 = "Agosto"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 09 = "Setembro"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 10 = "Outubro"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 11 = "Novembro"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | m == 12 = "Dezembro"
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
-- Questao 06

impBranco :: Int -&gt; String
impBranco&nbsp; qtd
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | qtd == 0 = ""
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | qtd &gt; 0 = " " ++ impBranco (qtd - 1)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
-- Questao 07

impMes :: Int -&gt; String
impMes mesp = desloca 6 (mes mesp) ++ impBranco (tamanhoLinha - length(mes mesp)) ++ show (vendas mesp) ++ "\n"

-- Questao 08

impMeses :: Int -&gt; String
impMeses 1 = impMes 1 ++ "\n"
impMeses messp = impMeses (messp - 1) ++ impMes messp ++&nbsp; "\n"&nbsp;&nbsp; 

-- Questao 09

somaVendas :: Int -&gt; Int
somaVendas 1 = vendas 1
somaVendas v = somaVendas (v - 1) +&nbsp; vendas v

-- Questao 10

impSoma :: Int -&gt; String
impSoma sv = impAsterisco tamanhoLinha ++ "\n" ++ desloca 6 ("Total de Vendas: ") ++ show (somaVendas sv) ++ "\n"

-- Questao 11

maiorVenda :: Int -&gt; Int 
maiorVenda 1 = vendas 1
maiorVenda maior = maiorVendaAux (vendas maior) (maiorVenda (maior - 1)) 

maiorVendaAux :: Int -&gt; Int
maiorVenda v1 v2
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; a &gt;= b = a
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; otherwise = b
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
Falta as útimas questões logo logo postarei aqui.