let undefined = ["string"; ""; "string"; "boolean";"";"innermost"]
我有一個列表,我想寫一個函數,返回一個沒有重複和空字符串列表的列表。例如上面的undefined
名單將返回:刪除重複的字符串和空字符串
["string"; "boolean"; "innermost"]
我寫這篇文章的功能還給我沒有重複的,但我怎麼可以測試一個空字符串添加條件。
let rec uniquify = function
| [] -> []
| x::xs -> x :: uniquify (List.filter ((<>) x) xs)
非常感謝您
@ Febrice Le Fessant我試過你的代碼,並且在第8行(iter s set tail)編譯器給了我這個錯誤:Error:「This expression has type StringSet.elt = string but a expectation of type'list 「。 – Quyen 2012-02-18 05:52:18
第8行有一個小錯誤,iter的第一個參數是一個字符串列表,而不僅僅是一個字符串。 我剛剛在[TryOCaml](http://try.ocamlpro.com/)上試過了,現在它工作正常。 – cago 2012-02-18 12:08:05