Hi Ramesh,

Sorry. I was trying to solve a wrong problem. Please refer to solution provided by AbdulSattar Mohammed.

Regards



From: Ramesh Kumar <rameshkumar.techdynamics@ymail.com>
To: Prasanna K Rao <prasannakrao@yahoo.com>; "Beginners@haskell.org" <Beginners@haskell.org>
Sent: Wednesday, March 28, 2012 7:53 AM
Subject: Re: [Haskell-beginners] Unique integers in a list

Thanks Prasanna.

Is there a base case for the solution with recursion?
I'm trying with this code but I'm not getting the actual desired result:


isIn :: Integer -> [Integer] -> Bool
isIn _ [] = False
isIn n (x:xs) = if (n == x) then True else isIn n xs

unique :: [Integer] -> [Integer]
unique [] = []
unique (x:xs)   = if not(isIn x xs) then [x] ++ unique(xs) else unique(xs)


*Main> unique [1,2,3,4,5,4]
[1,2,3,5,4]         *** should have been  [1,2,3,5]


Thanks & Regards.





From: Prasanna K Rao <prasannakrao@yahoo.com>
To: Ramesh Kumar <rameshkumar.techdynamics@ymail.com>; "Beginners@haskell.org" <Beginners@haskell.org>
Sent: Wednesday, March 28, 2012 1:40 PM
Subject: Re: [Haskell-beginners] Unique integers in a list

Hi,

One way is to define a 'isin' function like this::

isin x (a:[])  = if (x == a) then True else False
isin x (a:as)  = if (x == a) then True else isin x as

and use it like this::

unique (x:xs)   = if not(isin x xs) then [x] ++ unique(xs) else unique(xs)

or like this::

unique(x:xs)    = [x | x <- (x:xs), not(isin x xs)] ++ unique xs

The later being the preferred one. HTH..

Regards,



From: Ramesh Kumar <rameshkumar.techdynamics@ymail.com>
To: "Beginners@haskell.org" <Beginners@haskell.org>
Sent: Wednesday, March 28, 2012 3:03 AM
Subject: [Haskell-beginners] Unique integers in a list

Hi,

I've just started learning Haskell a couple of weeks ago using Simon Thompson's "Haskell: Craft of Functional Programming".
There is an exercise in chapter 7 of the book which goes something like this:

Define a function of the type:     unique :: [Integer] -> [Integer]
which if given a list of integers, should return a list of those integers which occur only once in the input list.
Example:
   unique [5,2,4,2,3,1,5,2] should result in [4,3,1]


*** The questions assumes we know only of list comprehensions and recursion.

I am guessing the solution must include something like this:

unique :: [Integer] -> [Integer]
unique xs = [ x | x <- xs, isSingle x ]

My problem is in defining the function 'isSingle'.

I would greatly appreciate any pointers on this.

Many thanks.
Ramesh



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners