I need to remove duplicates from lazy list. Here is some piece of code I managed to write but I have no clue on how to fix it to work properly.
I know how to do this using regular list, but lazy list are still new to me, so I will be happy with any help. Here is piece of code I tried to write:
let rec removeDuplicate = function
LNil -> LNil
|LCons(x,xf) -> x::(removeDuplicate (List.filter (fun a -> a<>x) xf));;
Thanks in advance for your help.
The first problem, that I see in your code, is that instead of building lazy list in your second clause, you're actually building an OCaml's built-in list
, with ::
consing operator. Also, you can't use List.filter
on the tail part, since it also operates on values of type 'a list
, not yours list.