[clean-list] Can anyone improve this program?

Nick Oosterhof n.n.oosterhof at student.utwente.nl
Sun Jul 10 01:55:54 MEST 2005


Jigang Sun wrote:

>What I want to do is to get the occurences of a element in a list, e.g. [5,6,5,7,5,6] => [(5,3),(6,2)(7,1)], without using a extra parameter like n in (count n list).
>
>I prefer
>Start = count [5,6,5,7,5,6]
>than
>
>Start = count n [5,6,5,7,5,6]
>  
>
I see I misunderstood the question. Do you mean counting the number of 
occurences for each distinct element in the list? Then the following 
definition will do the job:

count []
 = []

count [x:xs]
 # xs1 = filter (\p.x<>p) xs
 = [(x,1+(length xs) - (length xs1)):(count xs1)]

I hope this is the right program :-)

regards,
Nick


More information about the clean-list mailing list