Reputation: 503
I have a 2 lists of strings
eg:
listx = ["name","age","rank"]
input = ["name","age"]
How can I compare the two lists to check whether the listx
contains "name"
& "age"
given in input
?
Upvotes: 3
Views: 4936
Reputation: 35044
One more way.
import Data.Set
(fromList input) `isSubsetOf` (fromList listX)
Upvotes: 4
Reputation: 122538
B is a subset of A iff B \ A is empty
so another way to do it is
import Data.List ((\\))
null (input \\ listx)
Upvotes: 6
Reputation: 34205
all (flip elem listx) input
comes to mind. No idea how efficient it is though...
Upvotes: 5
Reputation: 25710
Is this homework? :)
You need to create one or two recursive functions to walk through both lists, and search for every string in the input.
Or, you could look up some good functions in the Prelude that helps here.
Upvotes: 4