Case insensitive without importing modules. I need the original output and not the lower ones

Viewed 129

this is my code:

x = ["Hello","Nice","WELCOME"]
y = "HELLO"
app x y = filter (isInfixOf (map to Lower y)) (map (map toLower) x)

Output: app x y -> ["hello"]

I need the original output ("HELLO") and not the lower one without importing modules.--

thank you

1 Answers

You should only map toLower in the filter condition, not perform a mapping on the elements itself:

app :: [String] -> String -> [String]
app xs y = filter (isInfixOf (map toLower y) . map toLower) xs

Note that using toLower x == toLower y is however not a good way to do case insensitive matching: certain characters do not have an uppercase/lowercase variant, and the rules of matching two strings in a caseinsensitive manner are more complicated. Usually one uses toCaseFold :: Text -> Text to normalize text to do case-insensitive matching. For example:

ghci> map toLower "straße" == map toLower "strasse"
False
ghci> toCaseFold "straße" == toCaseFold "strasse"
True
Related