user2020331
user2020331

Reputation: 13

Haskell Function that Compares the Items of Two lists

Im writing a Haskell function called myElems

that takes two lists of values and returns true if all the values in the first list are in the second list. E.g., myElems "db" "abcd" should return true whereas myElems [1,2] [0,1,3,4] should return false.

myElem function is like this

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 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.

Upvotes: 0

Views: 4802

Answers (2)

Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61121

I know it's not the exact answer, but how about what Learn You Haskell book recommends:

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

Upvotes: 2

Quuxplusone
Quuxplusone

Reputation: 27350

You mean

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

Upvotes: 2

Related Questions