Amber
Amber

Reputation: 275

how can I modify the "any" function to become more like "all" in haskell

Right now I have this piece of code: filter (any ('elem' "abc")) [String]. This checks to see if an element from [String] contains 'a' or 'b' or 'c', which returns as true, but this isn't exactily what I need. I want to see if it contains 'a' and 'b' and 'c'. the "all" function won't work because it would check if everything was 'a' or 'b' or 'c', which means something like "abcd" would return false which I don't want. Ty.

Upvotes: 1

Views: 75

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476977

You should swap the "abc" and `"abcdefg" part:

all (`elem` "abcdefg") "abc"

this thus checks if 'a', 'b' and 'c' are all elements of the "abcdefg" string.

Or you can filter a list of strings with:

filter (\x -> all (`elem` x) "abc") ["abcdef", "acdef"]

Upvotes: 2

Related Questions