crislanio_macedo Posted March 9, 2015 at 01:23 AM Report Share #578996 Posted March 9, 2015 at 01:23 AM (edited) 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 March 9, 2015 at 03:06 AM by thoga31 GeSHi Link to comment Share on other sites More sharing options...
thoga31 Posted March 9, 2015 at 03:11 AM Report Share #578998 Posted March 9, 2015 at 03:11 AM (edited) 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 March 9, 2015 at 03:12 AM by thoga31 Knowledge is free! Link to comment Share on other sites More sharing options...
crislanio_macedo Posted March 9, 2015 at 03:35 PM Author Report Share #579018 Posted March 9, 2015 at 03:35 PM (edited) 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 March 9, 2015 at 08:50 PM by thoga31 GeSHi Link to comment Share on other sites More sharing options...
Raul3212 Posted March 13, 2015 at 07:33 PM Report Share #579353 Posted March 13, 2015 at 07:33 PM (edited) 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 March 13, 2015 at 07:48 PM by Raul3212 1 Report Link to comment Share on other sites More sharing options...
crislanio_macedo Posted March 13, 2015 at 10:33 PM Author Report Share #579362 Posted March 13, 2015 at 10:33 PM 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now