pier
pier

Reputation: 503

compare string contents in haskell

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

Answers (4)

Apocalisp
Apocalisp

Reputation: 35044

One more way.

import Data.Set
(fromList input) `isSubsetOf` (fromList listX)

Upvotes: 4

newacct
newacct

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

viraptor
viraptor

Reputation: 34205

all (flip elem listx) input

comes to mind. No idea how efficient it is though...

Upvotes: 5

Macke
Macke

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

Related Questions