small hugs problem 
Author Message
 small hugs problem

I've come across a problem which I know I could solve easily using a normal
language but I'm stumped doing it in haskell.

I need to take a list and produce a list of pairs which contain the element
and how many times the element appears

e.g.

['a','a','b','a','b','b','a']     ->  [('a',4), ('b',2)]

I have a solution but it's pretty messy - does anyone have an elegant
solution?

Thanks

Sam



Sat, 03 Feb 2001 03:00:00 GMT  
 small hugs problem

: ['a','a','b','a','b','b','a']     ->  [('a',4), ('b',2)]

: I have a solution but it's pretty messy - does anyone have an elegant
: solution?

Always check the standard libaries to see if they don't contain partial
solutions...

import List

compact :: Ord a => [a] -> [(a,Int)]

Dirk van Deun
--


--
Knight of the Order of the Command Line  ---  http://knights.rave.org



Sat, 03 Feb 2001 03:00:00 GMT  
 small hugs problem

Quote:

> I need to take a list and produce a list of pairs which contain the element
> and how many times the element appears
> ['a','a','b','a','b','b','a']     ->  [('a',4), ('b',2)]
> I have a solution but it's pretty messy - does anyone have an elegant
> solution?

Use an Array defined over the type of the first element and containing
integers?  The problem becomes ignoring all the unset array elements
efficiently, I guess.

~kzm
--
If I haven't seen further, it is by standing in the footprints of giants



Sat, 03 Feb 2001 03:00:00 GMT  
 small hugs problem

Quote:

> I need to take a list and produce a list of pairs which contain the element
> and how many times the element appears

Sort, partition, count.


Sun, 04 Feb 2001 03:00:00 GMT  
 small hugs problem
Thanks for the help - this is the solution I'm using.
Quote:
> import List
> compact :: Ord a => [a] -> [(a,Int)]
> compact list = [ (head x,length (x)) | x<-((group.sort) list) ]
>I need to take a list and produce a list of pairs which contain the element
>and how many times the element appears

>e.g.

>['a','a','b','a','b','b','a']     ->  [('a',4), ('b',2)]



Wed, 07 Feb 2001 03:00:00 GMT  
 small hugs problem

Quote:

>> I need to take a list and produce a list of pairs which contain the element
>> and how many times the element appears

>> ['a','a','b','a','b','b','a']     ->  [('a',4), ('b',2)]

>> I have a solution but it's pretty messy - does anyone have an elegant
>> solution?

>Use an Array defined over the type of the first element and containing
>integers?  The problem becomes ignoring all the unset array elements
>efficiently, I guess.

to do this in J, one does:

    x =.  'aababba'

+-----+-----+
|+-+-+|+-+-+|
||a|4|||b|3||
|+-+-+|+-+-+|
+-----+-----+

(for more J info, see http://www.jsoftware.com )



Sat, 10 Feb 2001 03:00:00 GMT  
 small hugs problem

Quote:


>> I need to take a list and produce a list of pairs which contain the element
>> and how many times the element appears

>Sort, partition, count.

doesn't this only give the counts.  How is the actual unique element included?


Sat, 10 Feb 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. small hugs problem

2. Hugs Graphics Library problem

3. hugs 98 Float problems - can anyone help?

4. Int(eger) conversion problem in Haskell/HUGS?

5. hugs File I/O problem

6. Problems with HUGS...

7. Q: Psion and Small (really small) talk

8. small small question about linking libraries

9. Small small question about linking libraries

10. small painting problem

11. A couple of small problems

12. Small problem with package dependencies

 

 
Powered by phpBB® Forum Software