本文介绍了查找两个列表之间差异的库函数 - OCaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有库函数可以查找List1 减去出现在List2 中的元素?我一直在谷歌搜索,并没有找到太多.

Is there a library function to find List1 minus elements that appear in List2? I've been googling around and haven't found much.

自己编写它似乎并不简单.我编写了一个函数来从列表中删除特定元素,但这要简单得多:

It doesn't seem too trivial to write it myself. I've written a function to remove a specific element from a list but that's much more simple:

let rec difference l arg = match l with
| [] -> []
| x :: xs ->
    if (x = arg) then difference xs arg
    else x :: difference xs arg;;

推荐答案

这行吗?

let diff l1 l2 = List.filter (fun x -> not (List.mem x l2)) l1

这篇关于查找两个列表之间差异的库函数 - OCaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:53