findMatch :: [String] -> [String]
findMatch xs =
let keywords = [("data", "set")]
in [ if null x then "null" else fst y | x <- xs, y <- keywords, (snd y) == x]
Everything in this function works except for the then. If the (snd y) can’t be matched to x (the x is drawn from a list of words sent by the user), I’d like to return a string that says “null.”
in [ if (snd y) == x then fst y else "null" | x <- xs, y <- keywords]
Writing the list comprehension this way (thanks byorgey) works better, but then “null” is returned more times than I need it to be when more than 1 set of words are used in my keywords variable. I only need the string “null” to return once.
Maybe Haskell has a kind of break that I could add?
Any help would be appreciated.
I think what you want is
unless the
keywordslist can contain several pairs with the same second component and you want them all listed, then it would beIn the first case, it would be nicer if the
keywordspairs were swapped,