Exemplos de códigos em Haskell

COMPARTILHAR:

Whatsapp Telegram Twitter Facebook Reddit

Confira exemplos de códigos na linguagem de programação Haskell.


Abaixo você confere exemplos de códigos em Haskell para aprendizado de programação funcional.

Você pode rodar o código abaixo copiando e colando em algum compilador de Haskell online, clicando aqui você encontra um.

fact:: Int -> Int
fact 0 = 1
fact n = n * fact(n-1)

potencia:: Int -> Int -> Int
potencia a b
  | b > 0 = a * potencia a (b-1)
  | otherwise = 1

somaImpares:: [Int] -> Int
somaImpares [] = 0
somaImpares (a:x) 
  | a `mod` 2 /= 0 = a + somaImpares x
  | otherwise = 0 + somaImpares x

substituir:: Int -> Int -> [Int] -> [Int]
substituir a b [] = []
substituir a b (c:x)
  | a == c = b: substituir a b x
  | otherwise = c: substituir a b x

divisores:: Int -> Int -> [Int] -> [Int]
divisores 0 n x = x
divisores divisor n x
  | n `mod` divisor /= 0 = divisores (divisor-1) n x
  | otherwise = [divisor] ++ (divisores (divisor-1) n x)

primo:: Int -> Bool
primo n
  | length(divisores n n []) == 2 = True
  | otherwise = False

soma:: [Int] -> Int
soma [] = 0
soma (a:x) = a + soma x

perfeito:: Int -> Bool
perfeito n 
  | soma(divisores n n [])-n==n=True
  | otherwise = False

binario:: Int -> [Int]
binario 0 = []
binario n
  | n `mod` 2 == 1 = (binario (n `div` 2)) ++ [1]
  | otherwise = (binario (n `div` 2)) ++ [0]

distintos3::[Int] -> Int -> Int
distintos3 [] num = 0
distintos3 (i:vet) num
  |i == num = 1 + distintos3 vet num
  |otherwise = 0 + distintos3 vet num


distintos2:: [Int] -> Int -> Bool
distintos2 vet pos
  |pos == -1 = True 
  |otherwise = (1 == distintos3 vet (vet !! pos)) && distintos2 vet (pos-1)

distintos:: [Int] -> Bool
distintos [] = True
distintos vet = distintos2 vet ((length vet)-1)

disjunto:: Int -> [Int] -> Bool
disjunto _ [] = False
disjunto a (b:x)
  | a == b = True
  | otherwise = disjunto a x

disjuntas:: [Int] -> [Int] -> Bool
disjuntas [] [] = False
disjuntas [] y = True
disjuntas x [] = True
disjuntas (a:x) y
  | disjunto a y == True = False
  | otherwise = disjuntas x y

contrario:: [Int] -> [Int]
contrario [] = []
contrario (a:x) = contrario x ++ [a]

palindromo:: [Int] -> Bool
palindromo [] = True
palindromo x 
  | x == contrario x = True
  | otherwise = False

somaParciais:: [Int] -> [Int]
somaParciais [] = []
somaParciais [a] = [a]
somaParciais x = (somaParciais (remove x)) ++ [(soma x)]

linearizar:: [[Int]] -> [Int]
linearizar [] = []
linearizar (a:x) = a ++ linearizar x

desloca:: [Int] -> [Int]
desloca [] = []
desloca (a:x) = x ++ [a]

shift:: Int -> [Int] -> [Int]
shift _ [] = []
shift 0 x = x
shift n x = (shift (n-1) (desloca x))

remove:: [Int] -> [Int]
remove [a] = []
remove (a:x) = [a] ++ remove x

removerFim:: Int -> [Int] -> [Int]
removerFim _ [] = []
removerFim 0 x = x
removerFim n x = (removerFim (n-1) (remove x))

intercalar:: [Int] -> [Int] -> [Int]
intercalar [] [] = []
intercalar (a:x) [] = [a] ++ intercalar x []
intercalar [] (b:y) = [b] ++ intercalar [] y
intercalar (a:x) (b:y)
  | a>b = [b] ++ [a] ++ intercalar x y
  | otherwise = [a] ++ [b] ++ intercalar x y

trocar:: Int -> [Int]
trocar val
  |val >= 100 = trocar (val-100) ++ [100]
  |val >= 50 = trocar (val-50) ++ [50]
  |val >= 10 = trocar (val-10) ++ [10]
  |val >= 1 = trocar (val-1) ++ [1]
  |otherwise = []

main = do
  print$ fact 4
  print$ potencia 2 3
  print$ somaImpares [1,3,2,7,4,6,5]
  print$ substituir 1 0 [1,2,1,3,1]
  print$ primo 17
  print$ primo 0
  print$ perfeito 28
  print$ binario 20
  print$ distintos [1,2,3,4,5]
  print$ disjuntas [1,2,3] [5,4,6,0]
  print$ palindromo [1,2,3,4,3,2,1]
  print$ somaParciais [1,2,3,4]
  print$ linearizar [[1,2],[5],[0,4,2]]
  print$ shift 3 [1,5,6,7,3,4,1] -- k=3
  print$ removerFim 2 [1,2,3,4,5,6] -- n=2
  print$ intercalar [1,5,10] [2,7,9,20,25]
  print$ trocar 162

A saída do programa para as entradas acima será a seguinte:


24
8
16
[0,2,0,3,0]
True
False
True
[1,0,1,0,0]
True
True
True
[1,3,6,10]
[1,2,5,0,4,2]
[7,3,4,1,1,5,6]
[1,2,3,4]
[1,2,5,7,9,10,20,25]
[1,1,10,50,100]

COMENTÁRIOS

Nome

#ann,25,#HK,30,#LTCode,126,Artigo - Diversos,156,Artigo - Games,200,Artigo - Tecnologia,598,autor-thomaz,7,Coluna - Alternative World,24,Coluna - Fail,12,Coluna - Tec Line,14,Criptomoeda,71,Curiosidades - Diversos,49,Curiosidades - Tecnologia,50,en,2,estudo,8,HN,12,logica,14,Pentest,23,Programar C,29,Programar POO,6,Programar Python,6,Programar Shell,21,Programar verilog,12,Raspberry Pi,15,Redes,3,root,101,Shorty Awards,1,Smartphones - Reviews,33,Teoria,10,Top Nostalgia,2,VPN,18,WhatsApp,46,
ltr
item
Limon Tec: Exemplos de códigos em Haskell
Exemplos de códigos em Haskell
Confira exemplos de códigos na linguagem de programação Haskell.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-A4V4cO-E5G6QUvpRIaD8wrG5dE56LLpItbuVar3tgDpai3UljNqmIkdROE0ZvIWNA1Uo-U1yAyTYexg-UAFJQvaSIEKMyi6HgBLyz3ln3nTfSjnHpZzkYhCHCDiNcs7FCudxaV1H1YUR/s640/haskell.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-A4V4cO-E5G6QUvpRIaD8wrG5dE56LLpItbuVar3tgDpai3UljNqmIkdROE0ZvIWNA1Uo-U1yAyTYexg-UAFJQvaSIEKMyi6HgBLyz3ln3nTfSjnHpZzkYhCHCDiNcs7FCudxaV1H1YUR/s72-c/haskell.png
Limon Tec
https://www.limontec.com/2019/10/exemplos-codigos-haskell.html
https://www.limontec.com/
https://www.limontec.com/
https://www.limontec.com/2019/10/exemplos-codigos-haskell.html
false
2157924926610706248
UTF-8
Carregar todos posts Não encontramos nenhum post VER TUDO Ler mais Responder Cancelar resposta Deletar Por Home PÁGINAS POSTS Ver tudo RECOMENDADO PARA VOCÊ LABEL ARQUIVO SEARCH TODOS POSTS Não encontramos nenhum post relacionado a sua requisição VOLTAR PÁGINA INICIAL Domingo Segunda Terça Quarta Quinta Sexta Sábado Dom Seg Ter Qua Qui Sex Sab Janeiro Fevereiro Março Abril Maio Junho Julho Agosto Setembro Outubro Novembro Dezembro Jan Fev Mar Abr Maio Jun Jul Ago Set Out Nov Dez apenas agora 1 minuto atrás $$1$$ minutes ago 1 hora atrás $$1$$ hours ago Ontem $$1$$ days ago $$1$$ weeks ago mais de 5 semanas atrás Seguidores Seguir ESTE CONTEÚDO ESTÁ BLOQUEADO PASSO 1: Compartilhe com seus amigos PASSO 2: Clique no link compartilhado Copiar Todo Código Selecionar Todo Código Todos códigos foram copiados para seu clipboard Não é possível copiar códigos / textos, por favor aperte [CTRL]+[C] (ou CMD+C no Mac) para copiar Tabela de conteúdo