Jump to content

Uniao de 2 listas sem repetição


crislanio_macedo
 Share

Recommended Posts

Olá queria fazer uma função que dada duas listas, devolva uma lista com a união das listas dadas, sem repetição de elementos.

uniao [] [] =[]
uniao [] (x:xs) =(x:xs)
uniao (x:xs) [] =(x:xs)
uniao (x:xs)(y:ys)
| x<y = x:uniao xs (y:ys)
| x==y = x: uniao xs ys
| otherwise = y: uniao (x:xs) y

Erro.

Prelude> :l Exercicios-2Entrega.hs
[1 of 1] Compiling Main             ( Exercicios-2Entrega.hs, interpreted )
Exercicios-2Entrega.hs:114:39:
   Occurs check: cannot construct the infinite type: a0 = [a0]
   In the second argument of `uniao', namely `y'
   In the second argument of `(', namely `uniao (x : xs) y'
   In the exp<b></b>ression: y : uniao (x : xs) y
Failed, modules loaded: none.
Edited by thoga31
GeSHi
Link to comment
Share on other sites

Mais uma vez, ler a mensagem do GHC e ter olhos de falcão a ler o código. Anota isto como uma Regra de Ouro da programação 😉

Vou extrair o que mais importa:

cannot construct the infinite type: a0 = [a0]
In the second argument of `uniao', namely `y'
...
In the exp<b></b>ression: y : uniao (x : xs) y

E agora olha onde ele está a apontar:

| otherwise = y: uniao (x:xs) y
--                            ^
--                            |
--                            |
--            É este o argumento correcto? Ou é ys?
Edited by thoga31

Knowledge is free!

Link to comment
Share on other sites

Mais uma vez, ler a mensagem do GHC e ter olhos de falcão a ler o código. Anota isto como uma Regra de Ouro da programação 😉

Vou extrair o que mais importa:

cannot construct the infinite type: a0 = [a0]
In the second argument of `uniao', namely `y'
...
In the exp<b></b>ression: y : uniao (x : xs) y

E agora olha onde ele está a apontar:

| otherwise = y: uniao (x:xs) y
--                            ^
--                            |
--                            |
--            É este o argumento correcto? Ou é ys?

Ok, grande falta de atenção mesmo, havia percebido aqui mais cedo, obrigado pelas dicas e explicações concisas.

frequencia a xs = length [x | x<-xs, a==x]
pertence a xs = frequencia a xs > 0
-- uniao [] [] =[]
uniao' [] y = y
uniao' (x:xs) a@(y:ys) = if (pertence x a) then uniao' xs a else x:uniao' xs a
Edited by thoga31
GeSHi
Link to comment
Share on other sites

Consegui resolver assim, Crislanio:

A função elemento retorna True se um determinado elemento está na lista:

elemento x [] = False
elemento x (y:ys) |x == y = True
|otherwise = elemento x ys

A união pode ser escrita usando a função elemento:

uniao xs [] = xs
uniao xs (y:ys) |elemento y xs = uniao xs ys ++ xs
|otherwise = uniao xs ys ++ [y]

👍

Edited by Raul3212
  • Vote 1
Link to comment
Share on other sites

Consegui resolver assim, Crislanio:

A função elemento retorna True se um determinado elemento está na lista:

elemento x [] = False
elemento x (y:ys) |x == y = True
|otherwise = elemento x ys

A união pode ser escrita usando a função elemento:

uniao xs [] = xs
uniao xs (y:ys) |elemento y xs = uniao xs ys ++ xs
|otherwise = uniao xs ys ++ [y]

👍

uniao [] [] =[]
uniao [] (x:xs) =(x:xs)
uniao (x:xs) [] =(x:xs)
uniao (x:xs)(y:ys)
| x<y = x:uniao xs (y:ys)
| x==y = x: uniao xs ys
| otherwise = y: uniao (x:xs) ys
-- uniao [] [] =[]
uniao' [] y = y
uniao' (x:xs) a@(y:ys) = if (pertence x a) then uniao' xs a else x:uniao' xs a

Ok, Raul, deu certo.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.