Duke Showbiz
Duke Showbiz

Reputation: 252

Haskell - Checking If Any String In List is In A String

I am looking for the reverse of this essentially, and am going out of my mind.

Something that reverses the arguments of the below to check of any element of a list is in a string.

Instead of: any (isInfixOf "Hask") ["I am", "new to", "Haskell"]

I need: any (isInfixOf ["I am", "new to", "Haskell"]) "I am new to"

Any help appreciated!

Upvotes: 2

Views: 513

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

You can write this with a lambda expression as:

any (\x -> isInfixOf x "I am new to") ["I am", "new to", "Haskell"]

Here x is thus the item in the list for which we call isInfixOf x "I am new to". This thus means that if one such element in the list is the infix of "I am new to", it will return True, otherwise it will return False.

You can also make use of flip :: (a -> b -> c) -> b -> a -> c:

any (flip isInfixOf "I am new to") ["I am", "new to", "Haskell"]

or with an operator section [Haskell-wiki]:

any (`isInfixOf` "I am new to") ["I am", "new to", "Haskell"]

Upvotes: 7

Related Questions