[英]Is there a function in haskell to determine if each element in a list is in another list?
我想知道haskell中是否有一個函數來確定列表中的每個元素是否在另一個列表中。 我自己寫的,但它似乎會出現在Prelude
或Data.List
each :: Eq a => [a] -> [a] -> Bool
each xs ys = foldl (\acc x -> if x `elem` ys then True && acc else False) True xs
這樣的東西已經存在了嗎?
您特別想要的集合操作不在 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
假設您的列表沒有重復值,您可以嘗試(\\)
。
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.