是否有库函数可以找到List1 minus elements that appear in List2
?我一直在谷歌搜索,并没有找到太多。
自己写它似乎太无足轻重。我编写了一个函数来从列表中删除特定元素,但这更简单:
let rec difference l arg = match l with
| [] -> []
| x :: xs ->
if (x = arg) then difference xs arg
else x :: difference xs arg;;
答案 0 :(得分:8)
这会吗?
let diff l1 l2 = List.filter (fun x -> not (List.mem x l2)) l1
答案 1 :(得分:2)
我最终实际做的只是写另一个函数,它会调用我发布的第一个函数
let rec difference l arg = match l with
| [] -> []
| x :: xs ->
if (x = arg) then difference xs arg
else x :: difference xs arg;;
let rec list_diff l1 l2 = match l2 with
| [] -> l1
| x :: xs -> list_diff (difference l1 x) xs;;
虽然我接受的解决方案更优雅