First of all, never simply state "does not work" in a question. This leaves to the reader to check whether it's a compile time error, run time error, or a wrong result.
In this case, I am guessing it's a wrong result, like this:
> compress [1,1,2,2,3,3,1]
[1,2,3,1]
The problem with your code is that it removes successive duplicates, only. The first pair of 1
s gets compressed, but the last lone 1
is not removed because of that.
If you can, sort the list in advance. That will make equal elements close, and then compress
does the right job. The output will be in a different order, of course. There are ways to keep the order too if needed (start with zip [0..] xs
, then sort, then ...).
If you can not sort becuase there is really no practical way to define a comparison, but only an equality, then use nub
. Be careful that this is much less efficient than sorting & compressing. This loss of performance is intrinsic: without a comparator, you can only use an inefficient quadratic algorithm.