简体   繁体   English

Haskell函数,用于比较两个列表的项目

[英]Haskell Function that Compares the Items of Two lists

Im writing a Haskell function called myElems 我正在编写一个名为myElems的Haskell函数

that takes two lists of values and returns true if all the values in the first list are in the second list. 它接受两个值列表,如果第一个列表中的所有值都在第二个列表中,则返回true。 Eg, myElems "db" "abcd" should return true whereas myElems [1,2] [0,1,3,4] should return false . 例如, myElems "db" "abcd"应该返回truemyElems [1,2] [0,1,3,4]应该返回false

myElem function is like this myElem函数是这样的

myElem n [] = False
myElem n (x:xs) = if n == x then True else myElem n xs

this function works just fine but when I try to apply it to myElems function which has this form 此函数工作正常,但是当我尝试将其应用于具有这种形式的myElems函数时

myElems xs [] = False
myElems [] ys = False
myElems (x:xs) (y:ys) = if myElem y xs /= myElem x ys then False else myElems (tail xs) (tail ys)

it doesn't work at all. 它根本不起作用。

You mean 你的意思是

myElems [] ys = True
myElems (x:xs) ys = if myElem x ys then myElems xs ys else False

I know it's not the exact answer, but how about what Learn You Haskell book recommends: 我知道这不是确切的答案,但是“ Learn Has Haskell”书推荐的内容如何?

import qualified Data.Set as Set  
Set.fromList [2,3,4] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]

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

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