Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Unique integers in a list (Prasanna K Rao) 2. Re: Unique integers in a list (Ramesh Kumar) ---------------------------------------------------------------------- Message: 1 Date: Tue, 27 Mar 2012 23:55:40 -0700 (PDT) From: Prasanna K Rao <prasannak...@yahoo.com> Subject: Re: [Haskell-beginners] Unique integers in a list To: Ramesh Kumar <rameshkumar.techdynam...@ymail.com>, "Beginners@haskell.org" <Beginners@haskell.org> Message-ID: <1332917740.46463.yahoomail...@web44709.mail.sp1.yahoo.com> Content-Type: text/plain; charset="iso-8859-1" Hi Ramesh, Sorry. I was trying to solve a wrong problem. Please refer to solution provided by AbdulSattar Mohammed. Regards ________________________________ From: Ramesh Kumar <rameshkumar.techdynam...@ymail.com> To: Prasanna K Rao <prasannak...@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 <prasannak...@yahoo.com> >To: Ramesh Kumar <rameshkumar.techdynam...@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.techdynam...@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 > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120327/7db11064/attachment-0001.htm> ------------------------------ Message: 2 Date: Wed, 28 Mar 2012 00:09:29 -0700 (PDT) From: Ramesh Kumar <rameshkumar.techdynam...@ymail.com> Subject: Re: [Haskell-beginners] Unique integers in a list To: AbdulSattar Mohammed <codingta...@gmail.com>, "beginners@haskell.org" <beginners@haskell.org> Message-ID: <1332918569.41561.yahoomail...@web120202.mail.ne1.yahoo.com> Content-Type: text/plain; charset="iso-8859-1" Thanks Abdul Sattar. I'll try this. >________________________________ > From: AbdulSattar Mohammed <codingta...@gmail.com> >To: beginners@haskell.org >Sent: Wednesday, March 28, 2012 2:30 PM >Subject: [Haskell-beginners] Unique integers in a list > > >On Wed, Mar 28, 2012 at 11:10 AM, Prasanna K Rao <prasannak...@yahoo.com> >wrote: > >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 >> >> > > >Unnecessary. We have elem for that. >? >and use it like this:: >> >> >>unique (x:xs) ? = if not(isin x xs) then [x] ++ unique(xs) else unique(xs) >> > > >It removes duplicates. Does not remove the elements that have duplicates >(which was asked by OP). It also needs the empty list check to terminate. >It'll fail with Non-exhaustive?patterns.? > >> >>or like this:: >> >> >>unique(x:xs) ? ?= [x | x <- (x:xs), not(isin x xs)] ++ unique xs >? >The not(isin x xs) will definitely fail for all the elements except the first >one because those elements are being taken from xs. >? > >> >>The later being the preferred one. HTH.. >> >> >If you see correctly, the former is the preferred one (giving different >solution). > > >To OP,? >When we pass x to isSingle x xs, we know that there is at least one x in xs. >If we remove that x and check for the existence of x in the remainder of the >list, we know if there is more than one x. > > >isSingle x xs = x `notElem` (delete x xs) > > >delete is in Data.List.? >Regards, >> >> >> >> >> >> >>________________________________ >> From: Ramesh Kumar <rameshkumar.techdynam...@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 >> >> >> >>_______________________________________________ >>Beginners mailing list >>Beginners@haskell.org >>http://www.haskell.org/mailman/listinfo/beginners >> >> > > > >-- >Warm Regards, > >AbdulSattar Mohammed > > > > >-- >Warm Regards, > >AbdulSattar Mohammed > >_______________________________________________ >Beginners mailing list >Beginners@haskell.org >http://www.haskell.org/mailman/listinfo/beginners > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120328/e4de4fde/attachment.htm> ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 45, Issue 35 *****************************************