let undefined = ["string"; ""; "string"; "boolean";"";"innermost"]
I have a list and I want to write a function that return a list without duplicate and empty string list. For example the undefined list above will return :
["string"; "boolean"; "innermost"]
I write this function it return for me without duplicate but how can I add the condition with testing an empty string.
let rec uniquify = function
| [] -> []
| x::xs -> x :: uniquify (List.filter ((<>) x) xs)
Thank you very much
Just pipe the result to
List.filter (fun s -> s <> "")to remove the empty string afterwards. That’s the simple, compositional way, yo u could also hack your function to drop it silentlyNote that your function is quadratic, you can have better complexity by sorting the list first, or by converting to a set and back. Batteries has functions to do that for you.