views:

134

answers:

4

Im trying to figure out how to create a new list that has only the elements that occur only once. I can't use recursion either.

This is a small part of a bigger function. Im trying to write a function to get the Intersect of two sets. Basically i combined the sets. Then sorted. Then i want to combine that with set B and get rid of all the duplicates in that set. Unless someone else knows an easier way.

I can use high order functions, but i can't use recursion.

Thanks

Edit: Example:

[1,2,3,3,4,4,5,5] Should come out as [1,2]

+1  A: 

I hope this hint is sufficiently vague. :)

Think about grouping duplicate elements together.

dino
I know how to zip and then get rid of the duplicates. But i only want the elements that occur only once. Edited my question with an example.
Matt
Do you think it would help if the, now adjacent, elements were themselves in smaller, internal lists?
dino
Oh I see, I get no love for trying to help the OP to come up with the answer on his own. bah
dino
Thanks for trying to help, but i would have still sat there scratching my head. I didn't know about the group function in haskell.
Matt
+1  A: 

Think about what data structure you'd use to keep track of how many times each thing shows up.

Daenyth
+2  A: 

This question was already asked before and essentially you just want to group them together by counting the number of elements and then only extracting those with a count of one. (probably using 'filter')

Please see http://stackoverflow.com/questions/3710976/counting-unique-elements-in-a-list/3713886#3713886

Robert Massaioli
The question is tagged as homework, so he probably wants hints more than someone to solve it for him
Daenyth
you're a genius if i can use group.
Matt
@Daenyth yeah but the first part of doing homework is doing your research. I wanted to show that perhaps a little more time searching would have led to the answer, maybe.
Robert Massaioli
@Matt hopefully you can and then hopefully it helps; better yet if you understand why it works then that is good enough for me.
Robert Massaioli
I had searched on here, but couldnt find anything as i didn't want to count unique elements, but just gather the ones that had no duplicates. But i didn't even know about the "group" function. I did come up with a way to do what i need to do with group, just need to see if i can use it now.
Matt
+1  A: 

How about:

concat $ filter (null . tail) $ group [1,2,3,3,4,4,5,5]
stusmith