I have the following code:
recorrido :: Int -> [Int] -> Int
recorrido _ [] = 0
recorrido x (y:ys) = if x/=y then recorrido x ys else (-1)
unique :: [Int] -> [Int]
unique [] = []
unique (x:xs) = [ x | x <- xs , recorrido x xs == 0]
The unique function iterates through each element of the list, and with iteration I compare the element with the other elements of the list, seeking to know if that element is repeated or not. If it is 0, it is not repeated and is added to the final result, if it is repeated it returns -1. When I compile, there are apparently no errors, but I always get an empty list:
PS C:\Users\CIEC\Documents\HASKELL> ghci 1.hs
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( 1.hs, interpreted )
Ok, one module loaded.
*Main> unique [1,2,3]
[]
*Main> unique [1,1,1]
[]
*Main> unique [1,2,3,1]
[]
What am I doing wrong?
In
unique
, when you do the compression[ x | x <- xs , recorrido x xs == 0]
you are hiding the argument of the functionx
.For example, when you do
unique [1, 2, 3]
, the following substitutions are made:A simple way is to
unique
recursive: