How can I remove duplicates in an F# sequence without using references

前端 未结 6 409
南方客
南方客 2021-01-20 09:07

I have a sorted sequence and want to go through it and return the unique entries in the sequence. I can do it using the following function, but it uses reference variables a

6条回答
  •  面向向阳花
    2021-01-20 09:52

    In my case I could not use Seq.distinct because I needed to preserve order of list elements. I used solution from http://ocaml.org/learn/tutorials/99problems.html. I think it is quite short

    let rec compress = function
        | a :: (b :: _ as t) -> if a = b then compress t else a :: compress t
        | smaller -> smaller
    

提交回复
热议问题