简体   繁体   English

haskell 中是否有一个函数来确定列表中的每个元素是否在另一个列表中?

[英]Is there a function in haskell to determine if each element in a list is in another list?

I am wondering if there is a function in haskell to determine if each element in a list is in another list.我想知道haskell中是否有一个函数来确定列表中的每个元素是否在另一个列表中。 I wrote my own, but it seems like something that would be in Prelude or Data.List我自己写的,但它似乎会出现在PreludeData.List

each :: Eq a => [a] -> [a] -> Bool
each xs ys = foldl (\acc x -> if x `elem` ys then True && acc else False) True xs

Does something like this already exist?这样的东西已经存在了吗?

The set operation you want specifically is not in the Prelude, but all makes the definition somewhat trivial (though not necessarily efficient).您特别想要的集合操作不在 Prelude 中,但all使定义有些琐碎(尽管不一定有效)。

-- Check if every element in xs is in ys (i.e., is xs a subset of ys)
each xs ys = all (`elem` ys) xs

Assuming your lists do not have duplicate values, you might try (\\) .假设您的列表没有重复值,您可以尝试(\\)

import Data.List

-- Check if removing all elements in ys from xs produces an empty list.
each xs ys = null (xs \\ ys)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM